Search in sources :

Example 1 with Oslp

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.");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) Sharable(io.netty.channel.ChannelHandler.Sharable) Base64(org.apache.commons.codec.binary.Base64) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LIGHT(org.opensmartgridplatform.oslp.Oslp.RelayType.LIGHT) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ParseException(java.text.ParseException) Message(org.opensmartgridplatform.oslp.Oslp.Message) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) TARIFF(org.opensmartgridplatform.oslp.Oslp.RelayType.TARIFF) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) ChannelFuture(io.netty.channel.ChannelFuture) ByteString(com.google.protobuf.ByteString) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) PlatformPubliclightingDefaults(org.opensmartgridplatform.cucumber.platform.publiclighting.PlatformPubliclightingDefaults) PrivateKey(java.security.PrivateKey) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DateTimeFormatter(java.time.format.DateTimeFormatter) Oslp(org.opensmartgridplatform.oslp.Oslp) InetSocketAddress(java.net.InetSocketAddress) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 2 with Oslp

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));
    }
}
Also used : Message(org.opensmartgridplatform.oslp.Oslp.Message) Oslp(org.opensmartgridplatform.oslp.Oslp) ParseException(java.text.ParseException) DeviceSimulatorException(org.opensmartgridplatform.cucumber.platform.publiclighting.mocks.oslpdevice.DeviceSimulatorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Then(io.cucumber.java.en.Then)

Aggregations

IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 Oslp (org.opensmartgridplatform.oslp.Oslp)2 Message (org.opensmartgridplatform.oslp.Oslp.Message)2 ByteString (com.google.protobuf.ByteString)1 Then (io.cucumber.java.en.Then)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 Sharable (io.netty.channel.ChannelHandler.Sharable)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 PrivateKey (java.security.PrivateKey)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1