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;
}
}
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;
}
Aggregations