use of org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem in project open-smart-grid-platform by OSGP.
the class OslpChannelHandler method channelRead0.
@Override
public void channelRead0(final ChannelHandlerContext ctx, final OslpEnvelope message) throws Exception {
final String deviceUid = Base64.encodeBase64String(message.getDeviceId());
if (this.isRebooting(message.getDeviceId())) {
LOGGER.warn("Disconnect device {}, simulating unavailability while rebooting", deviceUid);
ctx.disconnect();
return;
} else {
LOGGER.info("Process envelope for device {}, not rebooting", deviceUid);
}
this.oslpLogItemRepository.save(new OslpLogItem(message.getDeviceId(), this.getDeviceIdentificationFromMessage(message.getPayloadMessage()), true, message.getPayloadMessage()));
if (message.isValid()) {
if (this.isOslpResponse(message)) {
this.channelRead0OslpReponse(ctx, message);
} else {
this.channelRead0OslpRequest(ctx, message);
}
} else {
LOGGER.warn("Received message wasn't properly secured.");
}
}
use of org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem in project open-smart-grid-platform by OSGP.
the class OslpChannelHandler method channelRead0OslpRequest.
private void channelRead0OslpRequest(final ChannelHandlerContext ctx, final OslpEnvelope message) throws DeviceSimulatorException {
final String oslpRequest = message.getPayloadMessage().toString().split(" ")[0];
LOGGER.info("Received OSLP Request: {}", oslpRequest);
// Sequence number logic
byte[] sequenceNumber = message.getSequenceNumber();
int number = -1;
if (!(message.getPayloadMessage().hasRegisterDeviceRequest() || message.getPayloadMessage().hasConfirmRegisterDeviceRequest())) {
// Convert byte array to integer
number = this.convertByteArrayToInteger(sequenceNumber);
// increment
if (number >= this.sequenceNumberMaximum) {
LOGGER.info("wrapping sequence number back to 0, current sequence number: {} next sequence number: 0", number);
number = 0;
} else {
LOGGER.info("incrementing sequence number, current sequence number: {} next sequence number: {}", number, number + 1);
number += 1;
}
// Convert integer back to byte array
sequenceNumber = this.convertIntegerToByteArray(number);
}
final byte[] deviceId = message.getDeviceId();
// Build the OslpEnvelope with the incremented sequence number.
final OslpEnvelope.Builder responseBuilder = new OslpEnvelope.Builder().withSignature(this.oslpSignature).withProvider(this.oslpSignatureProvider).withPrimaryKey(this.privateKey).withDeviceId(deviceId).withSequenceNumber(sequenceNumber);
// Pass the incremented sequence number to the handleRequest()
// function for checking.
responseBuilder.withPayloadMessage(this.handleRequest(message, number));
final OslpEnvelope response = responseBuilder.build();
this.oslpLogItemRepository.save(new OslpLogItem(response.getDeviceId(), this.getDeviceIdentificationFromMessage(response.getPayloadMessage()), false, response.getPayloadMessage()));
LOGGER.info("sending OSLP response with sequence number: {}", this.convertByteArrayToInteger(response.getSequenceNumber()));
ctx.channel().writeAndFlush(response);
final String oslpResponse = response.getPayloadMessage().toString().split(" ")[0];
LOGGER.info("Sent OSLP Response: {}", oslpResponse);
}
use of org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem in project open-smart-grid-platform by OSGP.
the class DeviceSimulatorIT method savesOslpLogItemsForRequestAndResponse.
@Test
void savesOslpLogItemsForRequestAndResponse() throws Exception {
final long id = DEVICE_ID.incrementAndGet();
final int sequenceNumber = this.aSequenceNumber();
final byte[] deviceUid = this.aDeviceUid();
final Device device = this.aDevice(id, String.format("TST-%03d", id), deviceUid, sequenceNumber);
when(this.deviceManagementService.findDevice(device.getDeviceUid())).thenReturn(device);
final Channel channel = this.activeChannelToSimulator();
channel.writeAndFlush(this.setRebootEnvelope(deviceUid, sequenceNumber));
channel.closeFuture().awaitUninterruptibly(1000 * (REBOOT_DELAY_IN_SECONDS + 1));
verify(this.oslpLogItemRepository, times(2)).save(this.oslpLogItemCaptor.capture());
final List<OslpLogItem> savedOslpLogItems = this.oslpLogItemCaptor.getAllValues();
savedOslpLogItems.forEach(savedOslpLogItem -> {
assertThat(savedOslpLogItem.getDeviceUid()).isEqualTo(device.getDeviceUid());
});
assertThat(savedOslpLogItems.get(0).isIncoming()).isTrue();
assertThat(savedOslpLogItems.get(1).isIncoming()).isFalse();
}
Aggregations