Search in sources :

Example 6 with Iec60870Device

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device in project open-smart-grid-platform by OSGP.

the class ClientConnectionServiceTest method testGetConnectionShouldReturnExistingConnectionWhenInCache.

@Test
void testGetConnectionShouldReturnExistingConnectionWhenInCache() throws Exception {
    // Arrange
    final String deviceIdentification = "DA_DVC_1";
    final Iec60870Device device = Iec60870DeviceFactory.createDistributionAutomationDevice(deviceIdentification);
    when(this.iec60870DeviceRepository.findByDeviceIdentification(deviceIdentification)).thenReturn(Optional.of(device));
    final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(deviceIdentification);
    final ClientConnection expectedConnection = ClientConnectionFactory.forDevice(deviceIdentification);
    this.connectionCache.addConnection(deviceIdentification, expectedConnection);
    // Act
    final ClientConnection actualConnection = this.clientConnectionService.getConnection(requestMetadata);
    // Assert
    assertThat(actualConnection).isEqualTo(expectedConnection);
}
Also used : Iec60870Device(org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device) RequestMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata) Test(org.junit.jupiter.api.Test)

Example 7 with Iec60870Device

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device in project open-smart-grid-platform by OSGP.

the class Iec60870DeviceFactory method createDistributionAutomationDevice.

public static Iec60870Device createDistributionAutomationDevice(final String deviceIdentification) {
    final Iec60870Device device = new Iec60870Device(deviceIdentification, DeviceType.DISTRIBUTION_AUTOMATION_DEVICE);
    device.setCommonAddress(Integer.parseInt(DEFAULT_COMMON_ADDRESS));
    device.setPort(Integer.parseInt(DEFAULT_PORT));
    return device;
}
Also used : Iec60870Device(org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device)

Example 8 with Iec60870Device

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device in project open-smart-grid-platform by OSGP.

the class LightSensorDeviceResponseService method sendEvent.

private void sendEvent(final EventNotificationDto eventNotification, final Iec60870Device device, final ResponseMetadata responseMetadata) {
    final ResponseMetadata rm = new ResponseMetadata.Builder().withCorrelationUid(responseMetadata.getCorrelationUid()).withDeviceIdentification(device.getDeviceIdentification()).withDomainInfo(responseMetadata.getDomainInfo()).withMessageType(MessageType.EVENT_NOTIFICATION.name()).withOrganisationIdentification(responseMetadata.getOrganisationIdentification()).build();
    this.lightMeasurementService.sendEventNotification(eventNotification, rm);
}
Also used : ResponseMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ResponseMetadata)

Example 9 with Iec60870Device

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device in project open-smart-grid-platform by OSGP.

the class ClientConnectionService method createConnection.

private ClientConnection createConnection(final RequestMetadata requestMetadata) throws ConnectionFailureException {
    final String deviceIdentification = requestMetadata.getDeviceIdentification();
    final Iec60870Device device = this.getIec60870Device(deviceIdentification);
    final Iec60870Device connectionDevice = this.getConnectionDevice(device);
    final String connectionDeviceIdentification = device.getConnectionDeviceIdentification();
    final ConnectionParameters connectionParameters = this.createConnectionParameters(connectionDevice, requestMetadata.getIpAddress());
    final ResponseMetadata responseMetadata = ResponseMetadata.from(requestMetadata, connectionDeviceIdentification, connectionDevice.getDeviceType());
    final ClientConnectionEventListener eventListener = new ClientConnectionEventListener.Builder().withDeviceIdentification(connectionDeviceIdentification).withClientAsduHandlerRegistry(this.clientAsduHandlerRegistry).withClientConnectionCache(this.connectionCache).withLoggingService(this.loggingService).withLogItemFactory(this.logItemFactory).withResponseMetadata(responseMetadata).withResponseMetadataFactory(this.responseMetadataFactory).build();
    final ClientConnection newDeviceConnection = this.iec60870Client.connect(connectionParameters, eventListener);
    try {
        this.connectionCache.addConnection(connectionDeviceIdentification, newDeviceConnection);
    } catch (final ClientConnectionAlreadyInCacheException e) {
        LOGGER.warn("Client connection for device {} already exists. Closing new connection and returning existing connection", connectionDeviceIdentification);
        LOGGER.debug("Exception: ", e);
        newDeviceConnection.getConnection().close();
        return e.getClientConnection();
    }
    return newDeviceConnection;
}
Also used : ClientConnectionAlreadyInCacheException(org.opensmartgridplatform.adapter.protocol.iec60870.domain.exceptions.ClientConnectionAlreadyInCacheException) ConnectionParameters(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters) Iec60870Device(org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device) ResponseMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ResponseMetadata)

Example 10 with Iec60870Device

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device in project open-smart-grid-platform by OSGP.

the class ClientConnectionService method getConnection.

public ClientConnection getConnection(final RequestMetadata requestMetadata) throws ConnectionFailureException {
    final String deviceIdentification = requestMetadata.getDeviceIdentification();
    LOGGER.debug("Get connection called for device {}.", deviceIdentification);
    final Iec60870Device device = this.getIec60870Device(deviceIdentification);
    final String connectionDeviceIdentification = device.getConnectionDeviceIdentification();
    if (device.hasGatewayDevice()) {
        LOGGER.debug("Getting connection for device {} using gateway device {}.", deviceIdentification, connectionDeviceIdentification);
    }
    final ClientConnection cachedDeviceConnection = this.connectionCache.getConnection(connectionDeviceIdentification);
    if (cachedDeviceConnection != null) {
        LOGGER.info("Connection found in cache for device {}.", connectionDeviceIdentification);
        return cachedDeviceConnection;
    } else {
        LOGGER.info("No connection found in cache for device {}, creating new connection.", connectionDeviceIdentification);
        return this.createConnection(requestMetadata);
    }
}
Also used : Iec60870Device(org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device)

Aggregations

Iec60870Device (org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device)21 Test (org.junit.jupiter.api.Test)6 RequestMetadata (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata)4 ResponseMetadata (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ResponseMetadata)4 ConnectionEventListener (org.openmuc.j60870.ConnectionEventListener)3 ClientConnectionAlreadyInCacheException (org.opensmartgridplatform.adapter.protocol.iec60870.domain.exceptions.ClientConnectionAlreadyInCacheException)2 ConnectionParameters (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters)2 DomainInfo (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DomainInfo)2 MeasurementReportDto (org.opensmartgridplatform.dto.da.measurements.MeasurementReportDto)2 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)2 Given (io.cucumber.java.en.Given)1 When (io.cucumber.java.en.When)1 ClientConnectionEventListener (org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnectionEventListener)1 DeviceConnection (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection)1 DeviceType (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceType)1 LightSensorStatusDto (org.opensmartgridplatform.dto.valueobjects.LightSensorStatusDto)1 ConnectionFailureException (org.opensmartgridplatform.shared.exceptionhandling.ConnectionFailureException)1