Search in sources :

Example 1 with ThrottlingPermitDeniedException

use of org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException 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)

Example 2 with ThrottlingPermitDeniedException

use of org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException in project open-smart-grid-platform by OSGP.

the class RecoverKeyProcess method canConnectUsingNewKeys.

private boolean canConnectUsingNewKeys(final DlmsDevice device) {
    DlmsConnection connection = null;
    InvocationCountingDlmsMessageListener dlmsMessageListener = null;
    Permit permit = null;
    try {
        // before starting the recovery process check if there are still keys with status NEW
        final boolean hasNewSecret = this.secretManagementService.hasNewSecret(this.messageMetadata, this.deviceIdentification);
        if (!hasNewSecret) {
            log.warn("[{}] Device {} has no NEW key to use in KeyRecovery process", this.messageMetadata.getCorrelationUid(), this.deviceIdentification);
            return false;
        }
        if (this.throttlingClientConfig.clientEnabled()) {
            permit = this.throttlingClientConfig.throttlingClient().requestPermitUsingNetworkSegmentIfIdsAreAvailable(this.messageMetadata.getBaseTransceiverStationId(), this.messageMetadata.getCellId());
        } else {
            this.throttlingService.openConnection();
        }
        if (device.needsInvocationCounter()) {
            dlmsMessageListener = new InvocationCountingDlmsMessageListener();
        }
        this.domainHelperService.setIpAddressFromMessageMetadataOrSessionProvider(device, this.messageMetadata);
        connection = this.hls5Connector.connectUnchecked(this.messageMetadata, device, dlmsMessageListener, this.secretManagementService::getNewOrActiveKeyPerSecretType);
        return connection != null;
    } catch (final ThrottlingPermitDeniedException e) {
        throw e;
    } catch (final Exception e) {
        log.warn("Connection exception during key recovery process for device: {} {}", device.getDeviceIdentification(), e.getMessage(), e);
        return false;
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (final IOException e) {
                log.warn("Closing connection exception: {}", e.getMessage(), e);
            }
        }
        if (this.throttlingClientConfig.clientEnabled()) {
            if (permit != null) {
                this.throttlingClientConfig.throttlingClient().releasePermit(permit);
            }
        } else {
            this.throttlingService.closeConnection();
        }
        if (dlmsMessageListener != null) {
            final int numberOfSentMessages = dlmsMessageListener.getNumberOfSentMessages();
            device.incrementInvocationCounter(numberOfSentMessages);
            this.deviceRepository.save(device);
        }
    }
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) DlmsConnection(org.openmuc.jdlms.DlmsConnection) IOException(java.io.IOException) 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)

Example 3 with ThrottlingPermitDeniedException

use of org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException in project open-smart-grid-platform by OSGP.

the class DeviceRequestMessageProcessor method processMessage.

@Override
public void processMessage(final ObjectMessage message) throws JMSException {
    log.debug("Processing {} request message", this.messageType);
    final MessageMetadata messageMetadata = MessageMetadata.fromMessage(message);
    final Serializable messageObject = message.getObject();
    try {
        final DlmsDevice device;
        if (this.requiresExistingDevice()) {
            device = this.domainHelperService.findDlmsDevice(messageMetadata);
        } else {
            device = null;
        }
        if (this.usesDeviceConnection()) {
            /*
         * Set up a consumer to be called back with a DlmsConnectionManager for which the connection
         * with the device has been created. Note that when usesDeviceConnection is true, in this
         * way all logic in processMessageTasks is executed only after the connection to the device
         * has successfully been established.
         */
            final ThrowingConsumer<DlmsConnectionManager> taskForConnectionManager = connectionManager -> this.processMessageTasks(messageObject, messageMetadata, connectionManager, device);
            this.createAndHandleConnectionForDevice(device, messageMetadata, taskForConnectionManager);
        } else {
            this.processMessageTasks(messageObject, messageMetadata, null, device);
        }
    } catch (final ThrottlingPermitDeniedException exception) {
        /*
       * Throttling permit for network access not granted, send the request back to the queue to be
       * picked up again a little later by the message listener for device requests.
       */
        this.deviceRequestMessageSender.send(messageObject, messageMetadata, this.throttlingClientConfig.permitRejectedDelay());
    } catch (final DeviceKeyProcessAlreadyRunningException exception) {
        this.deviceRequestMessageSender.send(messageObject, messageMetadata, this.deviceKeyProcessingTimeout);
    } catch (final Exception exception) {
        this.sendErrorResponse(messageMetadata, exception, messageObject);
    }
}
Also used : MessageProcessor(org.opensmartgridplatform.shared.infra.jms.MessageProcessor) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectMessage(javax.jms.ObjectMessage) FunctionalExceptionType(org.opensmartgridplatform.shared.exceptionhandling.FunctionalExceptionType) MessageType(org.opensmartgridplatform.shared.infra.jms.MessageType) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) Value(org.springframework.beans.factory.annotation.Value) Duration(java.time.Duration) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ThrowingConsumer(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ThrowingConsumer) ComponentType(org.opensmartgridplatform.shared.exceptionhandling.ComponentType) DomainHelperService(org.opensmartgridplatform.adapter.protocol.dlms.application.services.DomainHelperService) MessageProcessorMap(org.opensmartgridplatform.shared.infra.jms.MessageProcessorMap) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) Instant(java.time.Instant) JMSException(javax.jms.JMSException) Serializable(java.io.Serializable) Slf4j(lombok.extern.slf4j.Slf4j) PostConstruct(javax.annotation.PostConstruct) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) SilentException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Serializable(java.io.Serializable) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) JMSException(javax.jms.JMSException) SilentException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException)

Aggregations

DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)3 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)3 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)3 IOException (java.io.IOException)2 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)2 RecoverKeyException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException)2 Serializable (java.io.Serializable)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 PostConstruct (javax.annotation.PostConstruct)1 JMSException (javax.jms.JMSException)1 ObjectMessage (javax.jms.ObjectMessage)1 Slf4j (lombok.extern.slf4j.Slf4j)1 DlmsConnection (org.openmuc.jdlms.DlmsConnection)1 DomainHelperService (org.opensmartgridplatform.adapter.protocol.dlms.application.services.DomainHelperService)1 DlmsConnectionManager (org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)1 SilentException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.SilentException)1 ThrowingConsumer (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ThrowingConsumer)1