SDK DMF Support - added demo (#1710)
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -35,6 +35,11 @@
|
|||||||
<artifactId>hawkbit-sdk-device</artifactId>
|
<artifactId>hawkbit-sdk-device</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.hawkbit</groupId>
|
||||||
|
<artifactId>hawkbit-sdk-dmf</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.hawkbit</groupId>
|
<groupId>org.eclipse.hawkbit</groupId>
|
||||||
<artifactId>hawkbit-mgmt-api</artifactId>
|
<artifactId>hawkbit-mgmt-api</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2024 Bosch.IO GmbH and others
|
||||||
|
*
|
||||||
|
* This program and the accompanying materials are made
|
||||||
|
* available under the terms of the Eclipse Public License 2.0
|
||||||
|
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: EPL-2.0
|
||||||
|
*/
|
||||||
|
package org.eclipse.hawkbit.sdk.demo.dmf;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.eclipse.hawkbit.sdk.Controller;
|
||||||
|
import org.eclipse.hawkbit.sdk.Tenant;
|
||||||
|
import org.eclipse.hawkbit.sdk.dmf.DmfController;
|
||||||
|
import org.eclipse.hawkbit.sdk.dmf.DmfTenant;
|
||||||
|
import org.eclipse.hawkbit.sdk.dmf.UpdateHandler;
|
||||||
|
import org.eclipse.hawkbit.sdk.dmf.amqp.Amqp;
|
||||||
|
import org.eclipse.hawkbit.sdk.dmf.amqp.AmqpProperties;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.shell.standard.ShellComponent;
|
||||||
|
import org.springframework.shell.standard.ShellMethod;
|
||||||
|
import org.springframework.shell.standard.ShellOption;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract class representing DDI device connecting directly to hawkVit.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@EnableConfigurationProperties({ RabbitProperties.class, AmqpProperties.class })
|
||||||
|
@SpringBootApplication
|
||||||
|
public class DmfApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DmfApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Amqp amqp(final RabbitProperties rabbitProperties, final AmqpProperties amqpProperties) {
|
||||||
|
return new Amqp(rabbitProperties, amqpProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ShellComponent
|
||||||
|
public static class Shell {
|
||||||
|
|
||||||
|
private final UpdateHandler updateHandler;
|
||||||
|
private final DmfTenant dmfTenant;
|
||||||
|
|
||||||
|
Shell(final Tenant tenant, final Optional<UpdateHandler> updateHandler, final Amqp amqp) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ShellMethod(key = "start")
|
||||||
|
public void start(
|
||||||
|
@ShellOption(value = "--prefix", defaultValue = "") final String prefix,
|
||||||
|
@ShellOption(value = "--offset", defaultValue = "0") final int offset,
|
||||||
|
@ShellOption(value = "--count") final int count) {
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
startOne(toId(prefix, offset + i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ShellMethod(key = "stop")
|
||||||
|
public void stop(
|
||||||
|
@ShellOption(value = "--prefix", defaultValue = "") final String prefix,
|
||||||
|
@ShellOption(value = "--offset", defaultValue = "0") final int offset,
|
||||||
|
@ShellOption(value = "--count") final int count) {
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
stopOne(toId(prefix, offset + i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toId(final String prefix, final int index) {
|
||||||
|
return String.format("%s%03d", prefix, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.sdk.dmf.amqp;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.hawkbit.sdk.Tenant;
|
import org.eclipse.hawkbit.sdk.Tenant;
|
||||||
|
import org.eclipse.hawkbit.sdk.Tenant.DMF;
|
||||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
||||||
@@ -37,16 +38,17 @@ public class Amqp {
|
|||||||
vHosts.values().forEach(VHost::stop);
|
vHosts.values().forEach(VHost::stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
public VHost getVhost(final Tenant.DMF dmf) {
|
public VHost getVhost(final DMF dmf) {
|
||||||
final String vHost = ObjectUtils.isEmpty(dmf.getVirtualHost()) ?
|
final String vHost = dmf == null || ObjectUtils.isEmpty(dmf.getVirtualHost()) ?
|
||||||
rabbitProperties.getVirtualHost() : dmf.getVirtualHost();
|
(rabbitProperties.getVirtualHost() == null ? "/" :rabbitProperties.getVirtualHost()) :
|
||||||
|
dmf.getVirtualHost();
|
||||||
return vHosts.computeIfAbsent(vHost, vh -> {
|
return vHosts.computeIfAbsent(vHost, vh -> {
|
||||||
final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitProperties.getHost());
|
final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitProperties.getHost());
|
||||||
connectionFactory.setUsername(
|
connectionFactory.setUsername(
|
||||||
ObjectUtils.isEmpty(dmf.getUsername()) ?
|
dmf == null || ObjectUtils.isEmpty(dmf.getUsername()) ?
|
||||||
rabbitProperties.getUsername() : dmf.getUsername());
|
rabbitProperties.getUsername() : dmf.getUsername());
|
||||||
connectionFactory.setPassword(
|
connectionFactory.setPassword(
|
||||||
ObjectUtils.isEmpty(dmf.getPassword()) ?
|
dmf == null || ObjectUtils.isEmpty(dmf.getPassword()) ?
|
||||||
rabbitProperties.getPassword() : dmf.getPassword());
|
rabbitProperties.getPassword() : dmf.getPassword());
|
||||||
connectionFactory.setVirtualHost(vHost);
|
connectionFactory.setVirtualHost(vHost);
|
||||||
return new VHost(connectionFactory, amqpProperties);
|
return new VHost(connectionFactory, amqpProperties);
|
||||||
|
|||||||
Reference in New Issue
Block a user