Skip to content
Snippets Groups Projects
Commit 17bd4519 authored by fsoldatos's avatar fsoldatos
Browse files

feat: streams implementation for automation service

parent 147fe0f4
No related branches found
No related tags found
1 merge request!54Release 2.0.0
Showing
with 1420 additions and 209 deletions
...@@ -10,7 +10,7 @@ REGISTRY_IMAGE="" ...@@ -10,7 +10,7 @@ REGISTRY_IMAGE=""
#REGISTRY_IMAGE="http://my-container-registry.local/" #REGISTRY_IMAGE="http://my-container-registry.local/"
# Set the list of components you want to build images for, and deploy. # Set the list of components you want to build images for, and deploy.
COMPONENTS="context device service compute monitoring centralizedattackdetector" COMPONENTS="context device automation service compute monitoring centralizedattackdetector"
# Set the tag you want to use for your images. # Set the tag you want to use for your images.
IMAGE_TAG="tf-dev" IMAGE_TAG="tf-dev"
...@@ -18,7 +18,6 @@ IMAGE_TAG="tf-dev" ...@@ -18,7 +18,6 @@ IMAGE_TAG="tf-dev"
# Set the name of the Kubernetes namespace to deploy to. # Set the name of the Kubernetes namespace to deploy to.
K8S_NAMESPACE="tf-dev" K8S_NAMESPACE="tf-dev"
######################################################################################################################## ########################################################################################################################
# Automated steps start here # Automated steps start here
######################################################################################################################## ########################################################################################################################
...@@ -34,10 +33,10 @@ mkdir -p $TMP_MANIFESTS_FOLDER ...@@ -34,10 +33,10 @@ mkdir -p $TMP_MANIFESTS_FOLDER
TMP_LOGS_FOLDER="$TMP_FOLDER/logs" TMP_LOGS_FOLDER="$TMP_FOLDER/logs"
mkdir -p $TMP_LOGS_FOLDER mkdir -p $TMP_LOGS_FOLDER
# Re-create the namespace to prevent being affected by garbage on it # Re-create the namespace to prevent being affected by garbage on it
kubectl delete namespace $K8S_NAMESPACE kubectl delete namespace $K8S_NAMESPACE
kubectl create namespace $K8S_NAMESPACE kubectl create namespace $K8S_NAMESPACE
printf "\n"
for COMPONENT in $COMPONENTS; do for COMPONENT in $COMPONENTS; do
echo "Processing '$COMPONENT' component..." echo "Processing '$COMPONENT' component..."
...@@ -46,34 +45,44 @@ for COMPONENT in $COMPONENTS; do ...@@ -46,34 +45,44 @@ for COMPONENT in $COMPONENTS; do
echo " Building Docker image..." echo " Building Docker image..."
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}.log" BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}.log"
docker build -t "$IMAGE_NAME" -f ./src/$COMPONENT/Dockerfile ./src/ > $BUILD_LOG
if [ "$COMPONENT" == "automation" ]; then
docker build -t "$IMAGE_NAME" -f ./src/"$COMPONENT"/Dockerfile ./src/"$COMPONENT"/ > "$BUILD_LOG"
else
docker build -t "$IMAGE_NAME" -f ./src/"$COMPONENT"/Dockerfile ./src/ > "$BUILD_LOG"
fi
if [ -n "$REGISTRY_IMAGE" ]; then if [ -n "$REGISTRY_IMAGE" ]; then
echo "Pushing Docker image to '$REGISTRY_IMAGE'..." echo "Pushing Docker image to '$REGISTRY_IMAGE'..."
TAG_LOG="$TMP_LOGS_FOLDER/tag_${COMPONENT}.log" TAG_LOG="$TMP_LOGS_FOLDER/tag_${COMPONENT}.log"
docker tag "$IMAGE_NAME" "$IMAGE_URL" > $TAG_LOG docker tag "$IMAGE_NAME" "$IMAGE_URL" > "$TAG_LOG"
PUSH_LOG="$TMP_LOGS_FOLDER/push_${COMPONENT}.log" PUSH_LOG="$TMP_LOGS_FOLDER/push_${COMPONENT}.log"
docker push "$IMAGE_URL" > "$PUSH_LOG" docker push "$IMAGE_URL" > "$PUSH_LOG"
fi fi
echo " Adapting manifest file..." echo " Adapting '$COMPONENT' manifest file..."
MANIFEST="$TMP_MANIFESTS_FOLDER/${COMPONENT}service.yaml" MANIFEST="$TMP_MANIFESTS_FOLDER/${COMPONENT}service.yaml"
cp ./manifests/${COMPONENT}service.yaml $MANIFEST cp ./manifests/"${COMPONENT}"service.yaml "$MANIFEST"
VERSION=$(grep -i "${GITLAB_REPO_URL}/${COMPONENT}:" "$MANIFEST" | cut -d ":" -f3)
if [ -n "$REGISTRY_IMAGE" ]; then if [ -n "$REGISTRY_IMAGE" ]; then
sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:latest#image: $IMAGE_URL#g" $MANIFEST
sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Always#g" $MANIFEST sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:${VERSION}#image: $IMAGE_URL#g" "$MANIFEST"
sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Always#g" "$MANIFEST"
else else
sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:latest#image: $IMAGE_NAME#g" $MANIFEST sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:${VERSION}#image: $IMAGE_NAME#g" "$MANIFEST"
sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Never#g" $MANIFEST sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Never#g" "$MANIFEST"
fi fi
echo " Deploying to Kubernetes..." echo " Deploying '$COMPONENT' component to Kubernetes..."
DEPLOY_LOG="$TMP_LOGS_FOLDER/push_${COMPONENT}.log" DEPLOY_LOG="$TMP_LOGS_FOLDER/push_${COMPONENT}.log"
kubectl --namespace $K8S_NAMESPACE apply -f $MANIFEST > $DEPLOY_LOG kubectl --namespace $K8S_NAMESPACE apply -f "$MANIFEST" > "$DEPLOY_LOG"
printf "\n"
done done
kubectl --namespace $K8S_NAMESPACE get all kubectl --namespace $K8S_NAMESPACE get all
echo "Done!" echo "Done!"
\ No newline at end of file
...@@ -10,7 +10,7 @@ service AutomationService { ...@@ -10,7 +10,7 @@ service AutomationService {
rpc ZtpAdd(DeviceRole) returns (DeviceRoleState) {} rpc ZtpAdd(DeviceRole) returns (DeviceRoleState) {}
rpc ZtpUpdate(DeviceRole) returns (DeviceRoleState) {} rpc ZtpUpdate(DeviceRole) returns (DeviceRoleState) {}
rpc ZtpDelete(DeviceRole) returns (DeviceRoleState) {} rpc ZtpDelete(DeviceRole) returns (DeviceRoleState) {}
rpc ZtpDeleteAllByDeviceId(context.DeviceId) returns (DeviceRoleState) {} rpc ZtpDeleteAll(Empty) returns (DeviceDeletionResult) {}
} }
enum DeviceRoleType { enum DeviceRoleType {
...@@ -39,12 +39,17 @@ message DeviceRoleState { ...@@ -39,12 +39,17 @@ message DeviceRoleState {
ZtpDeviceState devRoleState = 2; ZtpDeviceState devRoleState = 2;
} }
message DeviceDeletionResult {
repeated bool deleted = 1;
}
message Empty {}
enum ZtpDeviceState { enum ZtpDeviceState {
PLANNED = 0; ZTP_DEV_STATE_UNDEFINED = 0;
POTENCIAL_AVAILABLE = 1; ZTP_DEV_STATE_CREATED = 1;
POTENCIAL_BUSY = 2; ZTP_DEV_STATE_UPDATED = 2;
INSTALLED = 3; ZTP_DEV_STATE_DELETED = 3;
PENDING_REMOVAL = 4;
} }
src/main/docker/Dockerfile.multistage.jvm
\ No newline at end of file
package eu.teraflow.automation;
import io.smallrye.config.ConfigMapping;
@ConfigMapping(prefix = "automation")
public interface AutomationConfiguration {
boolean shouldSubscribeToContextComponent();
}
...@@ -4,9 +4,6 @@ import automation.Automation; ...@@ -4,9 +4,6 @@ import automation.Automation;
import context.ContextOuterClass; import context.ContextOuterClass;
import eu.teraflow.automation.device.model.DeviceId; import eu.teraflow.automation.device.model.DeviceId;
import eu.teraflow.automation.device.model.Uuid; import eu.teraflow.automation.device.model.Uuid;
import eu.teraflow.automation.model.DeviceRole;
import eu.teraflow.automation.model.DeviceRoleId;
import eu.teraflow.automation.model.DeviceRoleType;
import io.quarkus.grpc.GrpcService; import io.quarkus.grpc.GrpcService;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -36,7 +33,7 @@ public class AutomationGatewayImpl implements AutomationGateway { ...@@ -36,7 +33,7 @@ public class AutomationGatewayImpl implements AutomationGateway {
@Override @Override
public Uni<Automation.DeviceRoleState> ztpAdd(Automation.DeviceRole request) { public Uni<Automation.DeviceRoleState> ztpAdd(Automation.DeviceRole request) {
automationService.ztpAdd(getDeviceRole(request)); automationService.addDevice(getDeviceId(request.getDevRoleId().getDevId()));
return Uni.createFrom() return Uni.createFrom()
.item( .item(
...@@ -67,29 +64,14 @@ public class AutomationGatewayImpl implements AutomationGateway { ...@@ -67,29 +64,14 @@ public class AutomationGatewayImpl implements AutomationGateway {
} }
@Override @Override
public Uni<Automation.DeviceRoleState> ztpDeleteAllByDeviceId( public Uni<Automation.DeviceDeletionResult> ztpDeleteAll(Automation.Empty empty) {
ContextOuterClass.DeviceId request) { return Uni.createFrom().item(() -> Automation.DeviceDeletionResult.newBuilder().build());
return Uni.createFrom().item(() -> Automation.DeviceRoleState.newBuilder().build());
} }
private DeviceRole getDeviceRole(Automation.DeviceRole deviceRole) { private DeviceId getDeviceId(ContextOuterClass.DeviceId serializedDeviceId) {
final var OutDeviceId = deviceRole.getDevRoleId().getDevId().getDeviceUuid();
Uuid uuid = new Uuid(OutDeviceId.getUuid());
DeviceId deviceId = new DeviceId(uuid);
DeviceRoleId deviceRoleId = new DeviceRoleId(uuid, deviceId);
final var deviceRoleType = deviceRole.getDevRoleType(); Uuid uuid = new Uuid(serializedDeviceId.getDeviceUuid().getUuid());
if (deviceRoleType == Automation.DeviceRoleType.DEV_OPS) { return new DeviceId(uuid);
return new DeviceRole(deviceRoleId, DeviceRoleType.DEV_OPS);
} else if (deviceRoleType == Automation.DeviceRoleType.DEV_CONF) {
return new DeviceRole(deviceRoleId, DeviceRoleType.DEV_CONF);
} else if (deviceRoleType == Automation.DeviceRoleType.NONE) {
return new DeviceRole(deviceRoleId, DeviceRoleType.NONE);
} else {
return new DeviceRole(deviceRoleId, DeviceRoleType.PIPELINE_CONF);
}
} }
} }
package eu.teraflow.automation; package eu.teraflow.automation;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.model.DeviceRole; import eu.teraflow.automation.device.model.DeviceId;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
public interface AutomationService { public interface AutomationService {
Uni<Device> ztpAdd(DeviceRole deviceRole); Uni<Device> addDevice(DeviceId deviceId);
} }
...@@ -3,8 +3,7 @@ package eu.teraflow.automation; ...@@ -3,8 +3,7 @@ package eu.teraflow.automation;
import eu.teraflow.automation.context.ContextService; import eu.teraflow.automation.context.ContextService;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.DeviceService; import eu.teraflow.automation.device.DeviceService;
import eu.teraflow.automation.device.model.DeviceOperationalStatus; import eu.teraflow.automation.device.model.*;
import eu.teraflow.automation.model.DeviceRole;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -24,9 +23,9 @@ public class AutomationServiceImpl implements AutomationService { ...@@ -24,9 +23,9 @@ public class AutomationServiceImpl implements AutomationService {
} }
@Override @Override
public Uni<Device> ztpAdd(DeviceRole deviceRole) { public Uni<Device> addDevice(DeviceId deviceId) {
final var deviceUni = contextService.getDevice(deviceRole.getDeviceRoleId().getDeviceId()); final var deviceUni = contextService.getDevice(deviceId);
deviceUni deviceUni
.subscribe() .subscribe()
...@@ -37,9 +36,7 @@ public class AutomationServiceImpl implements AutomationService { ...@@ -37,9 +36,7 @@ public class AutomationServiceImpl implements AutomationService {
.toString() .toString()
.equals(DeviceOperationalStatus.ENABLED.toString()))) { .equals(DeviceOperationalStatus.ENABLED.toString()))) {
final var initialConfig = final var initialConfig = deviceService.getInitialConfiguration(deviceId);
deviceService.getInitialConfiguration(
deviceRole.getDeviceRoleId().getDeviceId());
device.setDeviceOperationalStatus(DeviceOperationalStatus.ENABLED); device.setDeviceOperationalStatus(DeviceOperationalStatus.ENABLED);
...@@ -49,7 +46,6 @@ public class AutomationServiceImpl implements AutomationService { ...@@ -49,7 +46,6 @@ public class AutomationServiceImpl implements AutomationService {
deviceConfig -> { deviceConfig -> {
device.setDeviceConfiguration(deviceConfig); device.setDeviceConfiguration(deviceConfig);
deviceService.configureDevice(device); deviceService.configureDevice(device);
LOGGER.infof("Received response %s", device); LOGGER.infof("Received response %s", device);
}); });
} }
......
package eu.teraflow.automation;
import eu.teraflow.automation.context.ContextService;
import eu.teraflow.automation.device.model.DeviceEvent;
import io.quarkus.runtime.StartupEvent;
import io.smallrye.mutiny.Multi;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.jboss.logging.Logger;
@ApplicationScoped
public class ContextSubscriber {
private static final Logger LOGGER = Logger.getLogger(ContextSubscriber.class);
private final ContextService contextService;
private final AutomationService automationService;
private final AutomationConfiguration automationConfiguration;
@Inject
public ContextSubscriber(
ContextService contextService,
AutomationService automationService,
AutomationConfiguration automationConfiguration) {
this.contextService = contextService;
this.automationService = automationService;
this.automationConfiguration = automationConfiguration;
}
public void listenForDeviceEvents() {
Multi<DeviceEvent> deviceEventsMulti = contextService.getDeviceEvents();
deviceEventsMulti
.onItem()
.transformToUniAndConcatenate(
deviceEvent -> automationService.addDevice(deviceEvent.getDeviceId()));
}
void onStart(@Observes StartupEvent ev) {
if (automationConfiguration.shouldSubscribeToContextComponent()) {
LOGGER.info("Listening for Device events...");
listenForDeviceEvents();
} else {
LOGGER.info("Not listening for Device events...");
}
}
}
package eu.teraflow.automation.context; package eu.teraflow.automation.context;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.model.DeviceEvent;
import eu.teraflow.automation.device.model.DeviceId; import eu.teraflow.automation.device.model.DeviceId;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
public interface ContextGateway { public interface ContextGateway {
Uni<Device> getDevice(DeviceId deviceId); Uni<Device> getDevice(DeviceId deviceId);
Multi<DeviceEvent> getDeviceEvents();
} }
...@@ -2,9 +2,12 @@ package eu.teraflow.automation.context; ...@@ -2,9 +2,12 @@ package eu.teraflow.automation.context;
import context.ContextOuterClass; import context.ContextOuterClass;
import context.ContextService; import context.ContextService;
import eu.teraflow.automation.context.model.Event;
import eu.teraflow.automation.context.model.EventTypeEnum;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.model.*; import eu.teraflow.automation.device.model.*;
import io.quarkus.grpc.GrpcClient; import io.quarkus.grpc.GrpcClient;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
...@@ -13,6 +16,15 @@ public class ContextGatewayImpl implements ContextGateway { ...@@ -13,6 +16,15 @@ public class ContextGatewayImpl implements ContextGateway {
@GrpcClient ContextService delegate; @GrpcClient ContextService delegate;
@Override
public Multi<DeviceEvent> getDeviceEvents() {
final var serializedEmpty = getSerializedEmpty();
final var deviceEventsMulti = delegate.getDeviceEvents(serializedEmpty);
return deviceEventsMulti.onItem().transform(this::getDeviceEvent);
}
@Override @Override
public Uni<Device> getDevice(DeviceId deviceId) { public Uni<Device> getDevice(DeviceId deviceId) {
final var serializedDeviceId = getSerializedDeviceId(deviceId); final var serializedDeviceId = getSerializedDeviceId(deviceId);
...@@ -21,6 +33,39 @@ public class ContextGatewayImpl implements ContextGateway { ...@@ -21,6 +33,39 @@ public class ContextGatewayImpl implements ContextGateway {
return serializedDeviceUni.onItem().transform(this::getDevice); return serializedDeviceUni.onItem().transform(this::getDevice);
} }
private ContextOuterClass.Empty getSerializedEmpty() {
return ContextOuterClass.Empty.newBuilder().build();
}
private DeviceEvent getDeviceEvent(ContextOuterClass.DeviceEvent deviceEvent) {
final var serializedDeviceId = deviceEvent.getDeviceId();
final var deviceId = getDeviceId(serializedDeviceId);
final var serializedEvent = deviceEvent.getEvent();
final var event = getEvent(serializedEvent);
return new DeviceEvent(event, deviceId);
}
private Event getEvent(ContextOuterClass.Event event) {
return new Event(event.getTimestamp(), getEventType(event.getEventType()));
}
private EventTypeEnum getEventType(ContextOuterClass.EventTypeEnum eventType) {
if (eventType == ContextOuterClass.EventTypeEnum.EVENTTYPE_CREATE) {
return EventTypeEnum.CREATE;
} else if (eventType == ContextOuterClass.EventTypeEnum.EVENTTYPE_REMOVE) {
return EventTypeEnum.REMOVE;
} else if (eventType == ContextOuterClass.EventTypeEnum.EVENTTYPE_UPDATE) {
return EventTypeEnum.UPDATE;
} else {
return EventTypeEnum.UNDEFINED;
}
}
private Device getDevice(ContextOuterClass.Device device) { private Device getDevice(ContextOuterClass.Device device) {
final var deviceId = getDeviceId(device.getDeviceId()); final var deviceId = getDeviceId(device.getDeviceId());
......
package eu.teraflow.automation.context; package eu.teraflow.automation.context;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.model.DeviceEvent;
import eu.teraflow.automation.device.model.DeviceId; import eu.teraflow.automation.device.model.DeviceId;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
public interface ContextService { public interface ContextService {
Uni<Device> getDevice(DeviceId deviceId); Uni<Device> getDevice(DeviceId deviceId);
Multi<DeviceEvent> getDeviceEvents();
} }
package eu.teraflow.automation.context; package eu.teraflow.automation.context;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.model.DeviceEvent;
import eu.teraflow.automation.device.model.DeviceId; import eu.teraflow.automation.device.model.DeviceId;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -21,4 +23,10 @@ public class ContextServiceImpl implements ContextService { ...@@ -21,4 +23,10 @@ public class ContextServiceImpl implements ContextService {
return contextGateway.getDevice(deviceId); return contextGateway.getDevice(deviceId);
} }
@Override
public Multi<DeviceEvent> getDeviceEvents() {
return contextGateway.getDeviceEvents();
}
} }
package eu.teraflow.automation.context.model;
public class Event {
private double timestamp;
private EventTypeEnum eventType;
public Event(double timestamp, EventTypeEnum eventType) {
this.timestamp = timestamp;
this.eventType = eventType;
}
public double getTimestamp() {
return timestamp;
}
public EventTypeEnum getEventTypeEnum() {
return eventType;
}
}
package eu.teraflow.automation.context.model;
public enum EventTypeEnum {
UNDEFINED,
CREATE,
UPDATE,
REMOVE
}
package eu.teraflow.automation.device.model;
import eu.teraflow.automation.context.model.Event;
public class DeviceEvent {
private Event event;
private DeviceId deviceId;
public DeviceEvent(Event event, DeviceId deviceId) {
this.event = event;
this.deviceId = deviceId;
}
public Event getEvent() {
return event;
}
public DeviceId getDeviceId() {
return deviceId;
}
}
package eu.teraflow.automation.model; package eu.teraflow.automation.model;
public enum DeviceState { public enum DeviceState {
PLANNED, UNDEFINED,
POTENCIAL_AVAILABLE, CREATED,
POTENCIAL_BUSY, UPDATED,
INSTALLED, DELETED
PENDING_REMOVAL
} }
automation:
should-subscribe-to-context-component: false
quarkus: quarkus:
grpc: grpc:
server: server:
......
...@@ -8,9 +8,6 @@ import eu.teraflow.automation.context.ContextGateway; ...@@ -8,9 +8,6 @@ import eu.teraflow.automation.context.ContextGateway;
import eu.teraflow.automation.device.Device; import eu.teraflow.automation.device.Device;
import eu.teraflow.automation.device.DeviceGateway; import eu.teraflow.automation.device.DeviceGateway;
import eu.teraflow.automation.device.model.*; import eu.teraflow.automation.device.model.*;
import eu.teraflow.automation.model.DeviceRole;
import eu.teraflow.automation.model.DeviceRoleId;
import eu.teraflow.automation.model.DeviceRoleType;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
...@@ -58,8 +55,8 @@ public class AutomationFunctionalServiceTest { ...@@ -58,8 +55,8 @@ public class AutomationFunctionalServiceTest {
DeviceId deviceId = new DeviceId(uuid); DeviceId deviceId = new DeviceId(uuid);
DeviceType deviceType = new DeviceType("cisco"); DeviceType deviceType = new DeviceType("cisco");
ConfigRule configRule1 = new ConfigRule(ConfigActionEnum.UNDEFINED, "001", "100"); ConfigRule configRule1 = new ConfigRule(ConfigActionEnum.UNDEFINED, "1", "1");
ConfigRule configRule2 = new ConfigRule(ConfigActionEnum.SET, "002", "101"); ConfigRule configRule2 = new ConfigRule(ConfigActionEnum.SET, "2", "2");
List<ConfigRule> configRuleList = new ArrayList<>(); List<ConfigRule> configRuleList = new ArrayList<>();
configRuleList.add(configRule1); configRuleList.add(configRule1);
configRuleList.add(configRule2); configRuleList.add(configRule2);
...@@ -68,10 +65,6 @@ public class AutomationFunctionalServiceTest { ...@@ -68,10 +65,6 @@ public class AutomationFunctionalServiceTest {
Uni<DeviceConfig> expectedDeviceConfigUni = Uni.createFrom().item(expectedDeviceConfig); Uni<DeviceConfig> expectedDeviceConfigUni = Uni.createFrom().item(expectedDeviceConfig);
Uni<DeviceId> expectedDeviceId = Uni.createFrom().item(deviceId); Uni<DeviceId> expectedDeviceId = Uni.createFrom().item(deviceId);
DeviceRoleId deviceRoleId = new DeviceRoleId(uuid, deviceId);
DeviceRole deviceRole = new DeviceRole(deviceRoleId, DeviceRoleType.DEV_CONF);
Device device = new Device(deviceId, deviceType, DeviceOperationalStatus.DISABLED); Device device = new Device(deviceId, deviceType, DeviceOperationalStatus.DISABLED);
Uni<Device> deviceUni = Uni.createFrom().item(device); Uni<Device> deviceUni = Uni.createFrom().item(device);
...@@ -80,7 +73,7 @@ public class AutomationFunctionalServiceTest { ...@@ -80,7 +73,7 @@ public class AutomationFunctionalServiceTest {
.thenReturn(expectedDeviceConfigUni); .thenReturn(expectedDeviceConfigUni);
Mockito.when(deviceGateway.configureDevice(Mockito.any())).thenReturn(expectedDeviceId); Mockito.when(deviceGateway.configureDevice(Mockito.any())).thenReturn(expectedDeviceId);
final var currentDevice = automationService.ztpAdd(deviceRole); final var currentDevice = automationService.addDevice(deviceId);
Assertions.assertThat(currentDevice).isNotNull(); Assertions.assertThat(currentDevice).isNotNull();
currentDevice currentDevice
...@@ -88,12 +81,21 @@ public class AutomationFunctionalServiceTest { ...@@ -88,12 +81,21 @@ public class AutomationFunctionalServiceTest {
.with( .with(
deviceConfig -> { deviceConfig -> {
LOGGER.infof("Received response %s", deviceConfig); LOGGER.infof("Received response %s", deviceConfig);
assertThat(deviceConfig.getDeviceOperationalStatus().toString()) assertThat(deviceConfig.getDeviceOperationalStatus().toString())
.isEqualTo(device.getDeviceOperationalStatus().toString()); .isEqualTo(device.getDeviceOperationalStatus().toString());
assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull();
assertThat(deviceConfig.getDeviceConfig().toString())
.isEqualTo(expectedDeviceConfig.toString()); final var rulesList = deviceConfig.getDeviceConfig().getConfigRules();
assertThat(deviceConfig.getDeviceId().getId()).isEqualTo(deviceId.getId());
for (int i = 0; i < rulesList.size(); i++) {
assertThat(rulesList.get(i).getResourceKey()).isEqualTo(String.valueOf(i + 1));
assertThat(rulesList.get(i).getResourceValue()).isEqualTo(String.valueOf(i + 1));
}
assertThat(deviceConfig.getDeviceId().getId().getId())
.isEqualTo(deviceId.getId().getId());
}); });
} }
...@@ -125,24 +127,20 @@ public class AutomationFunctionalServiceTest { ...@@ -125,24 +127,20 @@ public class AutomationFunctionalServiceTest {
List<ConfigRule> configRuleList = new ArrayList<>(); List<ConfigRule> configRuleList = new ArrayList<>();
ConfigRule configRule1 = ConfigRule expectedConfigRule =
new ConfigRule(ConfigActionEnum.UNDEFINED, "001", "already-configured"); new ConfigRule(ConfigActionEnum.UNDEFINED, "001", "already-configured");
configRuleList.add(configRule1); configRuleList.add(expectedConfigRule);
DeviceConfig expectedDeviceConfig = new DeviceConfig(configRuleList); DeviceConfig expectedDeviceConfig = new DeviceConfig(configRuleList);
Uuid devRoleIdUuid = new Uuid(outDeviceRoleId.getDevRoleId().getUuid());
DeviceRoleId deviceRoleId = new DeviceRoleId(devRoleIdUuid, deviceId);
DeviceRole deviceRole = new DeviceRole(deviceRoleId, DeviceRoleType.PIPELINE_CONF);
Device device = Device device =
new Device(deviceId, deviceType, expectedDeviceConfig, DeviceOperationalStatus.ENABLED); new Device(deviceId, deviceType, expectedDeviceConfig, DeviceOperationalStatus.ENABLED);
Uni<Device> deviceUni = Uni.createFrom().item(device); Uni<Device> deviceUni = Uni.createFrom().item(device);
Mockito.when(contextGateway.getDevice(Mockito.any())).thenReturn(deviceUni); Mockito.when(contextGateway.getDevice(Mockito.any())).thenReturn(deviceUni);
final var currentDevice = automationService.ztpAdd(deviceRole); final var currentDevice = automationService.addDevice(deviceId);
Assertions.assertThat(currentDevice).isNotNull(); Assertions.assertThat(currentDevice).isNotNull();
...@@ -151,13 +149,23 @@ public class AutomationFunctionalServiceTest { ...@@ -151,13 +149,23 @@ public class AutomationFunctionalServiceTest {
.with( .with(
deviceConfig -> { deviceConfig -> {
LOGGER.infof("Received response %s", deviceConfig); LOGGER.infof("Received response %s", deviceConfig);
assertThat(deviceConfig.getDeviceOperationalStatus().toString()) assertThat(deviceConfig.getDeviceOperationalStatus().toString())
.isEqualTo(device.getDeviceOperationalStatus().toString()); .isEqualTo(device.getDeviceOperationalStatus().toString());
assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull();
assertThat(deviceConfig.getDeviceConfig().toString())
.isEqualTo(expectedDeviceConfig.toString()); final var rulesList = deviceConfig.getDeviceConfig().getConfigRules();
assertThat(deviceConfig.getDeviceId().getId().toString())
.isEqualTo(deviceId.getId().toString()); for (ConfigRule configRule : rulesList) {
assertThat(configRule.getResourceKey())
.isEqualTo(expectedConfigRule.getResourceKey());
assertThat(configRule.getResourceValue())
.isEqualTo(expectedConfigRule.getResourceValue());
}
assertThat(deviceConfig.getDeviceId().getId().getId())
.isEqualTo(deviceId.getId().getId());
}); });
} }
} }
...@@ -2,6 +2,7 @@ package eu.teraflow.automation; ...@@ -2,6 +2,7 @@ package eu.teraflow.automation;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import automation.Automation;
import automation.Automation.DeviceRole; import automation.Automation.DeviceRole;
import automation.Automation.DeviceRoleId; import automation.Automation.DeviceRoleId;
import automation.AutomationService; import automation.AutomationService;
...@@ -177,20 +178,15 @@ class AutomationServiceTest { ...@@ -177,20 +178,15 @@ class AutomationServiceTest {
throws ExecutionException, InterruptedException, TimeoutException { throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> message = new CompletableFuture<>(); CompletableFuture<String> message = new CompletableFuture<>();
final var empty = Automation.Empty.newBuilder().build();
final var uuid =
Uuid.newBuilder()
.setUuid(UUID.fromString("0f14d0ab-9605-4a62-a9e4-5ed26688389b").toString())
.build();
final var deviceId = ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuid).build();
client client
.ztpDeleteAllByDeviceId(deviceId) .ztpDeleteAll(empty)
.subscribe() .subscribe()
.with( .with(
deviceRoleState -> { deletionResult -> {
LOGGER.infof("Received response %s", deviceRoleState); LOGGER.infof("Received response %s", deletionResult);
message.complete(deviceRoleState.getDevRoleId().toString()); message.complete(deletionResult.toString());
}); });
assertThat(message.get(5, TimeUnit.SECONDS)).isEmpty(); assertThat(message.get(5, TimeUnit.SECONDS)).isEmpty();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment