Search in sources :

Example 6 with ClientConnection

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

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

the class AbstractMessageProcessor method processMessage.

@Override
public void processMessage(final ObjectMessage message) {
    LOGGER.info("Processing message.");
    MessageMetadata messageMetadata = null;
    try {
        messageMetadata = MessageMetadata.fromMessage(message);
        final RequestMetadata requestMetadata = RequestMetadata.newBuilder().messageMetadata(messageMetadata).build();
        final ClientConnection deviceConnection = this.iec60870DeviceConnectionService.getConnection(requestMetadata);
        this.process(deviceConnection, requestMetadata);
    } catch (final ProtocolAdapterException e) {
        this.handleError(messageMetadata, e);
    } catch (final Exception e) {
        LOGGER.error("UNRECOVERABLE ERROR, unable to read ObjectMessage instance, giving up.", e);
    }
}
Also used : MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) ClientConnection(org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection) ProtocolAdapterException(org.opensmartgridplatform.shared.exceptionhandling.ProtocolAdapterException) ProtocolAdapterException(org.opensmartgridplatform.shared.exceptionhandling.ProtocolAdapterException) JMSException(javax.jms.JMSException) RequestMetadata(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.RequestMetadata)

Example 8 with ClientConnection

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

the class GeneralInterrogationService method sendGeneralInterrogation.

public void sendGeneralInterrogation(final ClientConnection deviceConnection, final RequestMetadata requestMetadata) throws IOException {
    final String connectedDevice = deviceConnection.getConnectionParameters().getDeviceIdentification();
    final int commonAddress = deviceConnection.getConnectionParameters().getCommonAddress();
    deviceConnection.getConnection().interrogation(commonAddress, CauseOfTransmission.ACTIVATION, new IeQualifierOfInterrogation(QUALIFIER_OF_INTERROGATION_ID));
    // interrogation command creates this asdu internally, however we
    // need it here as well for logging...
    final ASdu asdu = new ASdu(ASduType.C_IC_NA_1, false, CauseOfTransmission.ACTIVATION, false, false, ORIGINATOR_ADDRESS, commonAddress, new InformationObject(0, new IeQualifierOfInterrogation(QUALIFIER_OF_INTERROGATION_ID)));
    final LogItem logItem = new LogItem(connectedDevice, requestMetadata.getOrganisationIdentification(), false, asdu.toString());
    this.loggingService.log(logItem);
}
Also used : LogItem(org.opensmartgridplatform.adapter.protocol.iec60870.domain.valueobjects.LogItem) IeQualifierOfInterrogation(org.openmuc.j60870.ie.IeQualifierOfInterrogation) ASdu(org.openmuc.j60870.ASdu) InformationObject(org.openmuc.j60870.ie.InformationObject)

Example 9 with ClientConnection

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

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

the class ClientConnectionServiceTest method testGetConnectionShouldReturnNewConnectionWhenNotInCache.

@Test
void testGetConnectionShouldReturnNewConnectionWhenNotInCache() 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);
    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)

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