Search in sources :

Example 6 with InvocationCountingDlmsMessageListener

use of org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener in project open-smart-grid-platform by OSGP.

the class DlmsConnectionFactoryTest method createsConnectionManagerForLls0Device.

@Test
void createsConnectionManagerForLls0Device() throws Exception {
    final DlmsDevice device = new DlmsDeviceBuilder().withLls1Active(false).build();
    final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
    when(this.lls0Connector.connect(this.messageMetadata, device, listener)).thenReturn(this.connection);
    final DlmsConnectionManager expected = this.newConnectionManager(device, listener, this.lls0Connector);
    this.factory.createAndHandleConnection(this.messageMetadata, device, listener, this.task);
    this.assertConnectionManagerForDevice(expected);
}
Also used : DlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsDeviceBuilder(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder) Test(org.junit.jupiter.api.Test)

Example 7 with InvocationCountingDlmsMessageListener

use of org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener in project open-smart-grid-platform by OSGP.

the class DlmsConnectionFactoryTest method createAndHandleConnectionThrowsForHls3Device.

@Test
void createAndHandleConnectionThrowsForHls3Device() {
    final DlmsDevice device = new DlmsDeviceBuilder().withHls3Active(true).build();
    final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
    assertThatExceptionOfType(FunctionalException.class).isThrownBy(() -> this.factory.createAndHandleConnection(this.messageMetadata, device, listener, this.task));
}
Also used : DlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsDeviceBuilder(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) Test(org.junit.jupiter.api.Test)

Example 8 with InvocationCountingDlmsMessageListener

use of org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener 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 9 with InvocationCountingDlmsMessageListener

use of org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener in project open-smart-grid-platform by OSGP.

the class DlmsConnectionHelperTest method initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForIskraDevice.

@Test
void initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForIskraDevice() throws Exception {
    final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withProtocol("SMR").withInvocationCounter(123L).build();
    final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
    final ConnectionException exception = new ConnectionException("Error creating connection for device E0033006878667817 with Ip address:62.133.86.119 Port:4059 " + "UseHdlc:false UseSn:false Message:UNKNOWN: Received an association response (AARE) with an" + " error message. Result name REJECTED_PERMANENT. Assumed fault: user.");
    doThrow(exception).when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
    assertThrows(ConnectionException.class, () -> this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, null, this.task));
    verify(this.invocationCounterManager).initializeInvocationCounter(this.messageMetadata, device);
    verify(this.connectionFactory, times(2)).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
}
Also used : DlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsDeviceBuilder(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) Test(org.junit.jupiter.api.Test)

Example 10 with InvocationCountingDlmsMessageListener

use of org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener in project open-smart-grid-platform by OSGP.

the class DlmsConnectionHelperTest method createsConnectionForDeviceThatNeedsInvocationCounterWithInvocationCounterInitialized.

@Test
void createsConnectionForDeviceThatNeedsInvocationCounterWithInvocationCounterInitialized() throws Exception {
    final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withInvocationCounter(123L).build();
    final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
    doNothing().when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
    this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, this.task);
    verifyNoMoreInteractions(this.invocationCounterManager);
}
Also used : DlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsDeviceBuilder(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

InvocationCountingDlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)19 Test (org.junit.jupiter.api.Test)17 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)17 DlmsDeviceBuilder (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder)17 DlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener)17 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)4 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)3 IOException (java.io.IOException)1 DlmsConnection (org.openmuc.jdlms.DlmsConnection)1 TcpConnectionBuilder (org.openmuc.jdlms.TcpConnectionBuilder)1 DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)1 RecoverKeyException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException)1 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)1 Permit (org.opensmartgridplatform.throttling.api.Permit)1