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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations