Search in sources :

Example 1 with Permit

use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.

the class ThrottlingClientTest method unregisteredClientRequestsPermitForUnknownNetworkSegment.

@Test
void unregisteredClientRequestsPermitForUnknownNetworkSegment() {
    final short throttlingConfigId = 5456;
    final int clientId = 573467;
    final int requestId = 946585809;
    this.whenTheThrottlingClientUsesNextRequestId(requestId);
    this.whenTheThrottlingServiceGrantsTheRequestedPermit(throttlingConfigId, clientId, requestId);
    final Permit expectedPermit = new Permit(throttlingConfigId, clientId, requestId, null, null, null);
    final Optional<Permit> requestedPermit = this.throttlingClient.requestPermit();
    assertThat(requestedPermit).usingRecursiveComparison().ignoringFieldsOfTypes(Instant.class).isEqualTo(Optional.of(expectedPermit));
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Permit

use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.

the class ThrottlingClientTest method unregisteredClientReleasesPermitForNetworkSegment.

@Test
void unregisteredClientReleasesPermitForNetworkSegment() {
    final short throttlingConfigId = 901;
    final int clientId = 4518988;
    final int baseTransceiverStationId = 10029;
    final int cellId = 1;
    final int requestId = 23938477;
    this.whenTheThrottlingServiceReleasesThePermit(throttlingConfigId, clientId, baseTransceiverStationId, cellId, requestId, true);
    final Permit permitToBeReleased = new Permit(throttlingConfigId, clientId, requestId, baseTransceiverStationId, cellId, Instant.now().minusSeconds(3));
    final boolean released = this.throttlingClient.releasePermit(permitToBeReleased);
    assertThat(released).isTrue();
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Permit

use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.

the class ThrottlingClient method requestPermit.

/**
 * Requests a permit for a network segment identified by {@code baseTransceiverStationId} and
 * {@code cellId}
 *
 * <p>The return value contains a request ID that is unique to this client. This permit, with the
 * request, BTS and cell ID is required to release the permit when finished.
 *
 * @param baseTransceiverStationId BTS for which a permit is requested
 * @param cellId Cell of the BTS for which a permit is requested
 * @return a permit granting access to the given network and network segment, containing a locally
 *     (this client) unique request ID, or an empty response if no permit was available.
 */
public Optional<Permit> requestPermit(final int baseTransceiverStationId, final int cellId) {
    final int requestId = this.requestIdCounter.incrementAndGet();
    LOGGER.debug("Requesting permit for network segment ({}, {}) using requestId {} for clientId {} on {}", baseTransceiverStationId, cellId, requestId, this.clientId, this.throttlingConfig);
    final Integer numberOfGrantedPermits = this.numberOfGrantedPermits(requestId, baseTransceiverStationId, cellId);
    if (numberOfGrantedPermits == null) {
        this.discardPermitLogExceptionOnFailure(requestId);
        return Optional.empty();
    }
    /*
     * Set created at for new permits to now, which may be a slightly different instant than the one
     * with the throttling service. This is just an indication, and should not be depended on to
     * match the value when inspecting the permit at the throttling service.
     */
    return numberOfGrantedPermits > 0 ? Optional.of(new Permit(this.throttlingConfig.getId(), this.clientId, requestId, baseTransceiverStationId, cellId, Instant.now())) : Optional.empty();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Permit(org.opensmartgridplatform.throttling.api.Permit)

Example 4 with Permit

use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.

the class ThrottlingClient method requestPermit.

/**
 * Requests a permit for the entire network. All calls to this method share a single throttling
 * limit.
 *
 * <p>The return value contains a request ID that is unique to this client. This permit, with the
 * request ID is required to release the permit when finished.
 *
 * @return a permit granting access to the network, containing a locally (this client) unique
 *     request ID, or an empty response if no permit was available.
 */
public Optional<Permit> requestPermit() {
    final int requestId = this.requestIdCounter.incrementAndGet();
    LOGGER.debug("Requesting permit using requestId {} for clientId {} on {}", requestId, this.clientId, this.throttlingConfig);
    final Integer numberOfGrantedPermits = this.numberOfGrantedPermits(requestId);
    if (numberOfGrantedPermits == null) {
        this.discardPermitLogExceptionOnFailure(requestId);
        return Optional.empty();
    }
    /*
     * Set created at for new permits to now, which may be a slightly different instant than the one
     * with the throttling service. This is just an indication, and should not be depended on to
     * match the value when inspecting the permit at the throttling service.
     */
    return numberOfGrantedPermits > 0 ? Optional.of(new Permit(this.throttlingConfig.getId(), this.clientId, requestId, null, null, Instant.now())) : Optional.empty();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Permit(org.opensmartgridplatform.throttling.api.Permit)

Example 5 with Permit

use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.

the class ThrottlingClientTest method clientRequestsPermitForUnknownNetworkSegment.

@Test
void clientRequestsPermitForUnknownNetworkSegment() {
    final short throttlingConfigId = 5456;
    final int clientId = 573467;
    final int requestId = 946585809;
    this.whenTheThrottlingConfigIsIdentifiedById(throttlingConfigId);
    this.whenTheThrottlingClientHasRegisteredWithId(clientId);
    this.whenTheThrottlingClientUsesNextRequestId(requestId);
    this.whenTheThrottlingServiceGrantsTheRequestedPermit(throttlingConfigId, clientId, requestId);
    final Permit expectedPermit = new Permit(throttlingConfigId, clientId, requestId, null, null, null);
    final Optional<Permit> requestedPermit = this.throttlingClient.requestPermit();
    assertThat(requestedPermit).usingRecursiveComparison().ignoringFieldsOfTypes(Instant.class).isEqualTo(Optional.of(expectedPermit));
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) Instant(java.time.Instant) 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