Commit 02a2bfdb authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'develop' of ssh://gifrerenom_labs.etsi.org/tfs/controller into...

Merge branch 'develop' of ssh://gifrerenom_labs.etsi.org/tfs/controller into feat/148-ubi-refactor-policy-component
parents 5a4fc3a6 2856acfd
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -171,5 +171,8 @@ local_k8s_deployment.sh
# asdf configuration
.tool-versions

# libyang build files
libyang/

# Other logs
**/logs/*.log.*
+3 −0
Original line number Diff line number Diff line
@@ -32,8 +32,11 @@ sudo apt-get --yes --quiet --quiet update
sudo apt-get --yes --quiet --quiet install build-essential cmake libpcre2-dev python3-dev python3-cffi
mkdir libyang
git clone https://github.com/CESNET/libyang.git libyang
git fetch
git checkout v2.1.148
mkdir libyang/build
cd libyang/build
echo "*" > .gitignore
cmake -D CMAKE_BUILD_TYPE:String="Release" ..
make
sudo make install
+3 −0
Original line number Diff line number Diff line
@@ -61,6 +61,9 @@ RUN apt-get --yes --quiet --quiet update && \
    rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/libyang
RUN git clone https://github.com/CESNET/libyang.git /var/libyang
WORKDIR /var/libyang
RUN git fetch
RUN git checkout v2.1.148
RUN mkdir -p /var/libyang/build
WORKDIR /var/libyang/build
RUN cmake -D CMAKE_BUILD_TYPE:String="Release" ..
+3 −0
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ sudo apt-get install python3-dev gcc python3-cffi
```bash
mkdir ~/tfs-ctrl/libyang
git clone https://github.com/CESNET/libyang.git ~/tfs-ctrl/libyang
cd ~/tfs-ctrl/libyang
git fetch
git checkout v2.1.148
mkdir ~/tfs-ctrl/libyang/build
cd ~/tfs-ctrl/libyang/build
cmake -D CMAKE_BUILD_TYPE:String="Release" ..
+72 −96
Original line number Diff line number Diff line
@@ -23,12 +23,12 @@ import org.etsi.tfs.ztp.context.ContextService;
import org.etsi.tfs.ztp.context.model.Device;
import org.etsi.tfs.ztp.context.model.DeviceConfig;
import org.etsi.tfs.ztp.device.DeviceService;
import org.etsi.tfs.ztp.exception.ExternalServiceFailureException;
import org.jboss.logging.Logger;

@ApplicationScoped
public class ZtpServiceImpl implements ZtpService {
    private static final Logger LOGGER = Logger.getLogger(ZtpServiceImpl.class);
    // private static final String MESSAGE = "Retrieved %s";

    private final DeviceService deviceService;
    private final ContextService contextService;
@@ -41,128 +41,104 @@ public class ZtpServiceImpl implements ZtpService {

    @Override
    public Uni<Device> addDevice(String deviceId) {
        final var deserializedDeviceUni = contextService.getDevice(deviceId);

        deserializedDeviceUni
        return contextService
                .getDevice(deviceId)
                .onFailure()
                .recoverWithNull()
                .subscribe()
                .with(
                .transform(failure -> new ExternalServiceFailureException(failure.getMessage()))
                .onItem()
                .transformToUni(
                        device -> {
                            final var id = deviceId;

                            if (device == null) {
                                LOGGER.warnf("%s is null. Ignoring...", device);
                                return;
                            }

                            if (device.isEnabled()) {
                                LOGGER.warnf("%s has already been enabled. Ignoring...", device);
                                return;
                                return Uni.createFrom().failure(new Exception("Device is already enabled"));
                            } else {
                                return addDeviceTo(device, deviceId);
                            }
                        });
    }

                            // LOGGER.infof(MESSAGE, device);

                            final var initialConfiguration =
                                    deviceService.getInitialConfiguration(device.getDeviceId());

    public Uni<Device> addDeviceTo(Device device, String deviceId) {
        LOGGER.infof("Enabling device with ID [%s]", deviceId);
        device.enableDevice();
                            LOGGER.infof("Enabled device [%s]", id);

                            initialConfiguration
                                    .subscribe()
                                    .with(
        final Uni<DeviceConfig> initialConfiguration = deviceService.getInitialConfiguration(deviceId);

        return initialConfiguration
                .onItem()
                .transformToUni(
                        deviceConfig -> {
                            device.setDeviceConfiguration(deviceConfig);
                                                final var configuredDeviceIdUni = deviceService.configureDevice(device);

                                                configuredDeviceIdUni
                                                        .subscribe()
                                                        .with(
                                                                configuredDeviceId ->
                            LOGGER.infof(
                                                                                "Device [%s] has been successfully enabled and configured with %s.\n",
                                                                                id, deviceConfig));
                                    "Configuring device with ID [%s] with initial configuration %s",
                                    deviceId, deviceConfig);
                            return deviceService
                                    .configureDevice(device)
                                    .map(
                                            configuredDeviceId -> {
                                                LOGGER.infof(
                                                        "Device with ID [%s] has been successfully enabled and configured.",
                                                        deviceId);
                                                return device;
                                            });
                        });

        return deserializedDeviceUni;
    }

    @Override
    public Uni<Device> deleteDevice(String deviceId) {
        final var deserializedDeviceUni = contextService.getDevice(deviceId);

        deserializedDeviceUni
        return contextService
                .getDevice(deviceId)
                .onFailure()
                .recoverWithNull()
                .subscribe()
                .with(
                .transform(failure -> new ExternalServiceFailureException(failure.getMessage()))
                .onItem()
                .transformToUni(
                        device -> {
                            final var id = deviceId;

                            if (device == null) {
                                LOGGER.warnf("%s is null. Ignoring...", device);
                                return;
                            }

                            if (device.isDisabled()) {
                                LOGGER.warnf("%s has already been disabled. Ignoring...", device);
                                return;
                            }

                                LOGGER.warnf("Device with ID %s has already been disabled. Ignoring...", deviceId);
                                return Uni.createFrom().nullItem();
                            } else {
                                LOGGER.infof("Disabling device with ID [%s]", deviceId);
                                device.disableDevice();
                            LOGGER.infof("Disabled device [%s]", id);

                            // LOGGER.infof(MESSAGE, device);

                            final var empty = deviceService.deleteDevice(device.getDeviceId());

                            empty
                                    .subscribe()
                                    .with(
                                            emptyMessage ->
                                                    LOGGER.infof("Device [%s] has been successfully deleted.\n", id));
                                return deviceService
                                        .deleteDevice(deviceId)
                                        .onItem()
                                        .transform(
                                                emptyMessage -> {
                                                    LOGGER.infof(
                                                            "Device with ID [%s] has been successfully deleted.", deviceId);
                                                    return device;
                                                });
                            }
                        });

        return deserializedDeviceUni;
    }

    @Override
    public Uni<Device> updateDevice(String deviceId, DeviceConfig deviceConfig) {
        final var deserializedDeviceUni = contextService.getDevice(deviceId);

        deserializedDeviceUni
        return contextService
                .getDevice(deviceId)
                .onFailure()
                .recoverWithNull()
                .subscribe()
                .with(
                .transform(failure -> new ExternalServiceFailureException(failure.getMessage()))
                .onItem()
                .transformToUni(
                        device -> {
                            final var id = deviceId;

                            if (device == null) {
                                LOGGER.warnf("%s is null. Ignoring...", device);
                                return;
                            }

                            if (!device.isEnabled()) {
                                LOGGER.warnf("Cannot update disabled device %s. Ignoring...", device);
                                return;
                            }

                            // LOGGER.infof(MESSAGE, device);
                                LOGGER.warnf("Cannot update disabled device %s. Ignoring...", deviceId);
                                return Uni.createFrom().nullItem();
                            } else {
                                LOGGER.infof("Updating configuration of device with ID [%s]", deviceId);
                                device.setDeviceConfiguration(deviceConfig);
                            final var updatedDeviceIdUni = deviceService.configureDevice(device);

                            updatedDeviceIdUni
                                    .subscribe()
                                    .with(
                                            configuredDeviceId ->
                                return deviceService
                                        .configureDevice(device)
                                        .onItem()
                                        .transform(
                                                configuredDeviceId -> {
                                                    LOGGER.infof(
                                                            "Device [%s] has been successfully updated with %s.\n",
                                                            id, deviceConfig));
                                                            "Device with ID [%s] has been successfully updated with %s.",
                                                            deviceId, deviceConfig);
                                                    return device;
                                                });
                            }
                        });

        return deserializedDeviceUni;
    }
}
Loading