NetconfConnectionManagerImpl implements NetconfConnectionManager, CallHomeListener { void auditConnections() { m_txService.executeWithTx(new TXTemplate<Void>() { @Override public Void execute() { List<Device> allDevices = m_deviceDao.findAllDevices(); for (Device meta : allDevices) { NetconfClientSession session = null; try { if (!meta.getDeviceManagement().getDeviceConnection().getConnectionModel().equals("call-home")) { session = m_connPool.borrowObject(meta); } } catch (Exception e) { LOGGER.debug(String.format("Could not setup connection to device %s", meta), e); } finally { if (session != null) { m_connPool.returnObject(meta, session); } } } Set<Device> deletedDevices = new HashSet<>(m_factory.getAllKeys()); deletedDevices.removeAll(allDevices); for (Device meta : deletedDevices) { LOGGER.debug(String.format("Device %s has been un-managed, closing the connection", meta)); m_connPool.clear(meta); m_factory.getAllKeys().remove(meta); } return null; } }); } NetconfConnectionManagerImpl(CallHomeListenerComposite callHomeListenerComposite,
                                        NetconfClientDispatcher dispatcher); DeviceDao getDeviceDao(); void setDeviceDao(DeviceDao deviceDao); TxService getTxService(); void setTxService(TxService txService); static void fillTransportDetails(Authentication authentication, NetconfClientConfigurationBuilder
            configurationBuilder); Map<String, List<DefaultPooledObjectInfo>> getAllSessions(); @Override Future<NetConfResponse> executeNetconf(Device device, AbstractNetconfRequest request); @Override Future<NetConfResponse> executeNetconf(String deviceName, AbstractNetconfRequest request); @Override RT executeWithSession(Device device, NetconfTemplate<RT> netconfTemplate); @Override boolean isConnected(Device device); @Override ConnectionState getConnectionState(Device device); @Override List<NewDeviceInfo> getNewDevices(); @Override void dropNewDeviceConnection(String duid); @Override void registerDeviceConnectionListener(ConnectionListener connectionListener); @Override void unregisterDeviceConnectionListener(ConnectionListener connectionListener); void init(); @Override void connectionEstablished(NetconfClientSession deviceSession, NetconfLoginProvider netconfLoginProvider,
                                      X509Certificate deviceCert, boolean isSelfSigned); String extractDuid(X509Certificate x509Certificate, String ipAddress, Integer port); void destroy();  }
@Test public void testManagerTriesToSetupDirectConnection() throws NetconfClientDispatcherException { m_cm.auditConnections(); verifyAttemptMadeToConnectToAllDevices(); }
@Test public void testMgrHandlesExceptions() throws NetconfClientDispatcherException { doThrow(new RuntimeException("Something went wrong")).when(m_dispatcher).createClient(anyObject()); m_cm.auditConnections(); verifyAttemptMadeToConnectToAllDevices(); }
@Test public void testConnMgrClosesConnectionToDeletedDevices1() throws NetconfClientDispatcherException { List<Device> devicePresent = new ArrayList<>(); Device nokia = spy(createDirectDevice("5")); doThrow(new EntityNotFoundException("Entity Not Found")).when(nokia).toString(); devicePresent.add(nokia); when(m_deviceDao.findAllDevices()).thenReturn(devicePresent); try { m_cm.auditConnections(); fail(); } catch (Exception e) { assertEquals("Entity Not Found", e.getMessage()); } }
@Test public void testManagerDoesNotSetupConnectionToAlreadyConnectedDevices() throws Exception { m_cm.auditConnections(); verify(m_dispatcher, times(3)).createClient(anyObject()); m_cm.auditConnections(); verifyNoMoreInteractions(m_dispatcher); }
@Test public void verifyClosedSessionAreNotKept() throws Exception { m_cm.auditConnections(); when(m_sessions.get(getIp("2")).isOpen()).thenReturn(false); m_cm.auditConnections(); verify(m_connectionListener).deviceConnected(m_devices.get(0), m_sessions.get(getIp("1"))); verify(m_connectionListener).deviceConnected(m_devices.get(1), m_sessions.get(getIp("2"))); verify(m_connectionListener).deviceConnected(m_devices.get(2), m_sessions.get(getIp("3"))); verify(m_dispatcher, times(4)).createClient(anyObject()); }
@Test public void testDirectConnectionForNotNetconfDevice() throws NetconfClientDispatcherException { List<Device> devices = new ArrayList<>(); Device device = createDirectDevice("4"); device.getDeviceManagement().setNetconf(false); devices.add(device); when(m_deviceDao.findAllDevices()).thenReturn(devices); m_cm.auditConnections(); ArgumentCaptor<NetconfClientConfiguration> captor = ArgumentCaptor.forClass(NetconfClientConfiguration.class); verify(m_dispatcher, never()).createClient(captor.capture()); }