use of org.opensmartgridplatform.shared.exceptionhandling.FunctionalException in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method setMaintenanceStatus.
@Transactional(value = "writableTransactionManager")
public void setMaintenanceStatus(@Identification final String organisationIdentification, final String deviceIdentification, final boolean status) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceIdentification);
if (existingDevice == null) {
// device does not exist
LOGGER.info("Device does not exist, cannot set maintenance status.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceIdentification));
} else {
// Check to see if the organisation is CONFIGURATION or OWNER
// authorized
boolean isAuthorized = false;
for (final DeviceAuthorization authorizations : existingDevice.getAuthorizations()) {
if (organisationIdentification.equals(authorizations.getOrganisation().getOrganisationIdentification()) && (DeviceFunctionGroup.OWNER.equals(authorizations.getFunctionGroup()) || DeviceFunctionGroup.CONFIGURATION.equals(authorizations.getFunctionGroup()))) {
isAuthorized = true;
existingDevice.updateInMaintenance(status);
this.writableDeviceRepository.save(existingDevice);
break;
}
}
if (!isAuthorized) {
// unauthorized, throwing exception.
throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
}
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.FunctionalException in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method updateDevice.
@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, final String deviceToUpdateIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceToUpdateIdentification);
if (existingDevice == null) {
// device does not exist
LOGGER.info("Device does not exist, nothing to update.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceToUpdateIdentification));
}
final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingDevice, DeviceFunctionGroup.OWNER);
// Check organisation against owner of device
boolean isOwner = false;
for (final DeviceAuthorization owner : owners) {
if (owner.getOrganisation().getOrganisationIdentification().equalsIgnoreCase(organisationIdentification)) {
isOwner = true;
}
}
if (!isOwner) {
LOGGER.info("Device has no owner yet, or organisation is not the owner.");
throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
}
// Update the device
existingDevice.updateMetaData(updateDevice.getAlias(), updateDevice.getContainerAddress(), updateDevice.getGpsCoordinates());
existingDevice.setActivated(updateDevice.isActivated());
if (updateDevice.getDeviceLifecycleStatus() != null) {
existingDevice.setDeviceLifecycleStatus(updateDevice.getDeviceLifecycleStatus());
}
if (updateDevice.getTechnicalInstallationDate() != null) {
existingDevice.setTechnicalInstallationDate(updateDevice.getTechnicalInstallationDate());
}
final Ssld ssld = this.writableSsldRepository.findById(existingDevice.getId()).orElseThrow(() -> new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE));
ssld.updateOutputSettings(updateDevice.receiveOutputSettings());
ssld.setEans(updateDevice.getEans());
for (final Ean ean : updateDevice.getEans()) {
ean.setDevice(ssld);
}
this.writableSsldRepository.save(ssld);
}
use of org.opensmartgridplatform.shared.exceptionhandling.FunctionalException 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.shared.exceptionhandling.FunctionalException in project open-smart-grid-platform by OSGP.
the class FirmwareService method checkFirmwareFileSupportsDeviceModel.
public void checkFirmwareFileSupportsDeviceModel(final SmartMeter smartMeter, final FirmwareFile firmware) throws FunctionalException {
final DeviceModel deviceModel = this.determineDeviceModel(smartMeter);
final List<String> deviceModelCodes = firmware.getDeviceModels().stream().map(DeviceModel::getModelCode).collect(Collectors.toList());
if (!deviceModelCodes.contains(deviceModel.getModelCode())) {
throw new FunctionalException(FunctionalExceptionType.FIRMWARE_DOES_NOT_SUPPORT_DEVICE_MODEL, ComponentType.DOMAIN_SMART_METERING, new OsgpException(ComponentType.DOMAIN_SMART_METERING, String.format("DeviceModel %s of smartmeter %s is not in list of devicemodels supported by firmware file %s : %s", smartMeter.getDeviceModel().getModelCode(), smartMeter.getDeviceIdentification(), firmware.getIdentification(), deviceModelCodes)));
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.FunctionalException in project open-smart-grid-platform by OSGP.
the class ConfigurationManagementEndpoint method switchConfiguration.
@PayloadRoot(localPart = "SwitchConfigurationRequest", namespace = NAMESPACE)
@ResponsePayload
public SwitchConfigurationAsyncResponse switchConfiguration(@OrganisationIdentification final String organisationIdentification, @RequestPayload final SwitchConfigurationRequest request, @MessagePriority final String messagePriority) throws OsgpException {
LOGGER.info("Switch Configuration Request received from organisation: {} for device: {} with message priority: {}.", organisationIdentification, request.getDeviceIdentification(), messagePriority);
final SwitchConfigurationAsyncResponse response = new SwitchConfigurationAsyncResponse();
try {
final String correlationUid = this.configurationManagementService.enqueueSwitchConfigurationRequest(organisationIdentification, request.getDeviceIdentification(), String.valueOf(request.getConfigurationBank()), MessagePriorityEnum.getMessagePriority(messagePriority));
final AsyncResponse asyncResponse = new AsyncResponse();
asyncResponse.setCorrelationUid(correlationUid);
asyncResponse.setDeviceId(request.getDeviceIdentification());
response.setAsyncResponse(asyncResponse);
} catch (final ConstraintViolationException e) {
throw new FunctionalException(FunctionalExceptionType.VALIDATION_ERROR, ComponentType.WS_CORE, new ValidationException(e.getConstraintViolations()));
} catch (final Exception e) {
this.handleException(e);
}
return response;
}
Aggregations