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