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;
}
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);
}
}
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);
}
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);
}
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());
}
Aggregations