Search in sources :

Example 1 with ClientConnection

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

the class ClientConnectionServiceTest method testGetConnectionShouldReturnExistingConnectionToGatewayDeviceWhenInCache.

@Test
void testGetConnectionShouldReturnExistingConnectionToGatewayDeviceWhenInCache() throws Exception {
    // Arrange
    final String deviceIdentification = "LM_DVC_1";
    final String gatewayDeviceIdentification = "LM_GATEWAY_1";
    final Iec60870Device device = Iec60870DeviceFactory.createLightMeasurementDevice(deviceIdentification, gatewayDeviceIdentification);
    when(this.iec60870DeviceRepository.findByDeviceIdentification(deviceIdentification)).thenReturn(Optional.of(device));
    final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(deviceIdentification);
    final ClientConnection expectedConnection = ClientConnectionFactory.forDevice(gatewayDeviceIdentification);
    this.connectionCache.addConnection(gatewayDeviceIdentification, 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 2 with ClientConnection

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

the class ClientConnectionServiceTest method testGetConnectionShouldReturnNewConnectionToGatewayDeviceWhenNotInCache.

@Test
void testGetConnectionShouldReturnNewConnectionToGatewayDeviceWhenNotInCache() throws Exception {
    // Arrange
    final String deviceIdentification = "LM_DVC_1";
    final String gatewayDeviceIdentification = "LM_GATEWAY_1";
    final Iec60870Device device = Iec60870DeviceFactory.createLightMeasurementDevice(deviceIdentification, gatewayDeviceIdentification);
    final Iec60870Device gateway = Iec60870DeviceFactory.createLightMeasurementGatewayDevice(gatewayDeviceIdentification);
    when(this.iec60870DeviceRepository.findByDeviceIdentification(deviceIdentification)).thenReturn(Optional.of(device));
    when(this.iec60870DeviceRepository.findByDeviceIdentification(gatewayDeviceIdentification)).thenReturn(Optional.of(gateway));
    final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(deviceIdentification);
    final ClientConnection expectedConnection = ClientConnectionFactory.forDevice(gatewayDeviceIdentification);
    when(this.iec60870Client.connect(eq(expectedConnection.getConnectionParameters()), any(ConnectionEventListener.class))).thenReturn(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) ConnectionEventListener(org.openmuc.j60870.ConnectionEventListener) Test(org.junit.jupiter.api.Test)

Example 3 with ClientConnection

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection 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 4 with ClientConnection

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection 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 5 with ClientConnection

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection 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)6 RequestMetadata (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata)5 Test (org.junit.jupiter.api.Test)4 ClientConnection (org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection)3 Connection (org.openmuc.j60870.Connection)2 ConnectionEventListener (org.openmuc.j60870.ConnectionEventListener)2 ConnectionParameters (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters)2 DeviceConnection (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 JMSException (javax.jms.JMSException)1 ASdu (org.openmuc.j60870.ASdu)1 ClientConnectionBuilder (org.openmuc.j60870.ClientConnectionBuilder)1 IeQualifierOfInterrogation (org.openmuc.j60870.ie.IeQualifierOfInterrogation)1 InformationObject (org.openmuc.j60870.ie.InformationObject)1 ClientConnectionAlreadyInCacheException (org.opensmartgridplatform.adapter.protocol.iec60870.domain.exceptions.ClientConnectionAlreadyInCacheException)1 LogItem (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.LogItem)1 ResponseMetadata (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ResponseMetadata)1 ConnectionFailureException (org.opensmartgridplatform.shared.exceptionhandling.ConnectionFailureException)1 ProtocolAdapterException (org.opensmartgridplatform.shared.exceptionhandling.ProtocolAdapterException)1