Search in sources :

Example 1 with NotAuthorizedException

use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException 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 2 with NotAuthorizedException

use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException 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 3 with NotAuthorizedException

use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException 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 4 with NotAuthorizedException

use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException 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)

Aggregations

Device (org.opensmartgridplatform.domain.core.entities.Device)4 DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)4 NotAuthorizedException (org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException)4 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)4 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 Transactional (org.springframework.transaction.annotation.Transactional)4 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)2 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)2 Ean (org.opensmartgridplatform.domain.core.entities.Ean)1