Removing Rest and Netconf devices when the providers are disabled

Change-Id: Icac7146fea1295c11972ae4cbf87f8ef9689c671
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
index cebe012..99ef8ce 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
@@ -108,7 +108,7 @@
     }
 
     @Override
-    public void removeDevice(NetconfDeviceInfo deviceInfo) {
+    public void disconnectDevice(NetconfDeviceInfo deviceInfo) {
         if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
             log.warn("Device {} is not present", deviceInfo);
         } else {
@@ -116,6 +116,18 @@
         }
     }
 
+    @Override
+    public void removeDevice(NetconfDeviceInfo deviceInfo) {
+        if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
+            log.warn("Device {} is not present", deviceInfo);
+        } else {
+            netconfDeviceMap.remove(deviceInfo.getDeviceId());
+            for (NetconfDeviceListener l : netconfDeviceListeners) {
+                l.deviceRemoved(deviceInfo);
+            }
+        }
+    }
+
     private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
         NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
         for (NetconfDeviceListener l : netconfDeviceListeners) {
@@ -138,6 +150,11 @@
         return netconfDeviceMap;
     }
 
+    @Override
+    public Set<DeviceId> getNetconfDevices() {
+        return netconfDeviceMap.keySet();
+    }
+
     //Device factory for the specific NetconfDeviceImpl
     private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
 
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfStreamThread.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfStreamThread.java
index 74d49b2..0b2906e 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfStreamThread.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfStreamThread.java
@@ -182,6 +182,7 @@
                                     null, null, Optional.of(-1), netconfDeviceInfo);
                             netconfDeviceEventListeners.forEach(
                                     listener -> listener.event(event));
+                            this.interrupt();
                         } else {
                             deviceReply = deviceReply.replace(END_PATTERN, "");
                             if (deviceReply.contains(RPC_REPLY) ||
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
index 87bdda0..931bfd4 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
@@ -32,8 +32,10 @@
 import org.onosproject.netconf.NetconfSession;
 
 import java.lang.reflect.Field;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
@@ -134,6 +136,14 @@
     }
 
     @Test
+    public void testGetNetconfDevices() {
+        Set<DeviceId> devices = new HashSet<>();
+        devices.add(deviceId1);
+        devices.add(deviceId2);
+        assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
+    }
+
+    @Test
     public void testGetNetconfDevice() {
         NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
         assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
@@ -192,7 +202,16 @@
     }
 
     /**
-     * Check for removeDevice exception.
+     * Check that disconnectDevice actually disconnects the device and removes it.
+     */
+    @Test
+    public void testDisconnectDevice() throws Exception {
+        ctrl.disconnectDevice(deviceInfo1);
+        assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
+    }
+
+    /**
+     * Checks that disconnectDevice actually disconnects the device and removes it.
      */
     @Test
     public void testRemoveDevice() throws Exception {