private void stopDevice(NetconfDeviceInfo deviceInfo) {
   netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
   netconfDeviceMap.remove(deviceInfo.getDeviceId());
   for (NetconfDeviceListener l : netconfDeviceListeners) {
     l.deviceRemoved(deviceInfo);
   }
 }
 @Override
 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
   if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
     log.info("Device {} is already present", deviceInfo);
     return netconfDeviceMap.get(deviceInfo.getDeviceId());
   } else {
     log.info("Creating NETCONF device {}", deviceInfo);
     NetconfDevice device = createDevice(deviceInfo);
     device.getSession().addDeviceOutputListener(downListener);
     return device;
   }
 }
 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
   NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
   for (NetconfDeviceListener l : netconfDeviceListeners) {
     l.deviceAdded(deviceInfo);
   }
   netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
   return netconfDevice;
 }
 @Override
 public void removeDevice(NetconfDeviceInfo deviceInfo) {
   if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
     log.warn("Device {} is not present", deviceInfo);
   } else {
     stopDevice(deviceInfo);
   }
 }
  private void connectDevices() {
    NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
    if (cfg != null) {
      try {
        cfg.getDevicesAddresses()
            .stream()
            .forEach(
                addr -> {
                  try {
                    NetconfDeviceInfo netconf =
                        new NetconfDeviceInfo(addr.name(), addr.password(), addr.ip(), addr.port());
                    controller.connectDevice(netconf);
                    Device device = deviceService.getDevice(netconf.getDeviceId());
                    if (device.is(PortDiscovery.class)) {
                      PortDiscovery portConfig = device.as(PortDiscovery.class);
                      if (portConfig != null) {
                        providerService.updatePorts(netconf.getDeviceId(), portConfig.getPorts());
                      }
                    } else {
                      log.warn("No portGetter behaviour for device {}", netconf.getDeviceId());
                    }

                  } catch (IOException e) {
                    throw new RuntimeException(
                        new NetconfException(
                            "Can't connect to NETCONF "
                                + "device on "
                                + addr.ip()
                                + ":"
                                + addr.port(),
                            e));
                  }
                });

      } catch (ConfigException e) {
        log.error("Cannot read config error " + e);
      }
    }
  }
 @Override
 public void deviceAdded(NetconfDeviceInfo nodeId) {
   Preconditions.checkNotNull(nodeId, ISNULL);
   DeviceId deviceId = nodeId.getDeviceId();
   // Netconf configuration object
   ChassisId cid = new ChassisId();
   String ipAddress = nodeId.ip().toString();
   SparseAnnotations annotations =
       DefaultAnnotations.builder()
           .set(IPADDRESS, ipAddress)
           .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
           .build();
   DeviceDescription deviceDescription =
       new DefaultDeviceDescription(
           deviceId.uri(),
           Device.Type.SWITCH,
           UNKNOWN,
           UNKNOWN,
           UNKNOWN,
           UNKNOWN,
           cid,
           annotations);
   providerService.deviceConnected(deviceId, deviceDescription);
 }
 @Override
 public void deviceRemoved(NetconfDeviceInfo nodeId) {
   Preconditions.checkNotNull(nodeId, ISNULL);
   DeviceId deviceId = nodeId.getDeviceId();
   providerService.deviceDisconnected(deviceId);
 }