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]);
}
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();
}
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));
}
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);
}
}
}
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();
}
Aggregations