Search in sources :

Example 1 with OslpLogItem

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.");
    }
}
Also used : OslpLogItem(org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem) ByteString(com.google.protobuf.ByteString)

Example 2 with OslpLogItem

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);
}
Also used : OslpLogItem(org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem) ByteString(com.google.protobuf.ByteString) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 3 with OslpLogItem

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();
}
Also used : RegisterDevice(org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) OslpLogItem(org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem) Test(org.junit.jupiter.api.Test)

Aggregations

OslpLogItem (org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem)3 ByteString (com.google.protobuf.ByteString)2 Channel (io.netty.channel.Channel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 Test (org.junit.jupiter.api.Test)1 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)1 Device (org.opensmartgridplatform.webdevicesimulator.domain.entities.Device)1 RegisterDevice (org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice)1