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