use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method clientReleasesPermitThatIsNotHeldForUnknownNetworkSegment.
@Test
void clientReleasesPermitThatIsNotHeldForUnknownNetworkSegment() {
final short throttlingConfigId = 11;
final int clientId = 18;
final int requestId = 21;
this.whenTheThrottlingConfigIsIdentifiedById(throttlingConfigId);
this.whenTheThrottlingClientHasRegisteredWithId(clientId);
this.whenTheThrottlingServiceReleasesThePermit(throttlingConfigId, clientId, requestId, false);
final Permit permitToBeReleased = new Permit(throttlingConfigId, clientId, requestId, null, null, Instant.now().minusSeconds(2));
final boolean released = this.throttlingClient.releasePermit(permitToBeReleased);
assertThat(released).isFalse();
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method clientRequestsPermitByNetworkSegment.
@Test
void clientRequestsPermitByNetworkSegment() {
final short throttlingConfigId = 37;
final int clientId = 347198;
final int baseTransceiverStationId = 983745;
final int cellId = 2;
final int requestId = 894;
this.whenTheThrottlingConfigIsIdentifiedById(throttlingConfigId);
this.whenTheThrottlingClientHasRegisteredWithId(clientId);
this.whenTheThrottlingClientUsesNextRequestId(requestId);
this.whenTheThrottlingServiceGrantsTheRequestedPermit(throttlingConfigId, clientId, baseTransceiverStationId, cellId, requestId);
final Permit expectedPermit = new Permit(throttlingConfigId, clientId, requestId, baseTransceiverStationId, cellId, null);
final Optional<Permit> requestedPermit = this.throttlingClient.requestPermit(baseTransceiverStationId, cellId);
assertThat(requestedPermit).usingRecursiveComparison().ignoringExpectedNullFields().isEqualTo(Optional.of(expectedPermit));
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class DlmsConnectionMessageProcessor method createAndHandleConnectionForDevice.
public void createAndHandleConnectionForDevice(final DlmsDevice device, final MessageMetadata messageMetadata, final Consumer<DlmsConnectionManager> taskForConnectionManager) throws OsgpException {
Permit permit = null;
if (this.throttlingClientConfig.clientEnabled()) {
permit = this.throttlingClientConfig.throttlingClient().requestPermitUsingNetworkSegmentIfIdsAreAvailable(messageMetadata.getBaseTransceiverStationId(), messageMetadata.getCellId());
} else {
this.throttlingService.openConnection();
}
final DlmsMessageListener dlmsMessageListener = this.createMessageListenerForDeviceConnection(device, messageMetadata);
try {
this.dlmsConnectionHelper.createAndHandleConnectionForDevice(messageMetadata, device, dlmsMessageListener, permit, taskForConnectionManager);
} catch (final ConnectionTaskException e) {
/*
* Exceptions thrown by the tasks working with the DlmsConnectionManager are wrapped in
* ConnectionTaskException in the ThrowingConsumer.
*/
throw e.getOsgpException();
} catch (final Exception e) {
/*
* Throttling permit is released in this catch block to make sure that it is made available
* again when there were problems setting up the connection and only then (exceptions thrown
* by the tasks working with the DlmsConnectionManager are wrapped as ConnectionTaskException
* and are caught in the previous catch block, so they don't end up here).
*
* If there are no problems in setting up the connection, releasing the throttling permit is
* the responsibility of the tasks for the DlmsConnectionManager, as seen in
* DeviceRequestMessageProcessor.processMessageTasks(), where
* this.doConnectionPostProcessing() is called in a finally block.
*/
if (this.throttlingClientConfig.clientEnabled()) {
this.throttlingClientConfig.throttlingClient().releasePermit(permit);
} else {
this.throttlingService.closeConnection();
}
throw e;
}
}
use of org.opensmartgridplatform.throttling.api.Permit 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.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method registerFailureClientReleasesPermitForNetworkSegment.
@Test
void registerFailureClientReleasesPermitForNetworkSegment() {
final short throttlingConfigId = 901;
final int clientId = 4518988;
final int baseTransceiverStationId = 10029;
final int cellId = 1;
final int requestId = 23938477;
this.whenTheThrottlingServiceReturnsFailureOnRegistration();
final Permit permitToBeReleased = new Permit(throttlingConfigId, clientId, requestId, baseTransceiverStationId, cellId, Instant.now().minusSeconds(3));
final boolean released = this.throttlingClient.releasePermit(permitToBeReleased);
assertThat(released).isFalse();
}
Aggregations