Search in sources :

Example 1 with DeviceKeyProcessAlreadyRunningException

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

the class DeviceKeyProcessingService method startProcessing.

public void startProcessing(final String deviceIdentification) throws DeviceKeyProcessAlreadyRunningException, FunctionalException {
    final DlmsDevice dlmsDevice = this.dlmsDeviceRepository.findByDeviceIdentification(deviceIdentification);
    if (dlmsDevice == null) {
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.PROTOCOL_DLMS, new ProtocolAdapterException("Unable to start key changing process with unknown device: " + deviceIdentification));
    }
    final Instant oldestStartTimeNotConsiderTimedOut = Instant.now().minus(this.deviceKeyProcessingTimeout);
    final int updatedRecords = this.dlmsDeviceRepository.setProcessingStartTime(dlmsDevice.getDeviceIdentification(), oldestStartTimeNotConsiderTimedOut);
    if (updatedRecords == 0) {
        throw new DeviceKeyProcessAlreadyRunningException();
    }
}
Also used : DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) Instant(java.time.Instant) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)

Example 2 with DeviceKeyProcessAlreadyRunningException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException 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 3 with DeviceKeyProcessAlreadyRunningException

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

the class BundleService method callExecutor.

private void callExecutor(final CommandExecutor<?, ?> executor, final ActionDto action, final DlmsConnectionManager conn, final DlmsDevice device, final MessageMetadata messageMetadata) {
    final String executorName = executor.getClass().getSimpleName();
    try {
        log.info("Calling executor in bundle {} [deviceId={}]", executorName, device.getDeviceIdentification());
        final ActionResponseDto response = executor.executeBundleAction(conn, device, action.getRequest(), messageMetadata);
        action.setResponse(response);
    } catch (final ConnectionException ce) {
        log.warn("A connection exception occurred while executing {} [deviceId={}]", executorName, device.getDeviceIdentification(), ce);
        throw ce;
    } catch (final DeviceKeyProcessAlreadyRunningException e) {
        // The request will NOT be sent back to Core to retry but put back on the queue
        throw e;
    } catch (final Exception e) {
        log.error("Error while executing bundle action for {} with {} [deviceId={}]", action.getRequest().getClass().getName(), executorName, device.getDeviceIdentification(), e);
        final String message = String.format("Error handling request with %s: %s", executorName, e.getMessage());
        this.addFaultResponse(action, e, message, device);
    }
}
Also used : DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) ActionResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionResponseDto) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) MissingExecutorException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.MissingExecutorException) NonRetryableException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException) TechnicalException(org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)

Example 4 with DeviceKeyProcessAlreadyRunningException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException 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 DeviceKeyProcessAlreadyRunningException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException 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)5 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)3 Instant (java.time.Instant)2 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)2 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Duration (java.time.Duration)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 DomainHelperService (org.opensmartgridplatform.adapter.protocol.dlms.application.services.DomainHelperService)1 DlmsConnectionManager (org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)1 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)1 MissingExecutorException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.MissingExecutorException)1 NonRetryableException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException)1