Search in sources :

Example 1 with OslpLogItem

use of com.alliander.osgp.webdevicesimulator.domain.entities.OslpLogItem in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandler method messageReceived.

@Override
public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    final OslpEnvelope message = (OslpEnvelope) e.getMessage();
    this.oslpLogItemRepository.save(new OslpLogItem(message.getDeviceId(), this.getDeviceIdentificationFromMessage(message.getPayloadMessage()), true, message.getPayloadMessage()));
    if (message.isValid()) {
        if (this.isOslpResponse(message)) {
            LOGGER.info("Received OSLP Response (before callback): {}", message.getPayloadMessage());
            // Lookup correct callback and call handle method
            final Integer channelId = e.getChannel().getId();
            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 {
            LOGGER.info("Received OSLP Request: {}", message.getPayloadMessage().toString().split(" ")[0]);
            // Sequence number logic
            byte[] sequenceNumber = message.getSequenceNumber();
            Integer 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()));
            e.getChannel().write(response);
            LOGGER.info("Send OSLP Response: {}", response.getPayloadMessage().toString().split(" ")[0]);
        }
    } else {
        LOGGER.warn("Received message wasn't properly secured.");
    }
}
Also used : OslpLogItem(com.alliander.osgp.webdevicesimulator.domain.entities.OslpLogItem) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 2 with OslpLogItem

use of com.alliander.osgp.webdevicesimulator.domain.entities.OslpLogItem in project Protocol-Adapter-OSLP by OSGP.

the class RegisterDevice method writeOslpLogItem.

private void writeOslpLogItem(final OslpEnvelope oslpEnvelope, final Device device, final boolean incoming) {
    final OslpLogItem logItem = new OslpLogItem(oslpEnvelope.getDeviceId(), device.getDeviceIdentification(), incoming, oslpEnvelope.getPayloadMessage());
    this.oslpLogItemRepository.save(logItem);
}
Also used : OslpLogItem(com.alliander.osgp.webdevicesimulator.domain.entities.OslpLogItem)

Aggregations

OslpLogItem (com.alliander.osgp.webdevicesimulator.domain.entities.OslpLogItem)2 OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)1