Search in sources :

Example 41 with OslpEnvelope

use of org.opensmartgridplatform.oslp.OslpEnvelope 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)

Example 42 with OslpEnvelope

use of org.opensmartgridplatform.oslp.OslpEnvelope in project open-smart-grid-platform by OSGP.

the class RegisterDevice method sendRequest.

private OslpEnvelope sendRequest(final Device device, final OslpEnvelope request) throws IOException, DeviceSimulatorException {
    // Original protocol port.
    int port = this.oslpPortClient;
    // Newer protocol port.
    if (device.getProtocol().equals(ProtocolType.OSLP_ELSTER.toString())) {
        port = this.oslpElsterPortClient;
    }
    // Attempt to send the request and receive response.
    LOGGER.info("Trying to send request: {}", request.getPayloadMessage());
    final OslpEnvelope response = this.oslpChannelHandler.send(new InetSocketAddress(this.oslpAddressServer, port), request, device.getDeviceIdentification());
    LOGGER.info("Received response: {}", response.getPayloadMessage());
    return response;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Aggregations

OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)42 IOException (java.io.IOException)28 UnsignedOslpEnvelopeDto (org.opensmartgridplatform.oslp.UnsignedOslpEnvelopeDto)22 DeviceResponseHandler (org.opensmartgridplatform.adapter.protocol.oslp.elster.device.DeviceResponseHandler)20 DeviceResponse (org.opensmartgridplatform.adapter.protocol.oslp.elster.device.DeviceResponse)18 DeviceRequest (org.opensmartgridplatform.adapter.protocol.oslp.elster.device.DeviceRequest)17 ByteString (com.google.protobuf.ByteString)7 Device (org.opensmartgridplatform.webdevicesimulator.domain.entities.Device)5 DeviceSimulatorException (org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException)4 Given (io.cucumber.java.en.Given)3 Channel (io.netty.channel.Channel)3 ChannelFuture (io.netty.channel.ChannelFuture)3 UnknownHostException (java.net.UnknownHostException)3 GetStatusDeviceRequest (org.opensmartgridplatform.adapter.protocol.oslp.elster.device.requests.GetStatusDeviceRequest)3 GetStatusDeviceResponse (org.opensmartgridplatform.adapter.protocol.oslp.elster.device.responses.GetStatusDeviceResponse)3 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)3 Oslp (org.opensmartgridplatform.oslp.Oslp)3 Message (org.opensmartgridplatform.oslp.Oslp.Message)3 SocketChannel (io.netty.channel.socket.SocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2