Search in sources :

Example 1 with RecoverKeyException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException in project open-smart-grid-platform by OSGP.

the class RecoverKeyProcess method run.

@Override
public void run() {
    final DlmsDevice device = this.findDeviceAndCheckState();
    log.info("[{}] Attempting key recovery for device {}", this.messageMetadata.getCorrelationUid(), this.deviceIdentification);
    try {
        // The process started in this try-catch block should only be stopped in the
        // ThrottlingPermitDeniedException catch block. If a DeviceKeyProcessAlreadyRunningException
        // is thrown the process was already started and should not be stopped. This method below
        // could also throw a RecoverKeyException in this case the process wasn't started either
        // The next try-catch block has a finally block to ensure stopping the process started here.
        this.startProcessing(device);
        if (!this.canConnectUsingNewKeys(device)) {
            log.warn("[{}] Could not recover keys: could not connect to device {} using New keys", this.messageMetadata.getCorrelationUid(), this.deviceIdentification);
            return;
        }
    } catch (final ThrottlingPermitDeniedException e) {
        log.warn("RecoverKeyProcess could not connect to the device due to throttling constraints", e);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                RecoverKeyProcess.this.run();
            }
        }, this.throttlingClientConfig.permitRejectedDelay().toMillis());
        this.deviceKeyProcessingService.stopProcessing(this.deviceIdentification);
        return;
    } catch (final DeviceKeyProcessAlreadyRunningException e) {
        log.info("RecoverKeyProcess could not be started while other key changing process is already running.");
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                RecoverKeyProcess.this.run();
            }
        }, this.deviceKeyProcessingService.getDeviceKeyProcessingTimeout().toMillis());
        return;
    }
    try {
        this.secretManagementService.activateNewKeys(this.messageMetadata, this.deviceIdentification, Arrays.asList(E_METER_ENCRYPTION, E_METER_AUTHENTICATION));
    } catch (final Exception e) {
        throw new RecoverKeyException(e);
    } finally {
        this.deviceKeyProcessingService.stopProcessing(this.deviceIdentification);
    }
}
Also used : DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) RecoverKeyException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) RecoverKeyException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)1 DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)1 RecoverKeyException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException)1 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)1 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)1