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