Search in sources :

Example 66 with Device

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

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

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

the class DeviceManagementService method enqueueUpdateDeviceSslCertificationRequest.

public String enqueueUpdateDeviceSslCertificationRequest(final String organisationIdentification, final String deviceIdentification, final Certification certification, final int messagePriority) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    final Device device = this.domainHelperService.findActiveDevice(deviceIdentification);
    this.domainHelperService.isAllowed(organisation, device, DeviceFunction.UPDATE_DEVICE_SSL_CERTIFICATION);
    this.domainHelperService.isInMaintenance(device);
    LOGGER.debug("enqueueUpdateDeviceSslCertificationRequest called with organisation {} and device {}", organisationIdentification, deviceIdentification);
    final String correlationUid = this.correlationIdProviderService.getCorrelationId(organisationIdentification, deviceIdentification);
    final MessageMetadata messageMetadata = new MessageMetadata.Builder().withDeviceIdentification(deviceIdentification).withOrganisationIdentification(organisationIdentification).withCorrelationUid(correlationUid).withMessageType(MessageType.UPDATE_DEVICE_SSL_CERTIFICATION.name()).withMessagePriority(messagePriority).build();
    final CommonRequestMessage message = new CommonRequestMessage.Builder().messageMetadata(messageMetadata).request(certification).build();
    this.commonRequestMessageSender.send(message);
    return correlationUid;
}
Also used : CommonRequestMessage(org.opensmartgridplatform.adapter.ws.core.infra.jms.CommonRequestMessage) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device)

Example 69 with Device

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

the class FirmwareManagementService method getDeviceFirmwareFiles.

/**
 * Returns a list of all {@link DeviceFirmwareFile}s in the Platform
 */
public List<DeviceFirmwareFile> getDeviceFirmwareFiles(final String organisationIdentification, final String deviceIdentification) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.GET_FIRMWARE);
    final Device device = this.deviceRepository.findByDeviceIdentification(deviceIdentification);
    return this.deviceFirmwareFileRepository.findByDeviceOrderByInstallationDateAsc(device);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device)

Example 70 with Device

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

the class DeviceConverterTest method testSmartMeterConversion.

@Test
public void testSmartMeterConversion() throws UnknownHostException {
    final Device device = new SmartMeter("id", "alias", new Address("city", "postal", "street", 42, "nr", "munic"), new GpsCoordinates(12f, 13f));
    device.updateRegistrationData(InetAddress.getByName("localhost"), "type");
    final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device jaxbDevice = this.deviceManagementMapper.map(device, org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device.class);
    assertThat(jaxbDevice.getDeviceIdentification()).isEqualTo("id");
    assertThat(jaxbDevice.getAlias()).isEqualTo("alias");
    assertThat(jaxbDevice.getContainerAddress().getCity()).isEqualTo("city");
    assertThat(jaxbDevice.getContainerAddress().getPostalCode()).isEqualTo("postal");
    assertThat(jaxbDevice.getContainerAddress().getStreet()).isEqualTo("street");
    assertThat(jaxbDevice.getContainerAddress().getNumber()).isEqualTo(new Integer(42));
    assertThat(jaxbDevice.getContainerAddress().getNumberAddition()).isEqualTo("nr");
    assertThat(jaxbDevice.getContainerAddress().getMunicipality()).isEqualTo("munic");
    assertThat(jaxbDevice.getGpsLatitude()).isEqualTo("12.0");
    assertThat(jaxbDevice.getGpsLongitude()).isEqualTo("13.0");
    assertThat(jaxbDevice.getNetworkAddress()).isEqualTo("localhost/127.0.0.1");
    assertThat(jaxbDevice.getDeviceType()).isEqualTo("type");
    final SmartMeter mappedBack = this.deviceManagementMapper.map(jaxbDevice, SmartMeter.class);
    assertThat(mappedBack.getDeviceIdentification()).isEqualTo("id");
    assertThat(mappedBack.getAlias()).isEqualTo("alias");
    assertThat(mappedBack.getContainerAddress().getCity()).isEqualTo("city");
    assertThat(mappedBack.getContainerAddress().getPostalCode()).isEqualTo("postal");
    assertThat(mappedBack.getContainerAddress().getStreet()).isEqualTo("street");
    assertThat(mappedBack.getContainerAddress().getNumber()).isEqualTo(new Integer(42));
    assertThat(mappedBack.getContainerAddress().getNumberAddition()).isEqualTo("nr");
    assertThat(mappedBack.getContainerAddress().getMunicipality()).isEqualTo("munic");
    assertThat(mappedBack.getGpsCoordinates().getLatitude()).isEqualTo(12);
    assertThat(mappedBack.getGpsCoordinates().getLongitude()).isEqualTo(13);
    // alas networkaddress in jaxb device is just a string, need parsing to
    // convert that to InetAddress
    assertThat(mappedBack.getNetworkAddress()).isEqualTo(null);
    assertThat(mappedBack.getDeviceType()).isEqualTo("type");
}
Also used : Address(org.opensmartgridplatform.domain.core.valueobjects.Address) InetAddress(java.net.InetAddress) Device(org.opensmartgridplatform.domain.core.entities.Device) SmartMeter(org.opensmartgridplatform.domain.core.entities.SmartMeter) GpsCoordinates(org.opensmartgridplatform.domain.core.valueobjects.GpsCoordinates) Test(org.junit.jupiter.api.Test)

Aggregations

Device (org.opensmartgridplatform.domain.core.entities.Device)179 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)49 RequestMessage (org.opensmartgridplatform.shared.infra.jms.RequestMessage)36 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)35 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)32 Test (org.junit.jupiter.api.Test)27 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)27 Transactional (org.springframework.transaction.annotation.Transactional)24 Then (io.cucumber.java.en.Then)21 DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)18 SmartMeter (org.opensmartgridplatform.domain.core.entities.SmartMeter)17 CommonRequestMessage (org.opensmartgridplatform.adapter.ws.core.infra.jms.CommonRequestMessage)15 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)15 DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)12 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)12 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)11 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)11 Date (java.util.Date)10 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)10 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)9