Search in sources :

Example 6 with DeviceAuthorization

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

the class DeviceManagementService method addDeviceAuthorization.

public void addDeviceAuthorization(@Identification final String ownerOrganisationIdentification, @Identification final String organisationIdentification, @Identification final String deviceIdentification, @NotNull final DeviceFunctionGroup group) throws FunctionalException {
    // Check input data and authorization
    final Organisation organisation = this.findOrganisation(organisationIdentification);
    final Organisation ownerOrganisation = this.findOrganisation(ownerOrganisationIdentification);
    final Device device = this.findDevice(deviceIdentification);
    this.isAllowed(ownerOrganisation, device, DeviceFunction.SET_DEVICE_AUTHORIZATION);
    // Check if group is already set on device
    for (final DeviceAuthorization authorization : device.getAuthorizations()) {
        if (authorization.getOrganisation() == organisation && authorization.getFunctionGroup() == group) {
            LOGGER.info("Organisation {} already has authorization for group {} on device {}", organisationIdentification, group, deviceIdentification);
            // Ignore the request, the authorization is already available
            return;
        }
    }
    // All checks pass, add new authorization
    final DeviceAuthorization authorization = device.addAuthorization(organisation, group);
    this.deviceRepository.save(device);
    this.authorizationRepository.save(authorization);
    LOGGER.info("Organisation {} now has authorization for function group {} on device {}", organisationIdentification, group, deviceIdentification);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)

Example 7 with DeviceAuthorization

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

the class DeviceManagementService method removeDevice.

// === REMOVE DEVICE ===
/**
 * Removes a device.
 *
 * @param organisationIdentification The organisation identification who performs the action
 * @param deviceIdentification The device identification of the device
 * @throws FunctionalException In case the device or organisation can not be found or the
 *     organisation is not allowed to perform this action.
 */
public void removeDevice(@Identification final String organisationIdentification, @Identification final String deviceIdentification) throws FunctionalException {
    final Organisation organisation = this.findOrganisation(organisationIdentification);
    final Device device = this.findDevice(deviceIdentification);
    this.isAllowed(organisation, device, DeviceFunction.REMOVE_DEVICE);
    // First remove all authorizations
    final List<DeviceAuthorization> authorisations = this.authorizationRepository.findByDevice(device);
    for (final DeviceAuthorization authorisation : authorisations) {
        this.authorizationRepository.delete(authorisation);
    }
    // Remove all events
    final List<Event> events = this.eventRepository.findByDeviceIdentification(deviceIdentification);
    for (final Event event : events) {
        this.eventRepository.delete(event);
    }
    // Then remove the device.
    this.deviceRepository.delete(device);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) Event(org.opensmartgridplatform.domain.core.entities.Event)

Example 8 with DeviceAuthorization

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

the class DeviceConverterHelper method initJaxb.

Device initJaxb(final T source) {
    final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device destination = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device();
    destination.setAlias(source.getAlias());
    destination.setActivated(source.isActivated());
    destination.setDeviceLifecycleStatus(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceLifecycleStatus.valueOf(source.getDeviceLifecycleStatus().name()));
    destination.setContainerAddress(this.mapper.map(source.getContainerAddress(), org.opensmartgridplatform.adapter.ws.schema.core.common.Address.class));
    destination.setDeviceIdentification(source.getDeviceIdentification());
    destination.setDeviceType(source.getDeviceType());
    destination.setTechnicalInstallationDate(this.mapper.map(source.getTechnicalInstallationDate(), XMLGregorianCalendar.class));
    if (!Objects.isNull(source.getGpsCoordinates())) {
        final GpsCoordinates gpsCoordinates = source.getGpsCoordinates();
        if (gpsCoordinates.getLatitude() != null) {
            destination.setGpsLatitude(Float.toString(gpsCoordinates.getLatitude()));
        }
        if (gpsCoordinates.getLongitude() != null) {
            destination.setGpsLongitude(Float.toString(gpsCoordinates.getLongitude()));
        }
    }
    destination.setNetworkAddress(source.getNetworkAddress() == null ? null : source.getNetworkAddress().toString());
    destination.setOwner(source.getOwner() == null ? "" : source.getOwner().getName());
    destination.getOrganisations().addAll(source.getOrganisations());
    destination.setInMaintenance(source.isInMaintenance());
    final List<org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization> deviceAuthorizations = new ArrayList<>();
    for (final DeviceAuthorization deviceAuthorisation : source.getAuthorizations()) {
        final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization newDeviceAuthorization = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceAuthorization();
        newDeviceAuthorization.setFunctionGroup(deviceAuthorisation.getFunctionGroup().name());
        newDeviceAuthorization.setOrganisation(deviceAuthorisation.getOrganisation().getOrganisationIdentification());
        deviceAuthorizations.add(newDeviceAuthorization);
    }
    destination.getDeviceAuthorizations().addAll(deviceAuthorizations);
    if (source.getDeviceModel() != null) {
        final DeviceModel deviceModel = new DeviceModel();
        deviceModel.setDescription(source.getDeviceModel().getDescription());
        if (source.getDeviceModel().getManufacturer() != null) {
            final Manufacturer manufacturer = new Manufacturer();
            manufacturer.setManufacturerId(source.getDeviceModel().getManufacturer().getCode());
            manufacturer.setName(source.getDeviceModel().getManufacturer().getName());
            manufacturer.setUsePrefix(source.getDeviceModel().getManufacturer().isUsePrefix());
            deviceModel.setManufacturer(manufacturer);
        }
        deviceModel.setModelCode(source.getDeviceModel().getModelCode());
        destination.setDeviceModel(deviceModel);
    }
    destination.setLastCommunicationTime(this.mapper.map(source.getLastSuccessfulConnectionTimestamp(), XMLGregorianCalendar.class));
    if (source instanceof LightMeasurementDevice) {
        final LightMeasurementDevice sourceLmd = (LightMeasurementDevice) source;
        final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.LightMeasurementDevice destinationLmd = new org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.LightMeasurementDevice();
        destinationLmd.setDescription(sourceLmd.getDescription());
        destinationLmd.setCode(sourceLmd.getCode());
        destinationLmd.setColor(sourceLmd.getColor());
        destinationLmd.setDigitalInput(sourceLmd.getDigitalInput());
        destinationLmd.setLastCommunicationTime(this.mapper.map(sourceLmd.getLastCommunicationTime(), XMLGregorianCalendar.class));
        destination.setLightMeasurementDevice(destinationLmd);
    }
    return destination;
}
Also used : DeviceModel(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceModel) Address(org.opensmartgridplatform.domain.core.valueobjects.Address) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) Device(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device) ArrayList(java.util.ArrayList) Device(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device) GpsCoordinates(org.opensmartgridplatform.domain.core.valueobjects.GpsCoordinates) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) Manufacturer(org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Manufacturer)

Example 9 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization 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 10 with DeviceAuthorization

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

Aggregations

DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)25 Device (org.opensmartgridplatform.domain.core.entities.Device)17 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)15 Transactional (org.springframework.transaction.annotation.Transactional)9 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)7 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)5 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)5 Then (io.cucumber.java.en.Then)4 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)4 NotAuthorizedException (org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException)4 DeviceFunctionGroup (org.opensmartgridplatform.domain.core.valueobjects.DeviceFunctionGroup)4 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)3 Given (io.cucumber.java.en.Given)2 ExistingEntityException (org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceException (javax.persistence.PersistenceException)1