@Test public void testConnectAlreadyExistingDevice() throws Exception { NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId()); NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId()); assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(), deviceInfo1.getDeviceId()); assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(), deviceInfo2.getDeviceId()); }
@Override public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { if (netconfDeviceMap.containsKey(deviceId)) { log.debug("Device {} is already present", deviceId); return netconfDeviceMap.get(deviceId); } else { log.debug("Creating NETCONF device {}", deviceId); Device device = deviceService.getDevice(deviceId); String ip; int port; if (device != null) { ip = device.annotations().value("ipaddress"); port = Integer.parseInt(device.annotations().value("port")); } else { String[] info = deviceId.toString().split(":"); if (info.length == 3) { ip = info[1]; port = Integer.parseInt(info[2]); } else { ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])) .reduce((t, u) -> t + ":" + u) .get(); log.debug("ip v6 {}", ip); port = Integer.parseInt(info[info.length - 1]); } } try { UsernamePassword deviceKey = deviceKeyService.getDeviceKey( DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), deviceKey.password(), IpAddress.valueOf(ip), port); NetconfDevice netconfDevicedevice = createDevice(deviceInfo); netconfDevicedevice.getSession().addDeviceOutputListener(downListener); return netconfDevicedevice; } catch (NullPointerException e) { throw new NetconfException("No Device Key for device " + deviceId, e); } } }
NetconfControllerImpl implements NetconfController { @Override public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { if (netconfDeviceMap.containsKey(deviceId)) { log.debug("Device {} is already present", deviceId); return netconfDeviceMap.get(deviceId); } else { log.debug("Creating NETCONF device {}", deviceId); Device device = deviceService.getDevice(deviceId); String ip; int port; if (device != null) { ip = device.annotations().value("ipaddress"); port = Integer.parseInt(device.annotations().value("port")); } else { String[] info = deviceId.toString().split(":"); if (info.length == 3) { ip = info[1]; port = Integer.parseInt(info[2]); } else { ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])) .reduce((t, u) -> t + ":" + u) .get(); log.debug("ip v6 {}", ip); port = Integer.parseInt(info[info.length - 1]); } } try { UsernamePassword deviceKey = deviceKeyService.getDeviceKey( DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), deviceKey.password(), IpAddress.valueOf(ip), port); NetconfDevice netconfDevicedevice = createDevice(deviceInfo); netconfDevicedevice.getSession().addDeviceOutputListener(downListener); return netconfDevicedevice; } catch (NullPointerException e) { throw new NetconfException("No Device Key for device " + deviceId, e); } } } }
NetconfControllerImpl implements NetconfController { @Override public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { if (netconfDeviceMap.containsKey(deviceId)) { log.debug("Device {} is already present", deviceId); return netconfDeviceMap.get(deviceId); } else { log.debug("Creating NETCONF device {}", deviceId); Device device = deviceService.getDevice(deviceId); String ip; int port; if (device != null) { ip = device.annotations().value("ipaddress"); port = Integer.parseInt(device.annotations().value("port")); } else { String[] info = deviceId.toString().split(":"); if (info.length == 3) { ip = info[1]; port = Integer.parseInt(info[2]); } else { ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])) .reduce((t, u) -> t + ":" + u) .get(); log.debug("ip v6 {}", ip); port = Integer.parseInt(info[info.length - 1]); } } try { UsernamePassword deviceKey = deviceKeyService.getDeviceKey( DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), deviceKey.password(), IpAddress.valueOf(ip), port); NetconfDevice netconfDevicedevice = createDevice(deviceInfo); netconfDevicedevice.getSession().addDeviceOutputListener(downListener); return netconfDevicedevice; } catch (NullPointerException e) { throw new NetconfException("No Device Key for device " + deviceId, e); } } }  }
NetconfControllerImpl implements NetconfController { @Override public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { if (netconfDeviceMap.containsKey(deviceId)) { log.debug("Device {} is already present", deviceId); return netconfDeviceMap.get(deviceId); } else { log.debug("Creating NETCONF device {}", deviceId); Device device = deviceService.getDevice(deviceId); String ip; int port; if (device != null) { ip = device.annotations().value("ipaddress"); port = Integer.parseInt(device.annotations().value("port")); } else { String[] info = deviceId.toString().split(":"); if (info.length == 3) { ip = info[1]; port = Integer.parseInt(info[2]); } else { ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])) .reduce((t, u) -> t + ":" + u) .get(); log.debug("ip v6 {}", ip); port = Integer.parseInt(info[info.length - 1]); } } try { UsernamePassword deviceKey = deviceKeyService.getDeviceKey( DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), deviceKey.password(), IpAddress.valueOf(ip), port); NetconfDevice netconfDevicedevice = createDevice(deviceInfo); netconfDevicedevice.getSession().addDeviceOutputListener(downListener); return netconfDevicedevice; } catch (NullPointerException e) { throw new NetconfException("No Device Key for device " + deviceId, e); } } }  @Activate void activate(ComponentContext context); @Deactivate void deactivate(); @Modified void modified(ComponentContext context); @Override void addDeviceListener(NetconfDeviceListener listener); @Override void removeDeviceListener(NetconfDeviceListener listener); @Override NetconfDevice getNetconfDevice(DeviceId deviceInfo); @Override NetconfDevice getNetconfDevice(IpAddress ip, int port); @Override NetconfDevice connectDevice(DeviceId deviceId); @Override void disconnectDevice(DeviceId deviceId, boolean remove); @Override void removeDevice(DeviceId deviceId); @Override Map<DeviceId, NetconfDevice> getDevicesMap(); @Override Set<DeviceId> getNetconfDevices(); }
NetconfControllerImpl implements NetconfController { @Override public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { if (netconfDeviceMap.containsKey(deviceId)) { log.debug("Device {} is already present", deviceId); return netconfDeviceMap.get(deviceId); } else { log.debug("Creating NETCONF device {}", deviceId); Device device = deviceService.getDevice(deviceId); String ip; int port; if (device != null) { ip = device.annotations().value("ipaddress"); port = Integer.parseInt(device.annotations().value("port")); } else { String[] info = deviceId.toString().split(":"); if (info.length == 3) { ip = info[1]; port = Integer.parseInt(info[2]); } else { ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])) .reduce((t, u) -> t + ":" + u) .get(); log.debug("ip v6 {}", ip); port = Integer.parseInt(info[info.length - 1]); } } try { UsernamePassword deviceKey = deviceKeyService.getDeviceKey( DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), deviceKey.password(), IpAddress.valueOf(ip), port); NetconfDevice netconfDevicedevice = createDevice(deviceInfo); netconfDevicedevice.getSession().addDeviceOutputListener(downListener); return netconfDevicedevice; } catch (NullPointerException e) { throw new NetconfException("No Device Key for device " + deviceId, e); } } }  @Activate void activate(ComponentContext context); @Deactivate void deactivate(); @Modified void modified(ComponentContext context); @Override void addDeviceListener(NetconfDeviceListener listener); @Override void removeDeviceListener(NetconfDeviceListener listener); @Override NetconfDevice getNetconfDevice(DeviceId deviceInfo); @Override NetconfDevice getNetconfDevice(IpAddress ip, int port); @Override NetconfDevice connectDevice(DeviceId deviceId); @Override void disconnectDevice(DeviceId deviceId, boolean remove); @Override void removeDevice(DeviceId deviceId); @Override Map<DeviceId, NetconfDevice> getDevicesMap(); @Override Set<DeviceId> getNetconfDevices(); static final Logger log; }