diff --git a/hawkbit-sdk/hawkbit-sdk-commons/pom.xml b/hawkbit-sdk/hawkbit-sdk-commons/pom.xml index 978a773c18..a0e57a6c68 100644 --- a/hawkbit-sdk/hawkbit-sdk-commons/pom.xml +++ b/hawkbit-sdk/hawkbit-sdk-commons/pom.xml @@ -23,10 +23,7 @@ hawkBit :: SDK :: Commons SDK commons - - 4.1.0 - 13.2.1 - + @@ -39,7 +36,6 @@ feign-hc5 ${openfeign-hc5.version} - org.springframework.boot spring-boot-starter-hateoas diff --git a/hawkbit-sdk/hawkbit-sdk-demo/pom.xml b/hawkbit-sdk/hawkbit-sdk-demo/pom.xml index 55559acb76..3e4e8ccde3 100644 --- a/hawkbit-sdk/hawkbit-sdk-demo/pom.xml +++ b/hawkbit-sdk/hawkbit-sdk-demo/pom.xml @@ -42,7 +42,7 @@ org.eclipse.hawkbit - hawkbit-mgmt-api + hawkbit-sdk-mgmt ${project.version} diff --git a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/device/DeviceApp.java b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/device/DeviceApp.java index 0bae913718..a3ac8d9c50 100644 --- a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/device/DeviceApp.java +++ b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/device/DeviceApp.java @@ -15,25 +15,22 @@ import feign.codec.Encoder; import lombok.extern.slf4j.Slf4j; import org.eclipse.hawkbit.sdk.Controller; -import org.eclipse.hawkbit.sdk.HawkbitServer; import org.eclipse.hawkbit.sdk.HawkbitClient; -import org.eclipse.hawkbit.sdk.HawkbitSDKConfigurtion; +import org.eclipse.hawkbit.sdk.HawkbitServer; import org.eclipse.hawkbit.sdk.Tenant; -import org.eclipse.hawkbit.sdk.demo.SetupHelper; import org.eclipse.hawkbit.sdk.device.DdiController; +import org.eclipse.hawkbit.sdk.device.DdiTenant; import org.eclipse.hawkbit.sdk.device.UpdateHandler; -import org.springframework.beans.factory.annotation.Value; +import org.eclipse.hawkbit.sdk.mgmt.MgmtApi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Import; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.util.ObjectUtils; import java.util.Optional; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; /** * Abstract class representing DDI device connecting directly to hawkVit. @@ -54,49 +51,49 @@ HawkbitClient hawkbitClient( } @Bean - DdiController device( - @Value("${hawkbit.device:controller-default}") final String controllerId, - @Value("${hawkbit.device.securityToken:}") final String securityToken, - final Tenant defaultTenant, - final Optional updateHandler, - final HawkbitClient hawkbitClient) { - return new DdiController( - defaultTenant, - Controller.builder() - .controllerId(controllerId) - .securityToken(ObjectUtils.isEmpty(securityToken) ? - (ObjectUtils.isEmpty(defaultTenant.getGatewayToken()) ? SetupHelper.randomToken() : securityToken) : - securityToken) - .build(), - updateHandler.orElse(null), - hawkbitClient).setOverridePollMillis(10_000); + DdiTenant ddiTenant(final Tenant defaultTenant, + final HawkbitClient hawkbitClient) { + return new DdiTenant(defaultTenant, hawkbitClient); + } + + @Bean + MgmtApi mgmtApi(final Tenant tenant, final HawkbitClient hawkbitClient) { + return new MgmtApi(tenant, hawkbitClient); } @ShellComponent public static class Shell { - private final Tenant tenant; + private final DdiTenant ddiTenant; + private final DdiController device; - private final HawkbitClient hawkbitClient; + private final MgmtApi mgmtApi; + - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); + Shell(final DdiTenant ddiTenant, final MgmtApi mgmtApi, final Optional updateHandler) { + this.ddiTenant = ddiTenant; + this.mgmtApi = mgmtApi; + String controllerId = System.getProperty("demo.controller.id"); + String securityToken = System.getProperty("demo.controller.securityToken"); - Shell(final Tenant tenant, final DdiController device, final HawkbitClient hawkbitClient) { - this.tenant = tenant; - this.device = device; - this.hawkbitClient = hawkbitClient; + this.device = this.ddiTenant.createController(Controller.builder() + .controllerId(controllerId) + .securityToken(ObjectUtils.isEmpty(securityToken) ? + (ObjectUtils.isEmpty(ddiTenant.getTenant().getGatewayToken()) ? MgmtApi.randomToken() : securityToken) : + securityToken) + .build(), + updateHandler.orElse(null)).setOverridePollMillis(10_000); } @ShellMethod(key = "setup") public void setup() { - SetupHelper.setupTargetAuthentication(hawkbitClient, tenant); - SetupHelper.setupTargetToken( - device.getControllerId(), device.getTargetSecurityToken(), hawkbitClient, tenant); + mgmtApi.setupTargetAuthentication(); + mgmtApi.setupTargetToken(device.getControllerId(),device.getTargetSecurityToken()); } @ShellMethod(key = "start") public void start() { - device.start(scheduler); + device.start(Executors.newSingleThreadScheduledExecutor()); } @ShellMethod(key = "stop") diff --git a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/dmf/DmfApp.java b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/dmf/DmfApp.java index 2095388fb8..8a8e632a2a 100644 --- a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/dmf/DmfApp.java +++ b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/dmf/DmfApp.java @@ -27,6 +27,7 @@ import org.springframework.shell.standard.ShellOption; import java.util.Optional; +import java.util.concurrent.Executors; /** * Abstract class representing DDI device connecting directly to hawkVit. @@ -45,33 +46,35 @@ Amqp amqp(final RabbitProperties rabbitProperties, final AmqpProperties amqpProp return new Amqp(rabbitProperties, amqpProperties); } + @Bean + DmfTenant dmfTenant(Tenant tenant, Amqp amqp) { + return new DmfTenant(tenant, amqp); + } + @ShellComponent public static class Shell { - private final UpdateHandler updateHandler; private final DmfTenant dmfTenant; + private final UpdateHandler updateHandler; - Shell(final Tenant tenant, final Optional updateHandler, final Amqp amqp) { + Shell(final DmfTenant dmfTenant, final Optional updateHandler) { + this.dmfTenant = dmfTenant; this.updateHandler = updateHandler.orElse(null); - dmfTenant = new DmfTenant(tenant, amqp); } @ShellMethod(key = "start-one") public void startOne(@ShellOption("--id") final String controllerId) { - if (dmfTenant.getController(controllerId).isEmpty()) { - dmfTenant.create( - Controller.builder().controllerId(controllerId).build(), - updateHandler).connect(); - } + dmfTenant.getController(controllerId).ifPresentOrElse( + dmfController -> dmfController.start(Executors.newSingleThreadScheduledExecutor()), + () -> dmfTenant.createController(Controller.builder().controllerId(controllerId).build(), updateHandler) + .start(Executors.newSingleThreadScheduledExecutor())); } @ShellMethod(key = "stop-one") public void stopOne(@ShellOption("--id") final String controllerId) { dmfTenant.getController(controllerId).ifPresentOrElse( DmfController::stop, - () -> { - throw new IllegalArgumentException("Controller with id " + controllerId + " not found!"); - }); + () -> log.error("Controller with id " + controllerId + " not found!")); } @ShellMethod(key = "start") diff --git a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/multidevice/MultiDeviceApp.java b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/multidevice/MultiDeviceApp.java index 65ebc876ca..2ab418953d 100644 --- a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/multidevice/MultiDeviceApp.java +++ b/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/multidevice/MultiDeviceApp.java @@ -15,26 +15,22 @@ import feign.codec.Encoder; import lombok.extern.slf4j.Slf4j; import org.eclipse.hawkbit.sdk.Controller; -import org.eclipse.hawkbit.sdk.HawkbitServer; import org.eclipse.hawkbit.sdk.HawkbitClient; -import org.eclipse.hawkbit.sdk.HawkbitSDKConfigurtion; +import org.eclipse.hawkbit.sdk.HawkbitServer; import org.eclipse.hawkbit.sdk.Tenant; -import org.eclipse.hawkbit.sdk.demo.SetupHelper; import org.eclipse.hawkbit.sdk.device.DdiController; +import org.eclipse.hawkbit.sdk.device.DdiTenant; import org.eclipse.hawkbit.sdk.device.UpdateHandler; +import org.eclipse.hawkbit.sdk.mgmt.MgmtApi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Import; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; -import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; /** * Abstract class representing DDI device connecting directly to hawkVit. @@ -54,66 +50,66 @@ HawkbitClient hawkbitClient( return new HawkbitClient(hawkBitServer, client, encoder, decoder, contract); } + @Bean + DdiTenant ddiTenant(final Tenant defaultTenant, + final HawkbitClient hawkbitClient) { + return new DdiTenant(defaultTenant, hawkbitClient); + } + + @Bean + MgmtApi mgmtApi(final Tenant defaultTenant, final HawkbitClient hawkbitClient) { + return new MgmtApi(defaultTenant, hawkbitClient); + } + @ShellComponent public static class Shell { - private final Tenant tenant; + private final DdiTenant ddiTenant; + private final MgmtApi mgmtApi; private final UpdateHandler updateHandler; - private final HawkbitClient hawkbitClient; - private final Map devices = new ConcurrentHashMap<>(); - - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); private boolean setup; - Shell(final Tenant tenant, final Optional updateHandler, final HawkbitClient hawkbitClient) { - this.tenant = tenant; + Shell(final DdiTenant ddiTenant, final MgmtApi mgmtApi, final Optional updateHandler) { + this.ddiTenant = ddiTenant; + this.mgmtApi = mgmtApi; this.updateHandler = updateHandler.orElse(null); - this.hawkbitClient = hawkbitClient; } @ShellMethod(key = "setup") public void setup() { - SetupHelper.setupTargetAuthentication(hawkbitClient, tenant); + mgmtApi.setupTargetAuthentication(); setup = true; } @ShellMethod(key = "start-one") public void startOne(@ShellOption("--id") final String controllerId) { - DdiController device = devices.get(controllerId); final String securityTargetToken; if (setup) { - securityTargetToken = SetupHelper.setupTargetToken( - controllerId, null, hawkbitClient, tenant); + securityTargetToken = mgmtApi.setupTargetToken(controllerId,null); } else { securityTargetToken = null; } - if (device == null) { - device = new DdiController( - tenant, - Controller.builder() + // Create device with security token if not yet registered in this execution + // if already created in this execution of app, just start the poll + // for each new device - separate ThreadScheduler + ddiTenant.getController(controllerId).ifPresentOrElse( + ddiController -> ddiController.start(Executors.newSingleThreadScheduledExecutor()), + () -> ddiTenant.createController(Controller.builder() .controllerId(controllerId) .securityToken(securityTargetToken) - .build(), - updateHandler, - hawkbitClient).setOverridePollMillis(10_000); - final DdiController oldDevice = devices.putIfAbsent(controllerId, device); - if (oldDevice != null) { - device = oldDevice; // reuse existing - } - } - - device.start(scheduler); + .build(),updateHandler) + .setOverridePollMillis(10_000) + .start(Executors.newSingleThreadScheduledExecutor()) + ); } @ShellMethod(key = "stop-one") public void stopOne(@ShellOption("--id") final String controllerId) { - final DdiController device = devices.get(controllerId); - if (device == null) { - throw new IllegalArgumentException("Controller with id " + controllerId + " not found!"); - } else { - device.stop(); - } + ddiTenant.getController(controllerId).ifPresentOrElse( + DdiController::stop, + () -> log.error("Controller with id " + controllerId + " not found!")); + } @ShellMethod(key = "start") diff --git a/hawkbit-sdk/hawkbit-sdk-device/pom.xml b/hawkbit-sdk/hawkbit-sdk-device/pom.xml index 50c1676e3b..6b490a16b4 100644 --- a/hawkbit-sdk/hawkbit-sdk-device/pom.xml +++ b/hawkbit-sdk/hawkbit-sdk-device/pom.xml @@ -35,5 +35,6 @@ hawkbit-ddi-api ${project.version} + \ No newline at end of file diff --git a/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiController.java b/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiController.java index bcce7535c7..9401519da6 100644 --- a/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiController.java +++ b/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiController.java @@ -9,17 +9,6 @@ */ package org.eclipse.hawkbit.sdk.device; -import java.time.LocalTime; -import java.time.temporal.ChronoField; -import java.util.AbstractMap; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @@ -39,6 +28,17 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import java.time.LocalTime; +import java.time.temporal.ChronoField; +import java.util.AbstractMap; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + /** * Class representing DDI device connecting directly to hawkBit. */ @@ -84,7 +84,7 @@ public class DdiController { * for communication to hawkBit */ public DdiController(final Tenant tenant, final Controller controller, - final UpdateHandler updateHandler, final HawkbitClient hawkbitClient) { + final UpdateHandler updateHandler, final HawkbitClient hawkbitClient) { this.tenantId = tenant.getTenantId(); gatewayToken = tenant.getGatewayToken(); downloadAuthenticationEnabled = tenant.isDownloadAuthenticationEnabled(); @@ -96,13 +96,17 @@ public DdiController(final Tenant tenant, final Controller controller, // expects single threaded {@link java.util.concurrent.ScheduledExecutorService} public void start(final ScheduledExecutorService executorService) { - Objects.requireNonNull(executorService, "Require non null executor!"); + stop(); + Objects.requireNonNull(executorService, "Require non null executor!"); this.executorService = executorService; executorService.submit(this::poll); } public void stop() { + if (executorService != null) { + executorService.shutdown(); + } executorService = null; lastActionId = null; currentActionId = null; diff --git a/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiTenant.java b/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiTenant.java index 4df82b9898..1173960066 100644 --- a/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiTenant.java +++ b/hawkbit-sdk/hawkbit-sdk-device/src/main/java/org/eclipse/hawkbit/sdk/device/DdiTenant.java @@ -19,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap; /** - * An in-memory simulated DMF Tenant to hold the controller twins in + * An in-memory simulated DDI Tenant to hold the controller twins in * memory and be able to retrieve them again. */ public class DdiTenant { @@ -27,8 +27,9 @@ public class DdiTenant { @Getter private final Tenant tenant; - private final Map controllers = new ConcurrentHashMap<>(); + @Getter private final HawkbitClient hawkbitClient; + private final Map controllers = new ConcurrentHashMap<>(); public DdiTenant(final Tenant tenant, final HawkbitClient hawkbitClient) { this.tenant = tenant; @@ -40,17 +41,14 @@ public void destroy() { controllers.clear(); } - public DdiController create(final Controller controller, final UpdateHandler updateHandler) { + public DdiController createController(final Controller controller, final UpdateHandler updateHandler) { final DdiController ddiController = new DdiController(tenant, controller, updateHandler, hawkbitClient); controllers.put(controller.getControllerId(), ddiController); return ddiController; } - public void remove(final String controllerId) { - Optional.ofNullable(controllers.remove(controllerId)).ifPresent(DdiController::stop); - } - public Optional getController(final String controllerId) { return Optional.ofNullable(controllers.get(controllerId)); } + } \ No newline at end of file diff --git a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfController.java b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfController.java index 92bce6082f..bef946071d 100644 --- a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfController.java +++ b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfController.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; /** * Class representing DMF device twin connecting to hawkBit via DMF. @@ -50,6 +51,9 @@ public class DmfController { private volatile Long lastActionId; + @SuppressWarnings("java:S3077") // volatile used only for the reference as expected + private volatile ScheduledExecutorService executorService; + /** * Creates a new device instance. * @@ -67,26 +71,32 @@ public class DmfController { this.dmfSender = dmfSender; } - public void connect() { + public void start(ScheduledExecutorService executorService) { + stop(); + this.executorService = executorService; log.debug(LOG_PREFIX + "Connecting/Polling ...", getTenantId(), getControllerId()); dmfSender.createOrUpdateThing(getTenantId(), getControllerId()); log.debug(LOG_PREFIX + "Done. Create thing sent.", getTenantId(), getControllerId()); } - public void poll() { - log.debug(LOG_PREFIX + "Polling ..", getTenantId(), getControllerId()); - dmfSender.createOrUpdateThing(getTenantId(), getControllerId()); + public void unregisterThing() { + log.debug(LOG_PREFIX + "Removing Controller...", getTenantId(), getControllerId()); + dmfSender.removeThing(getTenantId(), getControllerId()); log.debug(LOG_PREFIX + "Done. Create thing sent.", getTenantId(), getControllerId()); } public void stop() { + if (executorService != null) { + executorService.shutdown(); + } + executorService = null; lastActionId = null; currentActionId = null; } public void processUpdate(final EventTopic actionType, final DmfDownloadAndUpdateRequest updateRequest) { log.info(LOG_PREFIX + "Processing update for action {} .", getTenantId(), controllerId, updateRequest.getActionId()); - updateHandler.getUpdateProcessor(this, actionType, updateRequest).run(); + executorService.submit(updateHandler.getUpdateProcessor(this, actionType, updateRequest)); } public void sendFeedback(final UpdateStatus updateStatus) { diff --git a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfTenant.java b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfTenant.java index 3368ba7885..9ca753d31f 100644 --- a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfTenant.java +++ b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/DmfTenant.java @@ -46,17 +46,21 @@ public DmfTenant(final Tenant tenant, final Amqp amqp, final boolean initVHost) } public void destroy() { - controllers.values().forEach(DmfController::stop); + controllers.values().forEach(DmfController::unregisterThing); controllers.clear(); } - public DmfController create(final Controller controller, final UpdateHandler updateHandler) { + public DmfController createController(final Controller controller, final UpdateHandler updateHandler) { final DmfController dmfController = new DmfController(tenant, controller, updateHandler, vHost); controllers.put(controller.getControllerId(), dmfController); return dmfController; } - public void remove(final String controllerId) { + public void removeController(final String controllerId) { + Optional.ofNullable(controllers.remove(controllerId)).ifPresent(DmfController::unregisterThing); + } + + public void handleThingDeleted(final String controllerId) { Optional.ofNullable(controllers.remove(controllerId)).ifPresent(DmfController::stop); } diff --git a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/DmfSender.java b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/DmfSender.java index 4e4ff3bf0e..88e0a00cfd 100644 --- a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/DmfSender.java +++ b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/DmfSender.java @@ -51,8 +51,16 @@ public DmfSender(final RabbitTemplate rabbitTemplate, final AmqpProperties amqpP } public void createOrUpdateThing(final String tenant, final String controllerId) { + sendThingMessage(tenant, controllerId, MessageType.THING_CREATED.name()); + } + + public void removeThing(final String tenant, final String controllerId) { + sendThingMessage(tenant, controllerId, MessageType.THING_REMOVED.name()); + } + + public void sendThingMessage(final String tenant, final String controllerId, String thingStatusChange) { final MessageProperties messagePropertiesForSP = new MessageProperties(); - messagePropertiesForSP.setHeader(MessageHeaderKey.TYPE, MessageType.THING_CREATED.name()); + messagePropertiesForSP.setHeader(MessageHeaderKey.TYPE, thingStatusChange); messagePropertiesForSP.setHeader(MessageHeaderKey.TENANT, tenant); messagePropertiesForSP.setHeader(MessageHeaderKey.THING_ID, controllerId); messagePropertiesForSP.setHeader(MessageHeaderKey.SENDER, "hawkBit-sdk"); diff --git a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/VHost.java b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/VHost.java index 8be269338e..d92f6742ad 100644 --- a/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/VHost.java +++ b/hawkbit-sdk/hawkbit-sdk-dmf/src/main/java/org/eclipse/hawkbit/sdk/dmf/amqp/VHost.java @@ -103,7 +103,7 @@ public void onMessage(final Message message) { final String controllerId = (String)message.getMessageProperties().getHeaders().get(MessageHeaderKey.THING_ID); final String type = (String)message.getMessageProperties().getHeaders().get(MessageHeaderKey.TYPE); - log.info("Message received for target {}, value : {}", controllerId, message.toString()); + log.info("Message received for target {}, value : {}", controllerId, message); switch (MessageType.valueOf(type)) { case EVENT: { checkContentTypeJson(message); @@ -112,7 +112,7 @@ public void onMessage(final Message message) { } case THING_DELETED: { checkContentTypeJson(message); - Optional.ofNullable(dmfTenants.get(tenantId)).ifPresent(dmfTenant -> dmfTenant.remove(controllerId)); + Optional.ofNullable(dmfTenants.get(tenantId)).ifPresent(dmfTenant -> dmfTenant.handleThingDeleted(controllerId)); break; } case PING_RESPONSE: { @@ -237,7 +237,7 @@ protected void handleUpdateProcess(final Message message, final String controlle final String tenant = getTenant(message); final DmfDownloadAndUpdateRequest downloadAndUpdateRequest = convertMessage(message, DmfDownloadAndUpdateRequest.class); - dmfTenants.get(tenant).getController(controllerId).get().setCurrentActionId(downloadAndUpdateRequest.getActionId()); + dmfTenants.get(tenant).getController(controllerId).ifPresent(controller -> controller.setCurrentActionId(downloadAndUpdateRequest.getActionId())); processUpdate(tenant, controllerId, actionType, downloadAndUpdateRequest); } diff --git a/hawkbit-sdk/hawkbit-sdk-mgmt/pom.xml b/hawkbit-sdk/hawkbit-sdk-mgmt/pom.xml new file mode 100644 index 0000000000..15782df135 --- /dev/null +++ b/hawkbit-sdk/hawkbit-sdk-mgmt/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + org.eclipse.hawkbit + hawkbit-sdk + ${revision} + + + hawkbit-sdk-mgmt + + hawkBit :: SDK :: Management Api SDK + Management Api SDK + + + + org.eclipse.hawkbit + hawkbit-sdk-commons + ${project.version} + + + org.eclipse.hawkbit + hawkbit-mgmt-api + ${project.version} + + + org.springframework.cloud + spring-cloud-starter-openfeign + ${spring-cloud-starter-openfeign.version} + + + io.github.openfeign + feign-hc5 + ${openfeign-hc5.version} + + + + \ No newline at end of file diff --git a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/SetupHelper.java b/hawkbit-sdk/hawkbit-sdk-mgmt/src/main/java/org/eclipse/hawkbit/sdk/mgmt/MgmtApi.java similarity index 90% rename from hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/SetupHelper.java rename to hawkbit-sdk/hawkbit-sdk-mgmt/src/main/java/org/eclipse/hawkbit/sdk/mgmt/MgmtApi.java index 21bf484e97..c6bc8e8c80 100644 --- a/hawkbit-sdk/hawkbit-sdk-demo/src/main/java/org/eclipse/hawkbit/sdk/demo/SetupHelper.java +++ b/hawkbit-sdk/hawkbit-sdk-mgmt/src/main/java/org/eclipse/hawkbit/sdk/mgmt/MgmtApi.java @@ -7,11 +7,12 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.eclipse.hawkbit.sdk.demo; +package org.eclipse.hawkbit.sdk.mgmt; + import feign.FeignException; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; +import lombok.AllArgsConstructor; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.eclipse.hawkbit.mgmt.json.model.target.MgmtTarget; import org.eclipse.hawkbit.mgmt.json.model.target.MgmtTargetRequestBody; @@ -29,21 +30,28 @@ import java.util.Random; /** - * Abstract class representing DDI device connecting directly to hawkVit. + * Management Api Interface */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) @Slf4j -public class SetupHelper { +@AllArgsConstructor +public class MgmtApi { private static final String AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY = "authentication.gatewaytoken.key"; private static final String AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED = "authentication.gatewaytoken.enabled"; private static final String AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED = "authentication.targettoken.enabled"; + + @NonNull + private final Tenant tenant; + @NonNull + private final HawkbitClient hawkbitClient; + + // if gateway toke is configured then the gateway auth is enabled key is set // so all devices use gateway token authentication // otherwise target token authentication is enabled. Then all devices shall be registerd // and the target token shall be set to the one from the DDI controller instance - public static void setupTargetAuthentication(final HawkbitClient hawkbitClient, final Tenant tenant) { + public void setupTargetAuthentication() { final MgmtTenantManagementRestApi mgmtTenantManagementRestApi = hawkbitClient.mgmtService(MgmtTenantManagementRestApi.class, tenant); final String gatewayToken = tenant.getGatewayToken(); @@ -75,9 +83,7 @@ public static void setupTargetAuthentication(final HawkbitClient hawkbitClient, } // returns target token - public static String setupTargetToken( - final String controllerId, String securityTargetToken, - final HawkbitClient hawkbitClient, final Tenant tenant) { + public String setupTargetToken(final String controllerId, String securityTargetToken) { if (ObjectUtils.isEmpty(tenant.getGatewayToken())) { final MgmtTargetRestApi mgmtTargetRestApi = hawkbitClient.mgmtService(MgmtTargetRestApi.class, tenant); try { @@ -112,6 +118,10 @@ public static String setupTargetToken( return securityTargetToken; } + public void deleteController(final String controllerId) { + hawkbitClient.mgmtService(MgmtTargetRestApi.class, tenant).deleteTarget(controllerId); + } + private static final Random RND = new SecureRandom(); public static String randomToken() { final byte[] rnd = new byte[24]; diff --git a/hawkbit-sdk/pom.xml b/hawkbit-sdk/pom.xml index bd7be7b2ef..bd994b5696 100644 --- a/hawkbit-sdk/pom.xml +++ b/hawkbit-sdk/pom.xml @@ -22,8 +22,14 @@ hawkbit-sdk pom + + 4.1.0 + 13.2.1 + + hawkbit-sdk-commons + hawkbit-sdk-mgmt hawkbit-sdk-device hawkbit-sdk-dmf hawkbit-sdk-demo