Search in sources :

Example 26 with Message

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

the class OslpUtils method isOslpResponse.

public static boolean isOslpResponse(final OslpEnvelope envelope) {
    final Message message = envelope.getPayloadMessage();
    final boolean[] hasResponse = { message.hasRegisterDeviceResponse(), message.hasConfirmRegisterDeviceResponse(), message.hasStartSelfTestResponse(), message.hasStopSelfTestResponse(), message.hasUpdateFirmwareResponse(), message.hasSetLightResponse(), message.hasSetEventNotificationsResponse(), message.hasEventNotificationResponse(), message.hasSetScheduleResponse(), message.hasGetFirmwareVersionResponse(), message.hasGetStatusResponse(), message.hasResumeScheduleResponse(), message.hasSetRebootResponse(), message.hasSetTransitionResponse(), message.hasSetConfigurationResponse(), message.hasGetConfigurationResponse(), message.hasSwitchConfigurationResponse(), message.hasGetActualPowerUsageResponse(), message.hasGetPowerUsageHistoryResponse(), message.hasSwitchFirmwareResponse(), message.hasUpdateDeviceSslCertificationResponse(), message.hasSetDeviceVerificationKeyResponse() };
    return BooleanUtils.or(hasResponse);
}
Also used : Message(org.opensmartgridplatform.oslp.Oslp.Message)

Example 27 with Message

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

the class OslpEnvelopeEcDsaTest method buildOslpMessageIncorrectSignature.

/**
 * Valid must fail when decryption fails using incorrect keys
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchProviderException
 */
@Test()
public void buildOslpMessageIncorrectSignature() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final byte[] deviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    final byte[] sequenceNumber = new byte[] { 0, 1 };
    final Message message = this.buildRegisterResponse();
    assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
        new OslpEnvelope.Builder().withSignature("Incorrect").withProvider(this.provider()).withPrimaryKey(CertificateHelper.createPrivateKeyFromBase64(PRIVATE_KEY_BASE_64, KEY_TYPE, this.provider())).withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPayloadMessage(message).build();
    });
}
Also used : Message(org.opensmartgridplatform.oslp.Oslp.Message) Test(org.junit.jupiter.api.Test)

Example 28 with Message

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

the class OslpChannelHandler method handleRequest.

private Oslp.Message handleRequest(final OslpEnvelope message, final int sequenceNumber) throws DeviceSimulatorException {
    final Oslp.Message request = message.getPayloadMessage();
    // Create response message
    final Oslp.Message response;
    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 int 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) {
        if (this.reponseDelayRandomRange == null) {
            this.sleep(this.responseDelayTime);
        } else {
            final Long randomDelay = (long) (this.reponseDelayRandomRange * this.random.nextDouble());
            this.sleep(this.responseDelayTime + randomDelay);
        }
    }
    response = this.checkForRequest(request, device);
    // Update device
    device.setSequenceNumber(expectedSequenceNumber);
    this.deviceManagementService.updateDevice(device);
    // Write log entry for response
    LOGGER.debug("Responding: {}", response);
    return response;
}
Also used : Message(org.opensmartgridplatform.oslp.Oslp.Message) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) DeviceSimulatorException(org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException) ByteString(com.google.protobuf.ByteString) Oslp(org.opensmartgridplatform.oslp.Oslp)

Aggregations

Message (org.opensmartgridplatform.oslp.Oslp.Message)28 Then (io.cucumber.java.en.Then)18 ByteString (com.google.protobuf.ByteString)5 IOException (java.io.IOException)4 Oslp (org.opensmartgridplatform.oslp.Oslp)4 ParseException (java.text.ParseException)3 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)3 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)3 UnknownHostException (java.net.UnknownHostException)2 Test (org.junit.jupiter.api.Test)2 DeviceSimulatorException (org.opensmartgridplatform.cucumber.platform.publiclighting.mocks.oslpdevice.DeviceSimulatorException)2 EventNotificationResponse (org.opensmartgridplatform.oslp.Oslp.EventNotificationResponse)2 Given (io.cucumber.java.en.Given)1 When (io.cucumber.java.en.When)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 Sharable (io.netty.channel.ChannelHandler.Sharable)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelId (io.netty.channel.ChannelId)1