Search in sources :

Example 1 with DlmsConnection

use of org.openmuc.jdlms.DlmsConnection in project open-smart-grid-platform by OSGP.

the class DlmsHelperTest method testGetWithListWorkaround.

@Test
public void testGetWithListWorkaround() throws ProtocolAdapterException, IOException {
    final DlmsConnection dlmsConnection = mock(DlmsConnection.class);
    final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class);
    final DlmsDevice dlmsDevice = mock(DlmsDevice.class);
    when(connectionManager.getConnection()).thenReturn(dlmsConnection);
    final AttributeAddress[] attrAddresses = new AttributeAddress[1];
    attrAddresses[0] = mock(AttributeAddress.class);
    when(dlmsDevice.isWithListSupported()).thenReturn(false);
    this.dlmsHelper.getWithList(connectionManager, dlmsDevice, attrAddresses);
    verify(dlmsConnection).get(attrAddresses[0]);
}
Also used : AttributeAddress(org.openmuc.jdlms.AttributeAddress) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnection(org.openmuc.jdlms.DlmsConnection) Test(org.junit.jupiter.api.Test)

Example 2 with DlmsConnection

use of org.openmuc.jdlms.DlmsConnection in project open-smart-grid-platform by OSGP.

the class DlmsConnectionManagerTest method connectAndClose.

@Test
void connectAndClose() throws OsgpException {
    final DlmsConnection dlmsConnection = mock(DlmsConnection.class);
    when(this.connector.connect(this.messageMetadata, this.device, this.dlmsMessageListener)).thenReturn(dlmsConnection);
    this.manager.connect();
    assertThat(this.manager.getConnection()).isEqualTo(dlmsConnection);
    this.manager.close();
    assertThat(this.manager.isConnected()).isFalse();
}
Also used : DlmsConnection(org.openmuc.jdlms.DlmsConnection) Test(org.junit.jupiter.api.Test)

Example 3 with DlmsConnection

use of org.openmuc.jdlms.DlmsConnection in project open-smart-grid-platform by OSGP.

the class DlmsHelperTest method testGetWithListSupported.

@Test
public void testGetWithListSupported() throws ProtocolAdapterException, IOException {
    final DlmsConnection dlmsConnection = mock(DlmsConnection.class);
    final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class);
    final DlmsDevice dlmsDevice = mock(DlmsDevice.class);
    when(connectionManager.getConnection()).thenReturn(dlmsConnection);
    final AttributeAddress[] attrAddresses = new AttributeAddress[1];
    attrAddresses[0] = mock(AttributeAddress.class);
    when(dlmsDevice.isWithListSupported()).thenReturn(true);
    this.dlmsHelper.getWithList(connectionManager, dlmsDevice, attrAddresses);
    verify(dlmsConnection).get(Arrays.asList(attrAddresses));
}
Also used : AttributeAddress(org.openmuc.jdlms.AttributeAddress) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DlmsConnection(org.openmuc.jdlms.DlmsConnection) Test(org.junit.jupiter.api.Test)

Example 4 with DlmsConnection

use of org.openmuc.jdlms.DlmsConnection 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 5 with DlmsConnection

use of org.openmuc.jdlms.DlmsConnection in project open-smart-grid-platform by OSGP.

the class DlmsConnectionManagerTest method closeWithIOExceptionShouldNotFail.

@Test
void closeWithIOExceptionShouldNotFail() throws OsgpException, IOException {
    final DlmsConnection dlmsConnection = mock(DlmsConnection.class);
    when(this.connector.connect(this.messageMetadata, this.device, this.dlmsMessageListener)).thenReturn(dlmsConnection);
    this.manager.connect();
    assertThat(this.manager.getConnection()).isEqualTo(dlmsConnection);
    doThrow(new IOException()).when(dlmsConnection).close();
    this.manager.close();
    assertThat(this.manager.isConnected()).isFalse();
}
Also used : DlmsConnection(org.openmuc.jdlms.DlmsConnection) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

DlmsConnection (org.openmuc.jdlms.DlmsConnection)6 Test (org.junit.jupiter.api.Test)4 IOException (java.io.IOException)3 AttributeAddress (org.openmuc.jdlms.AttributeAddress)3 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)3 DlmsConnectionManager (org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)3 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)1 DeviceKeyProcessAlreadyRunningException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException)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 ThrottlingPermitDeniedException (org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException)1 Permit (org.opensmartgridplatform.throttling.api.Permit)1