Search in sources :

Example 6 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class SigningService method doSignMessage.

private void doSignMessage(final UnsignedOslpEnvelopeDto unsignedOslpEnvelopeDto, final String correlationUid, final String deviceIdentification, final Destination replyToQueue) {
    final byte[] deviceId = unsignedOslpEnvelopeDto.getDeviceId();
    final byte[] sequenceNumber = unsignedOslpEnvelopeDto.getSequenceNumber();
    final Message payloadMessage = unsignedOslpEnvelopeDto.getPayloadMessage();
    final String organisationIdentification = unsignedOslpEnvelopeDto.getOrganisationIdentification();
    final OslpEnvelope oslpEnvelope = new OslpEnvelope.Builder().withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPrimaryKey(this.privateKey).withSignature(this.signature).withProvider(this.signatureProvider).withPayloadMessage(payloadMessage).build();
    ResponseMessage responseMessage = null;
    if (oslpEnvelope == null) {
        LOGGER.error("Message for device: {} with correlationId: {} NOT SIGNED, sending error to protocol-adpater", deviceIdentification, correlationUid);
        responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.NOT_OK).withOsgpException(new OsgpException(ComponentType.UNKNOWN, "Failed to build signed OslpEnvelope", null)).withDataObject(unsignedOslpEnvelopeDto).build();
    } else {
        LOGGER.info("Message for device: {} with correlationId: {} signed, sending response to protocol-adapter", deviceIdentification, correlationUid);
        final SignedOslpEnvelopeDto signedOslpEnvelopeDto = new SignedOslpEnvelopeDto(oslpEnvelope, unsignedOslpEnvelopeDto);
        responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.OK).withDataObject(signedOslpEnvelopeDto).build();
    }
    this.signingServerResponseMessageSender.send(responseMessage, "SIGNING_RESPONSE", replyToQueue);
}
Also used : OsgpException(com.alliander.osgp.shared.exceptionhandling.OsgpException) Message(com.alliander.osgp.oslp.Oslp.Message) ResponseMessage(com.alliander.osgp.shared.infra.jms.ResponseMessage) SignedOslpEnvelopeDto(com.alliander.osgp.oslp.SignedOslpEnvelopeDto) ResponseMessage(com.alliander.osgp.shared.infra.jms.ResponseMessage) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 7 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandler method handleRequest.

private Oslp.Message handleRequest(final OslpEnvelope message, final int sequenceNumber) throws DeviceSimulatorException, IOException, ParseException {
    final Oslp.Message request = message.getPayloadMessage();
    // Create response message
    Oslp.Message response = null;
    final String deviceIdString = Base64.encodeBase64String(message.getDeviceId());
    LOGGER.info("request received, sequenceNumber: {}", sequenceNumber);
    LOGGER.info("manufacturerId byte[0]: {} byte[1]: {}", message.getDeviceId()[0], message.getDeviceId()[1]);
    LOGGER.info("deviceId as BASE 64 STRING: {}", deviceIdString);
    // lookup correct device.
    final Device device = this.deviceManagementService.findDevice(deviceIdString);
    if (device == null) {
        throw new DeviceSimulatorException("device with id: " + deviceIdString + " is unknown");
    }
    // Calculate expected sequence number
    final Integer expectedSequenceNumber = device.doGetNextSequence();
    // Check sequence number
    if (Math.abs(expectedSequenceNumber - sequenceNumber) > this.sequenceNumberWindow) {
        this.outOfSequenceList.add(new OutOfSequenceEvent(device.getId(), message.getPayloadMessage().toString(), DateTime.now()));
        throw new DeviceSimulatorException("SequenceNumber incorrect for device: " + device.getDeviceIdentification() + " Expected: " + (expectedSequenceNumber == 0 ? this.sequenceNumberMaximum : expectedSequenceNumber - 1) + " Actual: " + (sequenceNumber == 0 ? this.sequenceNumberMaximum : sequenceNumber - 1) + " SequenceNumberWindow: " + this.sequenceNumberWindow + " Request: " + message.getPayloadMessage().toString());
    }
    // sleep for a little while
    if (this.responseDelayTime != null && this.reponseDelayRandomRange == null) {
        this.sleep(this.responseDelayTime);
    } else if (this.responseDelayTime != null && this.reponseDelayRandomRange != null) {
        final Long randomDelay = (long) (this.reponseDelayRandomRange * this.random.nextDouble());
        this.sleep(this.responseDelayTime + randomDelay);
    }
    // Handle only expected messages
    if (request.hasStartSelfTestRequest()) {
        device.setLightOn(true);
        device.setSelftestActive(true);
        response = createStartSelfTestResponse();
    } else if (request.hasStopSelfTestRequest()) {
        device.setLightOn(false);
        device.setSelftestActive(false);
        response = createStopSelfTestResponse();
    } else if (request.hasSetLightRequest()) {
        this.handleSetLightRequest(device, request.getSetLightRequest());
        response = createSetLightResponse();
    } else if (request.hasSetEventNotificationsRequest()) {
        this.handleSetEventNotificationsRequest(device, request.getSetEventNotificationsRequest());
        response = createSetEventNotificationsResponse();
    } else if (request.hasUpdateFirmwareRequest()) {
        this.handleUpdateFirmwareRequest(device, request.getUpdateFirmwareRequest());
        response = createUpdateFirmwareResponse();
    } else if (request.hasGetFirmwareVersionRequest()) {
        response = createGetFirmwareVersionResponse(this.firmwareVersion);
    } else if (request.hasSwitchFirmwareRequest()) {
        response = createSwitchFirmwareResponse();
    } else if (request.hasUpdateDeviceSslCertificationRequest()) {
        response = createUpdateDeviceSslCertificationResponse();
    } else if (request.hasSetDeviceVerificationKeyRequest()) {
        response = createSetDeviceVerificationKeyResponse();
    } else if (request.hasSetScheduleRequest()) {
        this.handleSetScheduleRequest(device, request.getSetScheduleRequest());
        response = createSetScheduleResponse();
    } else if (request.hasSetConfigurationRequest()) {
        this.handleSetConfigurationRequest(device, request.getSetConfigurationRequest());
        response = this.createSetConfigurationResponse();
    } else if (request.hasGetConfigurationRequest()) {
        this.handleGetConfigurationRequest(device, request.getGetConfigurationRequest());
        response = this.createGetConfigurationResponse(device);
    } else if (request.hasSwitchConfigurationRequest()) {
        response = createSwitchConfigurationResponse();
    } else if (request.hasGetActualPowerUsageRequest()) {
        this.handleGetActualPowerUsageRequest(device, request.getGetActualPowerUsageRequest());
        response = createGetActualPowerUsageResponse();
    } else if (request.hasGetPowerUsageHistoryRequest()) {
        this.handleGetPowerUsageHistoryRequest(device, request.getGetPowerUsageHistoryRequest());
        response = createGetPowerUsageHistoryWithDatesResponse(request.getGetPowerUsageHistoryRequest());
    } else if (request.hasGetStatusRequest()) {
        response = this.createGetStatusResponse(device);
    } else if (request.hasResumeScheduleRequest()) {
        response = createResumeScheduleResponse();
    } else if (request.hasSetRebootRequest()) {
        response = createSetRebootResponse();
        this.sendDelayedDeviceRegistration(device);
    } else if (request.hasSetTransitionRequest()) {
        this.handleSetTransitionRequest(device, request.getSetTransitionRequest());
        response = createSetTransitionResponse();
    } else if (request.hasConfirmRegisterDeviceRequest()) {
        response = createConfirmRegisterDeviceResponse(request.getConfirmRegisterDeviceRequest().getRandomDevice(), request.getConfirmRegisterDeviceRequest().getRandomPlatform());
    } else {
        // Handle errors by logging
        LOGGER.error("Did not expect request, ignoring: " + request.toString());
    }
    // Update device
    device.setSequenceNumber(expectedSequenceNumber);
    this.deviceManagementService.updateDevice(device);
    // Write log entry for response
    LOGGER.debug("Responding: " + response);
    return response;
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) DeviceSimulatorException(com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException) ByteString(com.google.protobuf.ByteString) Oslp(com.alliander.osgp.oslp.Oslp)

Aggregations

Message (com.alliander.osgp.oslp.Oslp.Message)7 OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)4 Test (org.junit.Test)4 Oslp (com.alliander.osgp.oslp.Oslp)1 SignedOslpEnvelopeDto (com.alliander.osgp.oslp.SignedOslpEnvelopeDto)1 OsgpException (com.alliander.osgp.shared.exceptionhandling.OsgpException)1 ResponseMessage (com.alliander.osgp.shared.infra.jms.ResponseMessage)1 Device (com.alliander.osgp.webdevicesimulator.domain.entities.Device)1 DeviceSimulatorException (com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException)1 ByteString (com.google.protobuf.ByteString)1