Search in sources :

Example 1 with SetKeysRequestDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto in project open-smart-grid-platform by OSGP.

the class ConfigurationService method replaceKeys.

public void replaceKeys(final MessageMetadata messageMetadata, final SetKeysRequestData keySet) throws FunctionalException {
    log.info("replaceKeys for organisationIdentification: {} for deviceIdentification: {}", messageMetadata.getOrganisationIdentification(), messageMetadata.getDeviceIdentification());
    final SmartMeter smartMeter = this.domainHelperService.findSmartMeter(messageMetadata.getDeviceIdentification());
    final SetKeysRequestDto requestDto = this.configurationMapper.map(keySet, SetKeysRequestDto.class);
    this.osgpCoreRequestMessageSender.send(requestDto, messageMetadata.builder().withIpAddress(smartMeter.getIpAddress()).withNetworkSegmentIds(smartMeter.getBtsId(), smartMeter.getCellId()).build());
}
Also used : SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) SmartMeter(org.opensmartgridplatform.domain.core.entities.SmartMeter)

Example 2 with SetKeysRequestDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto in project open-smart-grid-platform by OSGP.

the class SetKeysRequestMappingTest method testWithEmptyArrays.

// To test mapping when arrays are empty
@Test
public void testWithEmptyArrays() {
    // build test data
    final byte[] authenthicationKey = new byte[1];
    final byte[] encryptionKey = new byte[1];
    final SetKeysRequestData keySet = new SetKeysRequestData(authenthicationKey, encryptionKey);
    // actual mapping
    final SetKeysRequestDto keySetDto = this.configurationMapper.map(keySet, SetKeysRequestDto.class);
    // check if mapping succeeded
    assertThat(keySetDto).isNotNull();
    assertThat(keySetDto.getAuthenticationKey()).isNotNull();
    assertThat(keySetDto.getEncryptionKey()).isNotNull();
    assertThat(keySetDto.getAuthenticationKey().length).isEqualTo(keySet.getAuthenticationKey().length);
    assertThat(keySet.getEncryptionKey().length).isEqualTo(keySetDto.getEncryptionKey().length);
}
Also used : SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) SetKeysRequestData(org.opensmartgridplatform.domain.core.valueobjects.smartmetering.SetKeysRequestData) Test(org.junit.jupiter.api.Test)

Example 3 with SetKeysRequestDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto in project open-smart-grid-platform by OSGP.

the class AbstractReplaceKeyCommandExecutor method replaceKeys.

protected ActionResponseDto replaceKeys(final DlmsConnectionManager conn, final DlmsDevice device, final SetKeysRequestDto setKeysRequestDto, final MessageMetadata messageMetadata) throws OsgpException {
    log.info("Keys set on device :{}", device.getDeviceIdentification());
    SetKeysRequestDto setDecryptedKeysRequestDto = setKeysRequestDto;
    if (!setKeysRequestDto.isGeneratedKeys()) {
        setDecryptedKeysRequestDto = this.decryptRsaKeys(setKeysRequestDto);
    }
    // if keys are generated, then they are unencrypted by the GenerateAndReplaceKeyCommandExecutor
    final DlmsDevice devicePostSave = this.storeAndSendToDevice(conn, device, wrap(setDecryptedKeysRequestDto.getAuthenticationKey(), KeyId.AUTHENTICATION_KEY, SecurityKeyType.E_METER_AUTHENTICATION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
    this.storeAndSendToDevice(conn, devicePostSave, wrap(setDecryptedKeysRequestDto.getEncryptionKey(), KeyId.GLOBAL_UNICAST_ENCRYPTION_KEY, SecurityKeyType.E_METER_ENCRYPTION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
    return new ActionResponseDto(String.format("Replace keys for device %s was successful", device.getDeviceIdentification()));
}
Also used : ActionResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionResponseDto) SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)

Example 4 with SetKeysRequestDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto in project open-smart-grid-platform by OSGP.

the class GenerateAndReplaceKeyCommandExecutor method execute.

@Override
public ActionResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final GenerateAndReplaceKeysRequestDataDto actionRequestDto, final MessageMetadata messageMetadata) throws OsgpException {
    try {
        this.deviceKeyProcessingService.startProcessing(messageMetadata.getDeviceIdentification());
    } catch (final DeviceKeyProcessAlreadyRunningException e) {
        // This exception will be caught in the DeviceRequestMessageProcessor.
        // The request will NOT be sent back to Core to retry but put back on the queue
        LOGGER.info("Key changing process already running on device :{}", device.getDeviceIdentification());
        throw e;
    }
    try {
        LOGGER.info("Generate new keys for device {}", device.getDeviceIdentification());
        final SetKeysRequestDto setKeysRequest = this.generateSetKeysRequest(messageMetadata, device.getDeviceIdentification());
        return this.replaceKeys(conn, device, setKeysRequest, messageMetadata);
    } finally {
        this.deviceKeyProcessingService.stopProcessing(messageMetadata.getDeviceIdentification());
    }
}
Also used : DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto)

Example 5 with SetKeysRequestDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto in project open-smart-grid-platform by OSGP.

the class GenerateAndReplaceKeyCommandExecutor method generateSetKeysRequest.

private SetKeysRequestDto generateSetKeysRequest(final MessageMetadata messageMetadata, final String deviceIdentification) throws FunctionalException {
    try {
        final List<SecurityKeyType> keyTypes = Arrays.asList(E_METER_AUTHENTICATION, E_METER_ENCRYPTION);
        final Map<SecurityKeyType, byte[]> generatedKeys = this.secretManagementService.generate128BitsKeysAndStoreAsNewKeys(messageMetadata, deviceIdentification, keyTypes);
        final SetKeysRequestDto setKeysRequest = new SetKeysRequestDto(generatedKeys.get(E_METER_AUTHENTICATION), generatedKeys.get(E_METER_ENCRYPTION));
        setKeysRequest.setGeneratedKeys(true);
        return setKeysRequest;
    } catch (final EncrypterException e) {
        throw new FunctionalException(FunctionalExceptionType.ENCRYPTION_EXCEPTION, ComponentType.PROTOCOL_DLMS, e);
    }
}
Also used : SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)

Aggregations

SetKeysRequestDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto)9 Test (org.junit.jupiter.api.Test)3 SetKeysRequestData (org.opensmartgridplatform.domain.core.valueobjects.smartmetering.SetKeysRequestData)3 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)2 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)2 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)1 SecurityKeyType (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)1 DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)1 SmartMeter (org.opensmartgridplatform.domain.core.entities.SmartMeter)1 ActionResponseDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionResponseDto)1