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