Search in sources :

Example 1 with OutOfSequenceEvent

use of org.opensmartgridplatform.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent in project open-smart-grid-platform 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(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) OutOfSequenceEvent(org.opensmartgridplatform.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent) DeviceSimulatorException(org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Aggregations

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