Search in sources :

Example 6 with Permit

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();
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Permit

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));
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with Permit

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;
    }
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) ConnectionTaskException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException) ConnectionTaskException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) JMSException(javax.jms.JMSException) NonRetryableException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)

Example 9 with Permit

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);
        }
    }
}
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 10 with Permit

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();
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Permit (org.opensmartgridplatform.throttling.api.Permit)12 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 Instant (java.time.Instant)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IOException (java.io.IOException)1 JMSException (javax.jms.JMSException)1 DlmsConnection (org.openmuc.jdlms.DlmsConnection)1 ConnectionTaskException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException)1 DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)1 NonRetryableException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException)1 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)1 RecoverKeyException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException)1 InvocationCountingDlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)1 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)1 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)1 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)1