Search in sources :

Example 6 with UnknownEntityException

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

the class DeviceManagementService method setDeviceAlias.

@Transactional(value = "writableTransactionManager")
public void setDeviceAlias(@Identification final String organisationIdentification, final String deviceIdentification, final String deviceAlias, final List<DeviceOutputSetting> newDeviceOutputSettings) throws FunctionalException {
    final Ssld existingSsld = this.writableSsldRepository.findByDeviceIdentification(deviceIdentification);
    if (existingSsld == null) {
        // device does not exist
        LOGGER.info("Device does not exist, cannot set Alias.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceIdentification));
    }
    // Check to see if the organization is authorized for SET_DEVICE_ALIASES
    final Organisation organisation = this.organisationRepository.findByOrganisationIdentification(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, existingSsld, DeviceFunction.SET_DEVICE_ALIASES);
    if (deviceAlias != null) {
        existingSsld.setAlias(deviceAlias);
        this.writableDeviceRepository.save(existingSsld);
    }
    if (newDeviceOutputSettings != null && !newDeviceOutputSettings.isEmpty()) {
        this.updateRelayAliases(newDeviceOutputSettings, existingSsld);
    }
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with UnknownEntityException

use of org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException 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));
        }
    }
}
Also used : 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with UnknownEntityException

use of org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException 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);
}
Also used : Ean(org.opensmartgridplatform.domain.core.entities.Ean) 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 9 with UnknownEntityException

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

the class DeviceInstallationService method updateLightMeasurementDevice.

@Transactional(value = "writableTransactionManager")
public void updateLightMeasurementDevice(@Identification final String organisationIdentification, @Valid final LightMeasurementDevice updateLightMeasurementDevice) throws FunctionalException {
    final LightMeasurementDevice existingLmd = this.writableLightMeasurementDeviceRepository.findByDeviceIdentification(updateLightMeasurementDevice.getDeviceIdentification());
    if (existingLmd == 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, updateLightMeasurementDevice.getDeviceIdentification()));
    }
    final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingLmd, 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 LMD
    existingLmd.updateMetaData(updateLightMeasurementDevice.getAlias(), updateLightMeasurementDevice.getContainerAddress(), updateLightMeasurementDevice.getGpsCoordinates());
    existingLmd.setDeviceModel(updateLightMeasurementDevice.getDeviceModel());
    existingLmd.setDescription(updateLightMeasurementDevice.getDescription());
    existingLmd.setCode(updateLightMeasurementDevice.getCode());
    existingLmd.setColor(updateLightMeasurementDevice.getColor());
    existingLmd.setDigitalInput(updateLightMeasurementDevice.getDigitalInput());
    this.writableLightMeasurementDeviceRepository.save(existingLmd);
}
Also used : LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UnknownEntityException

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

the class DeviceManagementService method updateKey.

// === UPDATE KEY ===
public void updateKey(final String organisationIdentification, @Identification final String deviceIdentification, final String correlationUid, final String messageType, @PublicKey final String publicKey) throws FunctionalException {
    LOGGER.info("MessageType: {}. Updating key for device [{}] on behalf of organisation [{}]", messageType, deviceIdentification, organisationIdentification);
    try {
        this.organisationDomainService.searchOrganisation(organisationIdentification);
    } catch (final UnknownEntityException e) {
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_ORGANISATION, ComponentType.DOMAIN_ADMIN, e);
    }
    this.osgpCoreRequestMessageSender.send(new RequestMessage(correlationUid, organisationIdentification, deviceIdentification, publicKey), messageType, null);
}
Also used : UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) RequestMessage(org.opensmartgridplatform.shared.infra.jms.RequestMessage) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

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