Search in sources :

Example 1 with ConnectionParameters

use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters 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 2 with ConnectionParameters

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

the class Iec60870Client method connect.

@Override
public ClientConnection connect(final ConnectionParameters connectionParameters, final ConnectionEventListener asduListener) throws ConnectionFailureException {
    final InetAddress address = this.convertIpAddress(connectionParameters.getIpAddress());
    final String deviceIdentification = connectionParameters.getDeviceIdentification();
    final int port = connectionParameters.getPort() == null ? IEC60870_DEFAULT_PORT : connectionParameters.getPort();
    final ClientConnectionBuilder clientConnectionBuilder = new ClientConnectionBuilder(address).setPort(port).setConnectionTimeout(this.connectionTimeout);
    try {
        LOGGER.info("Connecting to device: {}...", deviceIdentification);
        final Connection connection = clientConnectionBuilder.build();
        connection.startDataTransfer(asduListener);
        LOGGER.info("Connected to device: {}", deviceIdentification);
        return new DeviceConnection(connection, connectionParameters);
    } catch (final IOException e) {
        final String errorMessage = "Unable to connect to remote host: " + connectionParameters.getIpAddress();
        LOGGER.error(errorMessage, e);
        throw new ConnectionFailureException(ComponentType.PROTOCOL_IEC60870, errorMessage);
    }
}
Also used : ClientConnectionBuilder(org.openmuc.j60870.ClientConnectionBuilder) ConnectionFailureException(org.opensmartgridplatform.shared.exceptionhandling.ConnectionFailureException) ClientConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection) Connection(org.openmuc.j60870.Connection) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 3 with ConnectionParameters

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

the class GetLightSensorStatusRequestMessageProcessorTest method testProcessShouldSendGeneralInInterrogation.

@Test
void testProcessShouldSendGeneralInInterrogation() throws Exception {
    // Arrange
    final ConnectionParameters connectionParameters = ConnectionParameters.newBuilder().deviceIdentification(GATEWAY_IDENTIFICATION).build();
    final DeviceConnection deviceConnection = new DeviceConnection(this.connection, connectionParameters);
    final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(DEVICE_IDENTIFICATION);
    // Act
    this.getLightSensorStatusRequestMessageProcessor.process(deviceConnection, requestMetadata);
    // Assert
    verify(this.generalInterrogationService).sendGeneralInterrogation(deviceConnection, requestMetadata);
}
Also used : ConnectionParameters(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection) RequestMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionParameters

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

the class ClientConnectionFactory method forDevice.

public static ClientConnection forDevice(final String deviceIdentification) {
    final Connection connection = Mockito.mock(Connection.class);
    final ConnectionParameters connectionParameters = new ConnectionParameters.Builder().deviceIdentification(deviceIdentification).build();
    return new DeviceConnection(connection, connectionParameters);
}
Also used : ClientConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection) Connection(org.openmuc.j60870.Connection) ConnectionParameters(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection)

Example 5 with ConnectionParameters

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

the class GeneralInterrogationServiceTest method testSendGeneralInterrogationShouldLogSameAsduAsUsedInInterrogation.

/**
 * Test method for {@link
 * org.opensmartgridplatform.adapter.protocol.iec60870.infra.messaging.processors.ConnectRequestMessageProcessor#process(org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection,
 * org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata)}.
 *
 * @throws Exception
 */
@Test
void testSendGeneralInterrogationShouldLogSameAsduAsUsedInInterrogation() throws Exception {
    // Arrange
    final ConnectionParameters connectionParameters = ConnectionParameters.newBuilder().deviceIdentification(DEVICE_IDENTIFICATION).build();
    final DeviceConnection deviceConnection = new DeviceConnection(this.connection, connectionParameters);
    final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(DEVICE_IDENTIFICATION);
    doCallRealMethod().when(this.connection).interrogation(anyInt(), any(CauseOfTransmission.class), any(IeQualifierOfInterrogation.class));
    // Act
    this.generalInterrogationService.sendGeneralInterrogation(deviceConnection, requestMetadata);
    // Assert
    final ArgumentCaptor<ASdu> asduCaptor = ArgumentCaptor.forClass(ASdu.class);
    final ArgumentCaptor<LogItem> logItemCaptor = ArgumentCaptor.forClass(LogItem.class);
    verify(this.connection).send(asduCaptor.capture());
    verify(this.loggingService).log(logItemCaptor.capture());
    assertThat(logItemCaptor.getValue().getMessage()).isEqualTo(asduCaptor.getValue().toString());
}
Also used : CauseOfTransmission(org.openmuc.j60870.CauseOfTransmission) LogItem(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.LogItem) ConnectionParameters(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters) DeviceConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection) IeQualifierOfInterrogation(org.openmuc.j60870.ie.IeQualifierOfInterrogation) ASdu(org.openmuc.j60870.ASdu) RequestMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionParameters (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.ConnectionParameters)5 DeviceConnection (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.DeviceConnection)5 Test (org.junit.jupiter.api.Test)3 RequestMetadata (org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata)3 Connection (org.openmuc.j60870.Connection)2 ClientConnection (org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ASdu (org.openmuc.j60870.ASdu)1 CauseOfTransmission (org.openmuc.j60870.CauseOfTransmission)1 ClientConnectionBuilder (org.openmuc.j60870.ClientConnectionBuilder)1 IeQualifierOfInterrogation (org.openmuc.j60870.ie.IeQualifierOfInterrogation)1 Iec60870Device (org.opensmartgridplatform.adapter.protocol.iec60870.domain.entities.Iec60870Device)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