SDK: Add Update & Artifact handler (#1640)
Extension points that could allow user to plug-in the update exection and simulate some behaviours, uncluding implement real updates Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -19,13 +19,13 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtTenantManagementRestApi;
|
||||
import org.eclipse.hawkbit.sdk.HawkbitClient;
|
||||
import org.eclipse.hawkbit.sdk.Tenant;
|
||||
import org.eclipse.hawkbit.sdk.device.DdiController;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
@@ -51,25 +51,25 @@ public class SetupHelper {
|
||||
final MgmtTenantManagementRestApi mgmtTenantManagementRestApi =
|
||||
hawkbitClient.mgmtService(MgmtTenantManagementRestApi.class, tenant);
|
||||
if (ObjectUtils.isEmpty(tenant.getGatewayToken())) {
|
||||
if (!((Boolean)mgmtTenantManagementRestApi
|
||||
if (!((Boolean) Objects.requireNonNull(mgmtTenantManagementRestApi
|
||||
.getTenantConfigurationValue(AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED)
|
||||
.getBody().getValue())) {
|
||||
.getBody()).getValue())) {
|
||||
mgmtTenantManagementRestApi.updateTenantConfiguration(
|
||||
Map.of(AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED, true)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!((Boolean)mgmtTenantManagementRestApi
|
||||
if (!((Boolean) Objects.requireNonNull(mgmtTenantManagementRestApi
|
||||
.getTenantConfigurationValue(AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED)
|
||||
.getBody().getValue())) {
|
||||
.getBody()).getValue())) {
|
||||
mgmtTenantManagementRestApi.updateTenantConfiguration(
|
||||
Map.of(AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED, true)
|
||||
);
|
||||
}
|
||||
if (!tenant.getGatewayToken().equals(
|
||||
mgmtTenantManagementRestApi
|
||||
Objects.requireNonNull(mgmtTenantManagementRestApi
|
||||
.getTenantConfigurationValue(AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY)
|
||||
.getBody().getValue())) {
|
||||
.getBody()).getValue())) {
|
||||
mgmtTenantManagementRestApi.updateTenantConfiguration(
|
||||
Map.of(AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, tenant.getGatewayToken())
|
||||
);
|
||||
@@ -85,7 +85,7 @@ public class SetupHelper {
|
||||
final MgmtTargetRestApi mgmtTargetRestApi = hawkbitClient.mgmtService(MgmtTargetRestApi.class, tenant);
|
||||
try {
|
||||
// test if target exist, if not - throws 404
|
||||
final MgmtTarget target = mgmtTargetRestApi.getTarget(controllerId).getBody();
|
||||
final MgmtTarget target = Objects.requireNonNull(mgmtTargetRestApi.getTarget(controllerId).getBody());
|
||||
if (ObjectUtils.isEmpty(securityTargetToken)) {
|
||||
if (ObjectUtils.isEmpty(target.getSecurityToken())) {
|
||||
// generate random to set to tha existing target without configured security token
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.eclipse.hawkbit.sdk.HawkbitSDKConfigurtion;
|
||||
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.UpdateHandler;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -30,6 +31,7 @@ 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;
|
||||
|
||||
@@ -56,7 +58,9 @@ public class DeviceApp {
|
||||
DdiController device(
|
||||
@Value("${hawkbit.device:controller-default}") final String controllerId,
|
||||
@Value("${hawkbit.device.securityToken:}") final String securityToken,
|
||||
final Tenant defaultTenant, final HawkbitClient hawkbitClient) {
|
||||
final Tenant defaultTenant,
|
||||
final Optional<UpdateHandler> updateHandler,
|
||||
final HawkbitClient hawkbitClient) {
|
||||
return new DdiController(
|
||||
defaultTenant,
|
||||
Controller.builder()
|
||||
@@ -65,6 +69,7 @@ public class DeviceApp {
|
||||
(ObjectUtils.isEmpty(defaultTenant.getGatewayToken()) ? SetupHelper.randomToken() : securityToken) :
|
||||
securityToken)
|
||||
.build(),
|
||||
updateHandler.orElse(null),
|
||||
hawkbitClient).setOverridePollMillis(10_000);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.eclipse.hawkbit.sdk.HawkbitSDKConfigurtion;
|
||||
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.UpdateHandler;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -30,6 +31,7 @@ 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;
|
||||
@@ -57,6 +59,7 @@ public class MultiDeviceApp {
|
||||
public static class Shell {
|
||||
|
||||
private final Tenant tenant;
|
||||
private final UpdateHandler updateHandler;
|
||||
private final HawkbitClient hawkbitClient;
|
||||
private final Map<String, DdiController> devices = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -64,8 +67,9 @@ public class MultiDeviceApp {
|
||||
|
||||
private boolean setup;
|
||||
|
||||
Shell(final Tenant tenant, final HawkbitClient hawkbitClient) {
|
||||
Shell(final Tenant tenant, final Optional<UpdateHandler> updateHandler, final HawkbitClient hawkbitClient) {
|
||||
this.tenant = tenant;
|
||||
this.updateHandler = updateHandler.orElse(null);
|
||||
this.hawkbitClient = hawkbitClient;
|
||||
}
|
||||
|
||||
@@ -86,11 +90,13 @@ public class MultiDeviceApp {
|
||||
securityTargetToken = null;
|
||||
}
|
||||
if (device == null) {
|
||||
device = new DdiController(tenant,
|
||||
device = new DdiController(
|
||||
tenant,
|
||||
Controller.builder()
|
||||
.controllerId(controllerId)
|
||||
.securityToken(securityTargetToken)
|
||||
.build(),
|
||||
updateHandler,
|
||||
hawkbitClient).setOverridePollMillis(10_000);
|
||||
final DdiController oldDevice = devices.putIfAbsent(controllerId, device);
|
||||
if (oldDevice != null) {
|
||||
|
||||
Reference in New Issue
Block a user