Search in sources :

Example 1 with MessageType

use of org.opensmartgridplatform.shared.infra.jms.MessageType 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 MessageType

use of org.opensmartgridplatform.shared.infra.jms.MessageType in project open-smart-grid-platform by OSGP.

the class OsgpCoreRequestMessageListener method onMessage.

@Override
public void onMessage(final Message message) {
    try {
        LOGGER.info("Received message of type: {}", message.getJMSType());
        final ObjectMessage objectMessage = (ObjectMessage) message;
        final MessageType messageType = MessageType.valueOf(objectMessage.getJMSType());
        final MessageProcessor messageProcessor = this.domainCoreDeviceRequestMessageProcessorMap.getMessageProcessor(messageType);
        messageProcessor.processMessage(objectMessage);
    } catch (final JMSException e) {
        LOGGER.error("Exception: {}", e.getMessage(), e);
    }
}
Also used : ObjectMessage(javax.jms.ObjectMessage) MessageProcessor(org.opensmartgridplatform.shared.infra.jms.MessageProcessor) JMSException(javax.jms.JMSException) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType)

Example 3 with MessageType

use of org.opensmartgridplatform.shared.infra.jms.MessageType in project open-smart-grid-platform by OSGP.

the class OslpSigningService method handleSignedOslpRequest.

private void handleSignedOslpRequest(final SignedOslpEnvelopeDto signedOslpEnvelopeDto, final String deviceIdentification) {
    final OslpEnvelope oslpEnvelope = signedOslpEnvelopeDto.getOslpEnvelope();
    final UnsignedOslpEnvelopeDto unsignedOslpEnvelopeDto = signedOslpEnvelopeDto.getUnsignedOslpEnvelopeDto();
    // Handle OSLP request message.
    LOGGER.debug(LINES);
    LOGGER.info("oslpEnvelope.size: {} for message type: {}", oslpEnvelope.getSize(), unsignedOslpEnvelopeDto.getMessageType());
    LOGGER.debug(LINES);
    LOGGER.debug("unsignedOslpEnvelopeDto.getCorrelationUid() : {}", unsignedOslpEnvelopeDto.getCorrelationUid());
    LOGGER.debug("unsignedOslpEnvelopeDto.getDeviceId() : {}", unsignedOslpEnvelopeDto.getDeviceId());
    LOGGER.debug("unsignedOslpEnvelopeDto.getDomain() : {}", unsignedOslpEnvelopeDto.getDomain());
    LOGGER.debug("unsignedOslpEnvelopeDto.getDomainVersion() : {}", unsignedOslpEnvelopeDto.getDomainVersion());
    LOGGER.debug("unsignedOslpEnvelopeDto.getIpAddress() : {}", unsignedOslpEnvelopeDto.getIpAddress());
    LOGGER.debug("unsignedOslpEnvelopeDto.getMessageType() : {}", unsignedOslpEnvelopeDto.getMessageType());
    LOGGER.debug("unsignedOslpEnvelopeDto.getMessagePriority() : {}", unsignedOslpEnvelopeDto.getMessagePriority());
    LOGGER.debug("unsignedOslpEnvelopeDto.getOrganisationIdentification() : {}", unsignedOslpEnvelopeDto.getOrganisationIdentification());
    LOGGER.debug("unsignedOslpEnvelopeDto.getPayloadMessage() : {}", unsignedOslpEnvelopeDto.getPayloadMessage().toString());
    LOGGER.debug("unsignedOslpEnvelopeDto.getRetryCount() : {}", unsignedOslpEnvelopeDto.getRetryCount());
    LOGGER.debug("unsignedOslpEnvelopeDto.getSequenceNumber() : {}", unsignedOslpEnvelopeDto.getSequenceNumber());
    LOGGER.debug("unsignedOslpEnvelopeDto.isScheduled() : {}", unsignedOslpEnvelopeDto.isScheduled());
    LOGGER.debug(LINES);
    final MessageType messageType = MessageType.valueOf(unsignedOslpEnvelopeDto.getMessageType());
    // Handle message for message type.
    final OslpEnvelopeProcessor messageProcessor = this.deviceRequestMessageProcessorMap.getOslpEnvelopeProcessor(messageType);
    if (messageProcessor == null) {
        LOGGER.error("No message processor for messageType: {}", unsignedOslpEnvelopeDto.getMessageType());
        return;
    }
    messageProcessor.processSignedOslpEnvelope(deviceIdentification, signedOslpEnvelopeDto);
}
Also used : OslpEnvelopeProcessor(org.opensmartgridplatform.adapter.protocol.oslp.elster.infra.messaging.OslpEnvelopeProcessor) UnsignedOslpEnvelopeDto(org.opensmartgridplatform.oslp.UnsignedOslpEnvelopeDto) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 4 with MessageType

use of org.opensmartgridplatform.shared.infra.jms.MessageType in project open-smart-grid-platform by OSGP.

the class GetDataResponseMessageProcessorTest method handlesResponseMessageValuesTest.

@Test
void handlesResponseMessageValuesTest() throws JMSException {
    // Arrange
    final String correlationUid = "CorrelationID-1";
    final MessageType messageType = MessageType.GET_DATA;
    final String organisationIdentification = "test-org";
    final String deviceIdentification = "device1";
    final ResponseMessage responseMessage = new ResponseMessage.Builder().withCorrelationUid(correlationUid).withMessageType(messageType.toString()).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.OK).withDataObject("the payload").build();
    when(this.receivedMessage.getObject()).thenReturn(responseMessage);
    // Act
    this.getDataResponseMessageProcessor.processMessage(this.receivedMessage);
    // Assert
    verify(this.adHocManagementService, times(1)).handleGetDataResponse(responseMessage, messageType);
}
Also used : ResponseMessage(org.opensmartgridplatform.shared.infra.jms.ResponseMessage) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) Test(org.junit.jupiter.api.Test)

Example 5 with MessageType

use of org.opensmartgridplatform.shared.infra.jms.MessageType in project open-smart-grid-platform by OSGP.

the class DomainResponseMessageProcessor method processMessage.

@Override
public void processMessage(final ObjectMessage message) {
    LOGGER.debug("Processing distribution automation response message");
    String correlationUid = null;
    MessageType messageType = null;
    String organisationIdentification = null;
    String deviceIdentification = null;
    ResponseMessageResultType resultType;
    String resultDescription;
    ResponseMessage dataObject;
    try {
        correlationUid = message.getJMSCorrelationID();
        organisationIdentification = message.getStringProperty(Constants.ORGANISATION_IDENTIFICATION);
        deviceIdentification = message.getStringProperty(Constants.DEVICE_IDENTIFICATION);
        messageType = MessageType.valueOf(message.getJMSType());
        resultType = ResponseMessageResultType.valueOf(message.getStringProperty(Constants.RESULT));
        resultDescription = message.getStringProperty(Constants.DESCRIPTION);
        dataObject = (ResponseMessage) message.getObject();
    } catch (final IllegalArgumentException e) {
        LOGGER.error("UNRECOVERABLE ERROR, received messageType {} is unknown.", messageType, e);
        logDebugInformation(messageType, correlationUid, organisationIdentification, deviceIdentification);
        return;
    } catch (final JMSException e) {
        LOGGER.error("UNRECOVERABLE ERROR, unable to read ObjectMessage instance, giving up.", e);
        logDebugInformation(messageType, correlationUid, organisationIdentification, deviceIdentification);
        return;
    }
    try {
        LOGGER.info("Handle message of type {} for device {} with result: {}, description {}.", messageType, deviceIdentification, resultType, resultDescription);
        this.handleMessage(messageType, dataObject);
    } catch (final RuntimeException e) {
        handleError(e, correlationUid);
    }
}
Also used : JMSException(javax.jms.JMSException) ResponseMessage(org.opensmartgridplatform.shared.infra.jms.ResponseMessage) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType)

Aggregations

MessageType (org.opensmartgridplatform.shared.infra.jms.MessageType)7 Oslp (org.opensmartgridplatform.oslp.Oslp)3 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)3 ByteString (com.google.protobuf.ByteString)2 JMSException (javax.jms.JMSException)2 Message (org.opensmartgridplatform.oslp.Oslp.Message)2 ResponseMessage (org.opensmartgridplatform.shared.infra.jms.ResponseMessage)2 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 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 PrivateKey (java.security.PrivateKey)1 ParseException (java.text.ParseException)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1