Search in sources :

Example 21 with DeviceAuthorization

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

the class DeviceManagementService method setOwner.

// === SET OWNER ===
/**
 * Sets the owner of the device
 *
 * @param organisationIdentification The organisation identification who performs the action
 *     (needed for security)
 * @param deviceIdentification The device identification of the device
 * @param newOwner The organisation identification of the new owner.
 * @throws FunctionalException In case the device or organisation can not be found or the
 *     organisation is not allowed to perform this action.
 */
public void setOwner(@Identification final String organisationIdentification, @Identification final String deviceIdentification, @Identification final String newOwner) throws FunctionalException {
    Organisation organisation = this.findOrganisation(organisationIdentification);
    final Device device = this.findDevice(deviceIdentification);
    this.isAllowed(organisation, PlatformFunction.SET_OWNER);
    organisation = this.findOrganisation(newOwner);
    // First remove any other owners.
    final List<DeviceAuthorization> owners = this.authorizationRepository.findByDeviceAndFunctionGroup(device, DeviceFunctionGroup.OWNER);
    if (!owners.isEmpty()) {
        for (final DeviceAuthorization owner : owners) {
            this.authorizationRepository.delete(owner);
        }
    }
    // Now add the authorization
    final DeviceAuthorization authorization = new DeviceAuthorization(device, organisation, DeviceFunctionGroup.OWNER);
    this.authorizationRepository.save(authorization);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)

Example 22 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization 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, @PublicKey final String publicKey, final Long protocolInfoId) throws FunctionalException {
    LOGGER.debug("Updating key for device [{}] on behalf of organisation [{}]", deviceIdentification, organisationIdentification);
    final Organisation organisation = this.findOrganisation(organisationIdentification);
    this.isAllowed(organisation, PlatformFunction.UPDATE_KEY);
    this.organisationDomainService.isOrganisationEnabled(organisation, ComponentType.WS_ADMIN);
    final Device device = this.deviceRepository.findByDeviceIdentification(deviceIdentification);
    if (device == null) {
        // Device not found, create new device
        LOGGER.debug("Device [{}] does not exist, creating new device", deviceIdentification);
        final Ssld ssld = new Ssld(deviceIdentification);
        final DeviceAuthorization authorization = ssld.addAuthorization(organisation, DeviceFunctionGroup.OWNER);
        final ProtocolInfo protocolInfo = this.protocolRepository.findById(protocolInfoId).orElseThrow(() -> new EntityNotFoundException("No protocol info record found with ID: " + protocolInfoId));
        ssld.updateProtocol(protocolInfo);
        this.authorizationRepository.save(authorization);
    }
    this.enqueueUpdateKeyRequest(organisationIdentification, deviceIdentification, publicKey);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) ProtocolInfo(org.opensmartgridplatform.domain.core.entities.ProtocolInfo) EntityNotFoundException(javax.persistence.EntityNotFoundException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld)

Example 23 with DeviceAuthorization

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

the class DeviceInstallationService method addDevice.

@Transactional(value = "writableTransactionManager")
public void addDevice(@Identification final String organisationIdentification, @Valid final Device newDevice, @Identification final String ownerOrganisationIdentification) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.GET_ORGANISATIONS);
    this.domainHelperService.isOrganisationEnabled(organisation);
    // If the device already exists, throw an exception.
    final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(newDevice.getDeviceIdentification());
    // field
    if (existingDevice != null && StringUtils.isNotBlank(existingDevice.getDeviceType())) {
        throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICE, ComponentType.WS_CORE, new ExistingEntityException(Device.class, newDevice.getDeviceIdentification()));
    }
    Ssld ssld;
    if (existingDevice != null) {
        // Update existing device
        ssld = this.writableSsldRepository.findByDeviceIdentification(newDevice.getDeviceIdentification());
        ssld.updateMetaData(newDevice.getAlias(), newDevice.getContainerAddress(), newDevice.getGpsCoordinates());
        ssld.getAuthorizations().clear();
    } else {
        // Create a new SSLD instance.
        ssld = new Ssld(newDevice.getDeviceIdentification(), newDevice.getAlias(), newDevice.getContainerAddress(), newDevice.getGpsCoordinates(), null);
    }
    ssld.setHasSchedule(false);
    ssld.setDeviceModel(newDevice.getDeviceModel());
    final Organisation ownerOrganisation = this.domainHelperService.findOrganisation(ownerOrganisationIdentification);
    ssld = this.writableSsldRepository.save(ssld);
    final DeviceAuthorization authorization = ssld.addAuthorization(ownerOrganisation, DeviceFunctionGroup.OWNER);
    this.writableAuthorizationRepository.save(authorization);
    LOGGER.info("Created new device {} with owner {}", newDevice.getDeviceIdentification(), ownerOrganisationIdentification);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with DeviceAuthorization

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

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

the class DeviceManagementService method findDevices.

/**
 * Find all devices
 *
 * @param organisationIdentification The organisation who performed the action
 * @param pageSpecifier The page to be returned
 * @param deviceFilter the filter object
 * @return A page with devices
 * @throws FunctionalException
 */
@Transactional(value = "transactionManager")
public Page<Device> findDevices(@Identification final String organisationIdentification, final PageSpecifier pageSpecifier, final DeviceFilter deviceFilter) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.FIND_DEVICES);
    this.pagingSettings.updatePagingSettings(pageSpecifier);
    Sort.Direction sortDir = Sort.Direction.DESC;
    String sortedBy = "creationTime";
    if (deviceFilter != null) {
        if (!StringUtils.isEmpty(deviceFilter.getSortDir()) && deviceFilter.getSortDir().contains("asc")) {
            sortDir = Sort.Direction.ASC;
        }
        if (!StringUtils.isEmpty(deviceFilter.getSortedBy())) {
            sortedBy = deviceFilter.getSortedBy();
        }
    }
    final PageRequest request = PageRequest.of(this.pagingSettings.getPageNumber(), this.pagingSettings.getPageSize(), sortDir, sortedBy);
    final Page<Device> devices = this.findDevices(organisationIdentification, deviceFilter, organisation, request);
    if (devices == null) {
        LOGGER.info("No devices found");
        return null;
    }
    for (final Device device : devices.getContent()) {
        for (final DeviceAuthorization deviceAutorization : device.getAuthorizations()) {
            device.addOrganisation(deviceAutorization.getOrganisation().getOrganisationIdentification());
        }
    }
    return devices;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) Sort(org.springframework.data.domain.Sort) 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