use of org.opensmartgridplatform.oslp.Oslp in project open-smart-grid-platform by OSGP.
the class MockOslpChannelHandler method channelRead0.
@Override
public void channelRead0(final ChannelHandlerContext ctx, final OslpEnvelope message) throws Exception {
if (message.isValid()) {
final String deviceUid = MockOslpChannelHandler.getDeviceUid(message);
final DeviceState deviceState = this.devicesContext.getDeviceState(deviceUid);
LOGGER.debug("Device {} received a message with sequence number {}", deviceUid, message.getSequenceNumber());
if (this.isOslpResponse(message)) {
LOGGER.debug("Device {} received an OSLP Response (before callback): {}", MockOslpChannelHandler.getDeviceUid(message), message.getPayloadMessage());
// Lookup correct callback and call handle method.
final String channelId = ctx.channel().id().asLongText();
final Callback callback = this.callbacks.remove(channelId);
if (callback == null) {
LOGGER.warn("Callback for channel {} does not longer exist, dropping response.", channelId);
return;
}
callback.handle(message);
} else {
final MessageType messageType = this.getMessageType(message.getPayloadMessage());
LOGGER.debug("Device {} received an OSLP Request of type {}", deviceUid, messageType);
if (deviceState.hasMockedResponses(messageType)) {
// Build the OslpEnvelope.
final OslpEnvelope.Builder responseBuilder = new OslpEnvelope.Builder().withSignature(this.oslpSignature).withProvider(this.oslpSignatureProvider).withPrimaryKey(this.privateKey).withDeviceId(message.getDeviceId()).withPayloadMessage(this.handleRequest(message));
// Add the new sequence number to the OslpEnvelope.
responseBuilder.withSequenceNumber(convertIntegerToByteArray(deviceState.getSequenceNumber()));
final OslpEnvelope response = responseBuilder.build();
LOGGER.debug("Device {} is sending an OSLP response with sequence number {}", MockOslpChannelHandler.getDeviceUid(response), convertByteArrayToInteger(response.getSequenceNumber()));
// wait for the response to actually be written. This
// improves stability of the tests
final ChannelFuture future = ctx.channel().writeAndFlush(response);
final InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
final InetSocketAddress clientAddress = new InetSocketAddress(remoteAddress.getAddress(), PlatformPubliclightingDefaults.OSLP_ELSTER_SERVER_PORT);
future.addListener((ChannelFutureListener) ChannelFutureListener -> {
Executors.newSingleThreadScheduledExecutor().schedule(() -> {
try {
MockOslpChannelHandler.this.sendNotifications(clientAddress, message, deviceUid, deviceState);
} catch (final DeviceSimulatorException | IOException e) {
LOGGER.info("Unable to send notifications", e);
}
}, 1000, TimeUnit.MILLISECONDS).get();
if (!ChannelFutureListener.isSuccess()) {
ChannelFutureListener.channel().close();
}
});
future.await();
LOGGER.debug("Sent OSLP response: {}", response.getPayloadMessage().toString().split(" ")[0]);
} else {
LOGGER.error("Device {} received a message of type {}, but no mocks are available, this test will fail!", deviceUid, messageType);
}
}
} else {
LOGGER.warn("Received message wasn't properly secured.");
}
}
use of org.opensmartgridplatform.oslp.Oslp in project open-smart-grid-platform by OSGP.
the class OslpDeviceSteps method theConfirmRegisterDeviceResponseContains.
/**
* Verify that we have received a confirm register device response over OSLP/OSLP ELSTER
*/
@Then("^the confirm register device response contains$")
public void theConfirmRegisterDeviceResponseContains(final Map<String, String> expectedResponse) {
final Exception e = (Exception) ScenarioContext.current().get("Error");
if (e == null || getString(expectedResponse, PlatformPubliclightingKeys.MESSAGE) == null) {
final Message responseMessage = this.oslpMockServer.waitForResponse();
final Oslp.ConfirmRegisterDeviceResponse response = responseMessage.getConfirmRegisterDeviceResponse();
Assert.assertNotNull(response);
assertThat(response.getStatus().name()).isEqualTo(getString(expectedResponse, PlatformPubliclightingKeys.KEY_STATUS));
} else {
assertThat(e).hasMessage(getString(expectedResponse, PlatformPubliclightingKeys.MESSAGE));
}
}
Aggregations