Search in sources :

Example 1 with DeviceFirmwareFile

use of org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile in project open-smart-grid-platform by OSGP.

the class DeviceFirmwareFileSteps method theDeviceFirmwareFileExists.

@Then("^the device firmware file exists$")
public void theDeviceFirmwareFileExists(final Map<String, String> settings) {
    final String deviceIdentification = settings.get(PlatformKeys.KEY_DEVICE_IDENTIFICATION);
    final Device device = this.deviceRepository.findByDeviceIdentification(deviceIdentification);
    final FirmwareFile firmwareFile = this.getFirmwareFile(getString(settings, PlatformKeys.FIRMWARE_FILE_FILENAME));
    final DeviceFirmwareFile deviceFirmwareFile = Wait.untilAndReturn(() -> {
        final DeviceFirmwareFile entity = this.deviceFirmwareFileRepository.findByDeviceAndFirmwareFile(device, firmwareFile);
        if (entity == null) {
            throw new Exception("Device with identification [" + deviceIdentification + "]");
        }
        return entity;
    });
    assertThat(deviceFirmwareFile.getDevice().getDeviceIdentification()).isEqualTo(deviceIdentification);
}
Also used : DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) Device(org.opensmartgridplatform.domain.core.entities.Device) ReadSettingsHelper.getString(org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Then(io.cucumber.java.en.Then)

Example 2 with DeviceFirmwareFile

use of org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile in project open-smart-grid-platform by OSGP.

the class FirmwareFileSteps method anInstalledFirmwareFileForAnSsld.

@Given("^an installed firmware file for an ssld$")
@Transactional("txMgrCore")
public void anInstalledFirmwareFileForAnSsld(final Map<String, String> settings) {
    final Ssld ssld = this.ssldRepository.findByDeviceIdentification(getString(settings, PlatformKeys.KEY_DEVICE_IDENTIFICATION, PlatformDefaults.DEFAULT_DEVICE_IDENTIFICATION));
    final FirmwareFile firmware = this.firmwareFileRepository.findByIdentification(getString(settings, PlatformKeys.FIRMWARE_FILE_FILENAME, PlatformDefaults.FIRMWARE_FILENAME));
    final Date installationDate = new Date();
    final String installedByUser = "installed by test code";
    final DeviceFirmwareFile deviceFirmwareFile = new DeviceFirmwareFile(ssld, firmware, installationDate, installedByUser);
    this.deviceFirmwareFileRepository.save(deviceFirmwareFile);
}
Also used : DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) ReadSettingsHelper.getString(org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Date(java.util.Date) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) Given(io.cucumber.java.en.Given) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with DeviceFirmwareFile

use of org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile in project open-smart-grid-platform by OSGP.

the class FirmwareManagementServiceTest method setUp.

@BeforeEach
void setUp() {
    // VERSION 1 and VERSION 2 have already been installed previously (in
    // that same order)
    final Manufacturer manufacturer = new Manufacturer("code", "name", false);
    final DeviceModel deviceModel = new DeviceModel(manufacturer, "modelCode", "description", false);
    final Device device = this.createDevice(deviceModel);
    when(this.deviceRepository.findByDeviceIdentification(anyString())).thenReturn(device);
    final DeviceFirmwareFile deviceFirmwareFile1 = new DeviceFirmwareFile(device, this.createFirmwareFile(VERSION_1), DateUtils.addDays(new Date(), -2), "me");
    final DeviceFirmwareFile deviceFirmwareFile2 = new DeviceFirmwareFile(device, this.createFirmwareFile(VERSION_2), DateUtils.addDays(new Date(), -1), "me");
    final List<DeviceFirmwareFile> deviceFirmwareFiles = Arrays.asList(deviceFirmwareFile1, deviceFirmwareFile2);
    when(this.deviceFirmwareFileRepository.findByDeviceOrderByInstallationDateAsc(any(Device.class))).thenReturn(deviceFirmwareFiles);
    when(this.deviceFirmwareFileRepository.save(any(DeviceFirmwareFile.class))).thenReturn(deviceFirmwareFile1);
    when(this.manufacturerRepository.findByCode(anyString())).thenReturn(manufacturer);
    when(this.deviceModelRepository.findByManufacturerAndModelCode(any(Manufacturer.class), anyString())).thenReturn(deviceModel);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) Device(org.opensmartgridplatform.domain.core.entities.Device) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with DeviceFirmwareFile

use of org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method removeFirmware.

/**
 * Removes a {@link FirmwareFile} from the platform. Throws exception if {@link FirmwareFile}
 * doesn't exist
 */
@Transactional(value = "writableTransactionManager")
public void removeFirmware(@Identification final String organisationIdentification, @Valid final int firmwareIdentification) throws OsgpException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.REMOVE_FIRMWARE);
    final FirmwareFile removedFirmwareFile = this.firmwareFileRepository.findById((long) firmwareIdentification).orElseThrow(supplyFirmwareFileNotFoundException(firmwareIdentification));
    final List<DeviceFirmwareFile> deviceFirmwares = this.deviceFirmwareFileRepository.findByFirmwareFile(removedFirmwareFile);
    if (!deviceFirmwares.isEmpty()) {
        LOGGER.info("FirmwareFile is linked to device.");
        throw new FunctionalException(FunctionalExceptionType.EXISTING_FIRMWARE_DEVICEFIRMWARE, ComponentType.WS_CORE, new ExistingEntityException(DeviceFirmwareFile.class, deviceFirmwares.get(0).getFirmwareFile().getDescription()));
    }
    /*
     * A firmware file has been changed to be related to (possibly) multiple
     * device models to be usable across different value streams for all
     * kinds of devices.
     *
     * If this code gets used in a scenario where multiple device models are
     * actually related to the firmware file it may need to be updated to
     * deal with this.
     */
    final Set<DeviceModel> deviceModels = removedFirmwareFile.getDeviceModels();
    if (deviceModels.size() != 1) {
        LOGGER.warn("Remove Firmware assumes a single DeviceModel, FirmwareFile (id={}) has {}: {}", removedFirmwareFile.getId(), deviceModels.size(), deviceModels);
    }
    final DeviceModel deviceModel = deviceModels.iterator().next();
    // Only remove the file if no other firmware is using it.
    if (deviceModel.isFileStorage() && this.firmwareFileRepository.findByDeviceModelAndFilename(deviceModel, removedFirmwareFile.getFilename()).size() == 1) {
        this.removePhysicalFirmwareFile(this.createFirmwarePath(deviceModel, removedFirmwareFile.getFilename()));
    }
    this.firmwareFileRepository.delete(removedFirmwareFile);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with DeviceFirmwareFile

use of org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method checkFirmwareHistoryForModuleVersionsNotCurrentlyInstalled.

/**
 * @param deviceId the id of the device we are checking
 * @param firmwareVersions the list of firmware modules versions (so type and version) to check if
 *     they are currently installed on the device, using the history of the devices firmware
 *     history
 * @return a list of firmware module versions not present in the the devices firmware history
 */
private List<FirmwareVersion> checkFirmwareHistoryForModuleVersionsNotCurrentlyInstalled(final String deviceId, final List<FirmwareVersion> firmwareVersions) {
    if (firmwareVersions.isEmpty()) {
        return firmwareVersions;
    }
    // copy input parameter
    final List<FirmwareVersion> firmwareVersionsToCheck = new ArrayList<>(firmwareVersions);
    // gets list of all historically installed modules
    final Device device = this.deviceRepository.findByDeviceIdentification(deviceId);
    final List<DeviceFirmwareFile> deviceFirmwareFiles = this.deviceFirmwareFileRepository.findByDeviceOrderByInstallationDateAsc(device);
    // Transform this list so it contains only the latest entry for each
    // moduleType
    final Map<String, FirmwareVersionWithInstallationDate> currentlyInstalledFirmwareVersionsPerType = new HashMap<>();
    for (final DeviceFirmwareFile firmwareFile : deviceFirmwareFiles) {
        final Map<FirmwareModule, String> fwms = firmwareFile.getFirmwareFile().getModuleVersions();
        final Date installationDate = firmwareFile.getInstallationDate();
        for (final Map.Entry<FirmwareModule, String> entry : fwms.entrySet()) {
            final String version = entry.getValue();
            final FirmwareModule fwm = entry.getKey();
            // of a later date
            if (currentlyInstalledFirmwareVersionsPerType.containsKey(fwm.getDescription()) && currentlyInstalledFirmwareVersionsPerType.get(fwm.getDescription()).getInstallationDate().before(installationDate)) {
                currentlyInstalledFirmwareVersionsPerType.replace(fwm.getDescription(), new FirmwareVersionWithInstallationDate(installationDate, new FirmwareVersion(FirmwareModuleType.forDescription(fwm.getDescription()), version)));
            } else {
                // no other module of this type found yet so just add it
                currentlyInstalledFirmwareVersionsPerType.put(fwm.getDescription(), new FirmwareVersionWithInstallationDate(installationDate, new FirmwareVersion(FirmwareModuleType.forDescription(fwm.getDescription()), version)));
            }
        }
    }
    final List<FirmwareVersion> latestfirmwareVersionsOfEachModuleTypeInHistory = currentlyInstalledFirmwareVersionsPerType.values().stream().map(e -> new FirmwareVersion(e.getFirmwareVersion().getFirmwareModuleType(), e.getFirmwareVersion().getVersion())).collect(Collectors.toList());
    // remove the latest history (module)versions from the firmwareVersions
    // parameter
    firmwareVersionsToCheck.removeAll(latestfirmwareVersionsOfEachModuleTypeInHistory);
    return firmwareVersionsToCheck;
}
Also used : Date(java.util.Date) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) LoggerFactory(org.slf4j.LoggerFactory) Identification(org.opensmartgridplatform.shared.validation.Identification) Autowired(org.springframework.beans.factory.annotation.Autowired) FirmwareModule(org.opensmartgridplatform.domain.core.entities.FirmwareModule) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) Locale(java.util.Locale) Map(java.util.Map) RequestMessage(org.opensmartgridplatform.shared.infra.jms.RequestMessage) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) SsldPendingFirmwareUpdate(org.opensmartgridplatform.domain.core.entities.SsldPendingFirmwareUpdate) Collection(java.util.Collection) DeviceRepository(org.opensmartgridplatform.domain.core.repositories.DeviceRepository) FirmwareModuleType(org.opensmartgridplatform.domain.core.valueobjects.FirmwareModuleType) FirmwareUpdateMessageDataContainer(org.opensmartgridplatform.domain.core.valueobjects.FirmwareUpdateMessageDataContainer) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) Entry(java.util.Map.Entry) Optional(java.util.Optional) TechnicalException(org.opensmartgridplatform.shared.exceptionhandling.TechnicalException) DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) CorrelationIds(org.opensmartgridplatform.shared.infra.jms.CorrelationIds) ResponseMessage(org.opensmartgridplatform.shared.infra.jms.ResponseMessage) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceFirmwareFileRepository(org.opensmartgridplatform.domain.core.repositories.DeviceFirmwareFileRepository) FirmwareFileRepository(org.opensmartgridplatform.domain.core.repositories.FirmwareFileRepository) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) Service(org.springframework.stereotype.Service) DeviceModelRepository(org.opensmartgridplatform.domain.core.repositories.DeviceModelRepository) ManufacturerRepository(org.opensmartgridplatform.domain.core.repositories.ManufacturerRepository) ComponentType(org.opensmartgridplatform.shared.exceptionhandling.ComponentType) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) Logger(org.slf4j.Logger) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) FirmwareVersionDto(org.opensmartgridplatform.dto.valueobjects.FirmwareVersionDto) FirmwareVersion(org.opensmartgridplatform.domain.core.valueobjects.FirmwareVersion) Device(org.opensmartgridplatform.domain.core.entities.Device) SsldPendingFirmwareUpdateRepository(org.opensmartgridplatform.domain.core.repositories.SsldPendingFirmwareUpdateRepository) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Comparator(java.util.Comparator) DeviceFunction(org.opensmartgridplatform.domain.core.valueobjects.DeviceFunction) MessagePriorityEnum(org.opensmartgridplatform.shared.wsheaderattribute.priority.MessagePriorityEnum) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) HashMap(java.util.HashMap) Device(org.opensmartgridplatform.domain.core.entities.Device) ArrayList(java.util.ArrayList) FirmwareVersion(org.opensmartgridplatform.domain.core.valueobjects.FirmwareVersion) Date(java.util.Date) FirmwareModule(org.opensmartgridplatform.domain.core.entities.FirmwareModule) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)10 Device (org.opensmartgridplatform.domain.core.entities.Device)8 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)8 Date (java.util.Date)6 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)5 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 ArrayList (java.util.ArrayList)3 Manufacturer (org.opensmartgridplatform.domain.core.entities.Manufacturer)3 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)3 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)3 Serializable (java.io.Serializable)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2