Search in sources :

Example 11 with OslpDevice

use of com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandler method logMessage.

protected void logMessage(final OslpEnvelope message, final boolean incoming) {
    final String deviceUid = Base64.encodeBase64String(message.getDeviceId());
    String deviceIdentification = this.getDeviceIdentificationFromMessage(message.getPayloadMessage());
    // Assume outgoing messages always valid.
    final boolean isValid = incoming ? message.isValid() : true;
    if (StringUtils.isEmpty(deviceIdentification)) {
        // Getting the deviceIdentification from the oslpDevice instance
        final OslpDevice oslpDevice = this.oslpDeviceRepository.findByDeviceUid(deviceUid);
        if (oslpDevice != null) {
            deviceIdentification = oslpDevice.getDeviceIdentification();
        }
    }
    final OslpLogItemRequestMessage oslpLogItemRequestMessage = new OslpLogItemRequestMessage(null, deviceUid, deviceIdentification, incoming, isValid, message.getPayloadMessage(), message.getSize());
    this.oslpLogItemRequestMessageSender.send(oslpLogItemRequestMessage);
}
Also used : OslpLogItemRequestMessage(com.alliander.osgp.adapter.protocol.oslp.elster.infra.messaging.OslpLogItemRequestMessage) OslpDevice(com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice)

Example 12 with OslpDevice

use of com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandlerServer method handleRegisterDeviceRequest.

private Oslp.Message handleRegisterDeviceRequest(final byte[] deviceUid, final byte[] sequenceNumber, final Oslp.RegisterDeviceRequest registerRequest) throws UnknownHostException {
    final String deviceIdentification = registerRequest.getDeviceIdentification();
    final String deviceType = registerRequest.getDeviceType().toString();
    final boolean hasSchedule = registerRequest.getHasSchedule();
    InetAddress inetAddress;
    // set, the values will be used to set an IP address for a device.
    if (this.testDeviceIps != null && this.testDeviceIps.containsKey(deviceIdentification)) {
        final String testDeviceIp = this.testDeviceIps.get(deviceIdentification);
        LOGGER.info("Using testDeviceId: {} and testDeviceIp: {}", deviceIdentification, testDeviceIp);
        inetAddress = InetAddress.getByName(testDeviceIp);
    } else {
        inetAddress = InetAddress.getByAddress(registerRequest.getIpAddress().toByteArray());
    }
    // Send message to OSGP-CORE to save IP Address, device type and has
    // schedule values in OSGP-CORE database.
    this.deviceRegistrationService.sendDeviceRegisterRequest(inetAddress, deviceType, hasSchedule, deviceIdentification);
    OslpDevice oslpDevice = this.oslpDeviceSettingsService.getDeviceByDeviceIdentification(registerRequest.getDeviceIdentification());
    // Save the security related values in the OSLP database.
    oslpDevice.updateRegistrationData(deviceUid, registerRequest.getDeviceType().toString(), Integer.valueOf(registerRequest.getRandomDevice()));
    oslpDevice.setSequenceNumber(SequenceNumberUtils.convertByteArrayToInteger(sequenceNumber));
    oslpDevice = this.oslpDeviceSettingsService.updateDevice(oslpDevice);
    // Return current date and time in UTC so the device can sync the clock.
    final Oslp.RegisterDeviceResponse.Builder responseBuilder = Oslp.RegisterDeviceResponse.newBuilder().setStatus(Oslp.Status.OK).setCurrentTime(Instant.now().toString(format)).setRandomDevice(registerRequest.getRandomDevice()).setRandomPlatform(oslpDevice.getRandomPlatform());
    // Return local time zone information of the platform. Devices can use
    // this to convert UTC times to local times.
    final LocationInfo.Builder locationInfo = LocationInfo.newBuilder();
    locationInfo.setTimeOffset(this.timeZoneOffsetMinutes);
    // Get the GPS values from OSGP-CORE database.
    final GpsCoordinatesDto gpsCoordinates = this.deviceDataService.getGpsCoordinatesForDevice(deviceIdentification);
    if (gpsCoordinates != null && gpsCoordinates.getLatitude() != null && gpsCoordinates.getLongitude() != null) {
        // Add GPS information when available in meta data.
        locationInfo.setLatitude(this.convertGpsCoordinateFromFloatToInt(gpsCoordinates.getLatitude())).setLongitude(this.convertGpsCoordinateFromFloatToInt(gpsCoordinates.getLongitude()));
    } else {
        // Otherwise use default GPS information.
        locationInfo.setLatitude(this.convertGpsCoordinateFromFloatToInt(this.defaultLatitude)).setLongitude(this.convertGpsCoordinateFromFloatToInt(this.defaultLongitude));
    }
    responseBuilder.setLocationInfo(locationInfo);
    return Oslp.Message.newBuilder().setRegisterDeviceResponse(responseBuilder.build()).build();
}
Also used : GpsCoordinatesDto(com.alliander.osgp.dto.valueobjects.GpsCoordinatesDto) InetAddress(java.net.InetAddress) OslpDevice(com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice) LocationInfo(com.alliander.osgp.oslp.Oslp.LocationInfo)

Example 13 with OslpDevice

use of com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice in project Protocol-Adapter-OSLP by OSGP.

the class OslpSecurityHandler method messageReceived.

@Override
public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent evt) throws Exception {
    final OslpEnvelope message = (OslpEnvelope) evt.getMessage();
    // Upon first registration, a deviceUid is unknown within the platform.
    // Search based on deviceIdentification in this case.
    OslpDevice oslpDevice = null;
    if (message.getPayloadMessage().hasRegisterDeviceRequest()) {
        final String deviceIdentification = message.getPayloadMessage().getRegisterDeviceRequest().getDeviceIdentification();
        oslpDevice = this.oslpDeviceSettingsService.getDeviceByDeviceIdentification(deviceIdentification);
    } else {
        oslpDevice = this.oslpDeviceSettingsService.getDeviceByUid(Base64.encodeBase64String(message.getDeviceId()));
    }
    if (oslpDevice == null) {
        LOGGER.warn("Received message from unknown device.");
    } else if (oslpDevice.getPublicKey() == null) {
        LOGGER.warn("Received message from device without public key: {}", oslpDevice.getDeviceIdentification());
    }
    // not valid.
    if (oslpDevice != null && oslpDevice.getPublicKey() != null) {
        final PublicKey publicKey = CertificateHelper.createPublicKeyFromBase64(oslpDevice.getPublicKey(), this.oslpKeyType, this.oslpSignatureProvider);
        message.validate(publicKey);
    }
    ctx.sendUpstream(evt);
}
Also used : PublicKey(java.security.PublicKey) OslpDevice(com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Aggregations

OslpDevice (com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice)13 ProtocolAdapterException (com.alliander.osgp.adapter.protocol.oslp.elster.exceptions.ProtocolAdapterException)3 OslpLogItemRequestMessage (com.alliander.osgp.adapter.protocol.oslp.elster.infra.messaging.OslpLogItemRequestMessage)3 OsgpException (com.alliander.osgp.shared.exceptionhandling.OsgpException)2 TechnicalException (com.alliander.osgp.shared.exceptionhandling.TechnicalException)2 EventNotificationDto (com.alliander.osgp.dto.valueobjects.EventNotificationDto)1 GpsCoordinatesDto (com.alliander.osgp.dto.valueobjects.GpsCoordinatesDto)1 Oslp (com.alliander.osgp.oslp.Oslp)1 LocationInfo (com.alliander.osgp.oslp.Oslp.LocationInfo)1 OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)1 RequestMessage (com.alliander.osgp.shared.infra.jms.RequestMessage)1 ByteString (com.google.protobuf.ByteString)1 InetAddress (java.net.InetAddress)1 PublicKey (java.security.PublicKey)1 ArrayList (java.util.ArrayList)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1