Search in sources :

Example 11 with UnknownEntityException

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

the class DeviceManagementService method removeOrganisation.

public void removeOrganisation(@Identification final String organisationIdentification, @Identification final String organisationToRemoveIdentification) throws FunctionalException {
    LOGGER.debug("removeOrganisation called with organisation {} and organisation to remove {}", organisationIdentification, organisationToRemoveIdentification);
    final Organisation organisation = this.findOrganisation(organisationIdentification);
    final Organisation organisationToRemove = this.findOrganisation(organisationToRemoveIdentification);
    this.isAllowed(organisation, PlatformFunction.REMOVE_ORGANISATION);
    try {
        final List<DeviceAuthorization> deviceAuthorizations = this.authorizationRepository.findByOrganisation(organisationToRemove);
        if (!deviceAuthorizations.isEmpty()) {
            throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICE_AUTHORIZATIONS, ComponentType.WS_ADMIN, new ValidationException(String.format("Device Authorizations are still present for the current organisation %s", organisationToRemove.getOrganisationIdentification())));
        }
        organisationToRemove.setIsEnabled(false);
        this.organisationRepository.save(organisationToRemove);
    } catch (final JpaSystemException ex) {
        if (ex.getCause() instanceof PersistenceException) {
            LOGGER.error("Remove organisation failure JpaSystemException", ex);
            throw new FunctionalException(FunctionalExceptionType.UNKNOWN_ORGANISATION, ComponentType.WS_ADMIN, new UnknownEntityException(Organisation.class, organisationToRemoveIdentification, ex));
        }
    }
}
Also used : JpaSystemException(org.springframework.orm.jpa.JpaSystemException) ValidationException(org.opensmartgridplatform.domain.core.exceptions.ValidationException) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) PersistenceException(javax.persistence.PersistenceException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Example 12 with UnknownEntityException

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

the class RtuResponseService method handleResponseMessageReceived.

@Transactional(value = "transactionManager")
public void handleResponseMessageReceived(final Logger logger, final String deviceIdentification, final boolean expectDeviceToBeKnown) throws FunctionalException {
    try {
        final RtuDevice device = this.rtuDeviceRepository.findByDeviceIdentification(deviceIdentification).orElse(null);
        if (device == null && expectDeviceToBeKnown) {
            throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, COMPONENT_TYPE, new UnknownEntityException(RtuDevice.class, deviceIdentification));
        } else if (device == null) {
            logger.info("No RTU device {} found to update communication time information for." + " This may be appropriate as the device could be expected to be unknown to GXF.", deviceIdentification);
            return;
        }
        if (this.shouldUpdateCommunicationTime(device, this.minimumDurationBetweenCommunicationTimeUpdates)) {
            device.messageReceived();
            this.rtuDeviceRepository.save(device);
        } else {
            logger.info("Last communication time within duration: {}. Skipping last communication date update.", this.minimumDurationBetweenCommunicationTimeUpdates);
        }
    } catch (final OptimisticLockException | JpaOptimisticLockingFailureException ex) {
        logger.warn("Last communication time not updated due to optimistic lock exception", ex);
    }
}
Also used : RtuDevice(org.opensmartgridplatform.domain.core.entities.RtuDevice) JpaOptimisticLockingFailureException(org.springframework.orm.jpa.JpaOptimisticLockingFailureException) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) OptimisticLockException(javax.persistence.OptimisticLockException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with UnknownEntityException

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

the class DeviceInstallationService method updateDevice.

@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
    final Ssld existingDevice = this.writableSsldRepository.findByDeviceIdentification(updateDevice.getDeviceIdentification());
    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, updateDevice.getDeviceIdentification()));
    }
    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.setPublicKeyPresent(updateDevice.isPublicKeyPresent());
    this.writableSsldRepository.save(existingDevice);
}
Also used : LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) Device(org.opensmartgridplatform.domain.core.entities.Device) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) NotAuthorizedException(org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with UnknownEntityException

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

the class FirmwareManagementService method changeFirmware.

/**
 * Updates a FirmwareFile to the platform. Throws exception if {@link FirmwareFile} doesn't exist.
 */
@Transactional(value = "writableTransactionManager")
public void changeFirmware(@Identification final String organisationIdentification, final int id, final FirmwareFileRequest firmwareFileRequest, final String manufacturer, final String modelCode, final FirmwareModuleData firmwareModuleData) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.CHANGE_FIRMWARE);
    FirmwareFile changedFirmwareFile = this.firmwareFileRepository.findById((long) id).orElseThrow(supplyFirmwareFileNotFoundException(id, firmwareFileRequest.getFileName()));
    final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturer);
    if (databaseManufacturer == null) {
        LOGGER.info("Manufacturer {} doesn't exist.", manufacturer);
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_MANUFACTURER, ComponentType.WS_CORE, new UnknownEntityException(Manufacturer.class, manufacturer));
    }
    final DeviceModel databaseDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(databaseManufacturer, modelCode);
    if (databaseDeviceModel == null) {
        LOGGER.info("DeviceModel unknown for manufacturer {} and model code {}.", manufacturer, modelCode);
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE, new UnknownEntityException(DeviceModel.class, modelCode));
    }
    changedFirmwareFile.setDescription(firmwareFileRequest.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.
     *
     * This code mimics the earlier behavior with a single device model
     * linked to the firmware file, where the device model is changed.
     *
     * If multiple device models are related, it is not clear what to do,
     * and which if the device models (if any) should be removed. In such
     * case the device model will be added for now.
     *
     * 2021-06-02: In case of multiple DeviceModels all existing must be deleted
     * and all new DeviceModels in the request must be added
     */
    final Set<DeviceModel> existingDeviceModels = changedFirmwareFile.getDeviceModels();
    if (existingDeviceModels.size() > 1) {
        LOGGER.warn("Change Firmware (FirmwareFile id={}) with {} existing DeviceModels, adding {}", changedFirmwareFile.getId(), existingDeviceModels.size(), databaseDeviceModel);
    } else {
        LOGGER.warn("Change Firmware (FirmwareFile id={}) with {} existing DeviceModel(s), replacing by {}", changedFirmwareFile.getId(), existingDeviceModels.size(), databaseDeviceModel);
    }
    existingDeviceModels.clear();
    changedFirmwareFile.addDeviceModel(databaseDeviceModel);
    changedFirmwareFile.setFilename(firmwareFileRequest.getFileName());
    changedFirmwareFile.updateFirmwareModuleData(firmwareModuleData.getVersionsByModule(this.firmwareModuleRepository, false));
    changedFirmwareFile.setPushToNewDevices(firmwareFileRequest.isPushToNewDevices());
    changedFirmwareFile.setActive(firmwareFileRequest.isActive());
    // Save the changed firmware entity
    changedFirmwareFile = this.firmwareFileRepository.save(changedFirmwareFile);
    // Set all devicefirmwares.pushToNewDevices on false
    if (firmwareFileRequest.isPushToNewDevices()) {
        final List<FirmwareFile> firmwareFiles = this.firmwareFileRepository.findByDeviceModel(databaseDeviceModel);
        firmwareFiles.remove(changedFirmwareFile);
        this.setPushToNewDevicesToFalse(firmwareFiles);
    }
    this.firmwareFileRepository.save(changedFirmwareFile);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with UnknownEntityException

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

the class FirmwareManagementService method addOrChangeFirmware.

@Transactional(value = "writableTransactionManager")
public void addOrChangeFirmware(@Identification final String organisationIdentification, final FirmwareFileRequest firmwareFileRequest, final byte[] file, final List<org.opensmartgridplatform.domain.core.valueobjects.DeviceModel> deviceModels, final FirmwareModuleData firmwareModuleData) throws OsgpException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.CREATE_FIRMWARE);
    // find for each DeviceModel from the WebServcies the corresponding entities
    // There should be at least one DeviceModel. If none found a FunctionalException should be
    // raised
    // Each DeviceModel can have it's own Manufacturer (at least a theory)
    // if a Manufacturer entity related to the DeviceModel can not be found a FunctionalException
    // should be raised
    // if one of the DeviceModel entities can not be found a FunctionalException should be raised
    final List<DeviceModel> databaseDeviceModels = new ArrayList<>();
    for (final org.opensmartgridplatform.domain.core.valueobjects.DeviceModel deviceModel : deviceModels) {
        final Manufacturer databaseManufacturer = this.findManufacturerByCode(deviceModel.getManufacturer());
        final DeviceModel databaseDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(databaseManufacturer, deviceModel.getModelCode());
        if (databaseDeviceModel == null) {
            LOGGER.info("DeviceModel doesn't exist.");
            throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE, new UnknownEntityException(DeviceModel.class, deviceModel.getModelCode()));
        }
        databaseDeviceModels.add(databaseDeviceModel);
    }
    if (!deviceModels.isEmpty() && databaseDeviceModels.isEmpty()) {
        LOGGER.info("No DeviceModels found.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE);
    }
    final Map<FirmwareModule, String> firmwareVersionsByModule = firmwareModuleData.getVersionsByModule(this.firmwareModuleRepository, true);
    final FirmwareFile firmwareFile = this.insertOrUdateDatabase(firmwareFileRequest, file);
    firmwareFile.updateFirmwareDeviceModels(databaseDeviceModels);
    firmwareFile.updateFirmwareModuleData(firmwareVersionsByModule);
    this.firmwareFileRepository.save(firmwareFile);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) ArrayList(java.util.ArrayList) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) FirmwareModule(org.opensmartgridplatform.domain.core.entities.FirmwareModule) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)17 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)15 Transactional (org.springframework.transaction.annotation.Transactional)9 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)8 Device (org.opensmartgridplatform.domain.core.entities.Device)6 DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)6 NotAuthorizedException (org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException)4 RequestMessage (org.opensmartgridplatform.shared.infra.jms.RequestMessage)4 PersistenceException (javax.persistence.PersistenceException)3 DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)3 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)3 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)3 Manufacturer (org.opensmartgridplatform.domain.core.entities.Manufacturer)3 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)3 JpaSystemException (org.springframework.orm.jpa.JpaSystemException)3 JMSException (javax.jms.JMSException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 FirmwareModule (org.opensmartgridplatform.domain.core.entities.FirmwareModule)2 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)2 RtuDevice (org.opensmartgridplatform.domain.core.entities.RtuDevice)2