Search in sources :

Example 1 with OutOfSequenceEvent

use of com.alliander.osgp.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent in project Protocol-Adapter-OSLP by OSGP.

the class RegisterDevice method sendConfirmDeviceRegistrationCommand.

public DeviceMessageStatus sendConfirmDeviceRegistrationCommand(final long deviceId) {
    // Find device.
    Device device = this.deviceManagementService.findDevice(deviceId);
    if (device == null) {
        // Set the DeviceMessageStatus NOT_FOUND as the device is not found.
        return DeviceMessageStatus.NOT_FOUND;
    }
    this.errorMessage = "";
    try {
        final Integer sequenceNumber = device.doGetNextSequence();
        // Create registration confirmation message.
        final OslpEnvelope oslpRequest = this.createEnvelopeBuilder(device.getDeviceUid(), sequenceNumber).withPayloadMessage(Message.newBuilder().setConfirmRegisterDeviceRequest(Oslp.ConfirmRegisterDeviceRequest.newBuilder().setRandomDevice(device.getRandomDevice()).setRandomPlatform(device.getRandomPlatform())).build()).build();
        // Write outgoing request to log.
        this.writeOslpLogItem(oslpRequest, device, false);
        final OslpEnvelope response = this.sendRequest(device, oslpRequest);
        // Write incoming response to log.
        this.writeOslpLogItem(response, device, true);
        // Get the sequence number from the response envelope and check it.
        this.checkSequenceNumber(response.getSequenceNumber(), sequenceNumber);
        // Get the two random numbers and check them both.
        this.checkRandomDeviceAndRandomPlatform(device.getRandomDevice(), response.getPayloadMessage().getConfirmRegisterDeviceResponse().getRandomDevice(), device.getRandomPlatform(), response.getPayloadMessage().getConfirmRegisterDeviceResponse().getRandomPlatform());
        // Successful.
        device.setSequenceNumber(sequenceNumber);
        device = this.deviceManagementService.updateDevice(device);
        // Check if there has been an out of sequence security event.
        OutOfSequenceEvent outOfSequenceEvent = this.oslpChannelHandler.hasOutOfSequenceEventForDevice(device.getId());
        while (outOfSequenceEvent != null) {
            // An event has occurred, send
            // SECURITY_EVENTS_OUT_OF_SEQUENCE_VALUE event notification.
            this.sendEventNotificationCommand(outOfSequenceEvent.getDeviceId(), Oslp.Event.SECURITY_EVENTS_OUT_OF_SEQUENCE_VALUE, "out of sequence event occurred at time stamp: " + outOfSequenceEvent.getTimestamp().toString() + " for request: " + outOfSequenceEvent.getRequest(), null);
            // Check if there has been another event, this will return null
            // if no more events are present in the list.
            outOfSequenceEvent = this.oslpChannelHandler.hasOutOfSequenceEventForDevice(device.getId());
        }
        // successful.
        return DeviceMessageStatus.OK;
    } catch (final Exception e) {
        LOGGER.error("confirm device registration exception", e);
        this.errorMessage = e.getMessage();
        // is NOT successful.
        return DeviceMessageStatus.FAILURE;
    }
}
Also used : Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) OutOfSequenceEvent(com.alliander.osgp.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DeviceSimulatorException(com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Aggregations

OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)1 Device (com.alliander.osgp.webdevicesimulator.domain.entities.Device)1 DeviceSimulatorException (com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException)1 OutOfSequenceEvent (com.alliander.osgp.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1