Fix for Netconf device re-connection.
Unproprer issuing of Device_UNREGISTERED event was causing incorrect device
removal during session re-establishment.
Unconsistent state in ports and links resulted.

Change-Id: Ie07d9077ff0b197a1cb8936b1f307807fae34aeb
diff --git a/apps/odtn/api/src/test/resources/openconfig-device-link.json b/apps/odtn/api/src/test/resources/openconfig-device-link.json
new file mode 100644
index 0000000..9cbc0e3
--- /dev/null
+++ b/apps/odtn/api/src/test/resources/openconfig-device-link.json
@@ -0,0 +1,46 @@
+{
+  "links": {
+    "netconf:127.0.0.1:11002/201-netconf:127.0.0.1:11003/201": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    },
+    "netconf:127.0.0.1:11002/202-netconf:127.0.0.1:11003/202": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    },
+    "netconf:127.0.0.1:11002/203-netconf:127.0.0.1:11003/203": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    },
+    "netconf:127.0.0.1:11002/204-netconf:127.0.0.1:11003/204": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    },
+    "netconf:127.0.0.1:11002/205-netconf:127.0.0.1:11003/205": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    },
+    "netconf:127.0.0.1:11002/206-netconf:127.0.0.1:11003/206": {
+      "basic": {
+        "type": "OPTICAL",
+        "metric": 1,
+        "durable": true
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/apps/odtn/api/src/test/resources/openconfig-devices.json b/apps/odtn/api/src/test/resources/openconfig-devices.json
index 95cc10b..12ef321 100644
--- a/apps/odtn/api/src/test/resources/openconfig-devices.json
+++ b/apps/odtn/api/src/test/resources/openconfig-devices.json
@@ -2,7 +2,7 @@
   "devices" : {
     "netconf:127.0.0.1:11002" : {
       "basic" : {
-        "driver":"infinera-xt3300"
+        "driver":"cassini-ocnos"
       },
       "netconf" : {
         "ip" : "127.0.0.1",
@@ -13,7 +13,7 @@
     },
     "netconf:127.0.0.1:11003" : {
       "basic" : {
-        "driver":"infinera-xt3300"
+        "driver":"cassini-ocnos"
       },
       "netconf" : {
         "ip" : "127.0.0.1",
@@ -23,4 +23,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
index 6af2517..ece7ca2 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
@@ -310,6 +310,7 @@
             netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
             return netconfDevicedevice;
         } finally {
+
             mutex.unlock();
         }
     }
@@ -423,7 +424,7 @@
                     } catch (NetconfException e) {
                         log.error("The SSH connection with device {} couldn't be " +
                                 "reestablished due to {}. " +
-                                "Marking the device as unreachable", e.getMessage());
+                                "Marking the device as unreachable", did, e.getMessage());
                         log.debug("Complete exception: ", e);
                         removeDevice(did);
                     }
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfStreamThread.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfStreamThread.java
index c559a03..55bd659 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfStreamThread.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfStreamThread.java
@@ -68,6 +68,7 @@
     private static final Pattern CHUNKED_SIZE_PATTERN = Pattern.compile("\\n#([1-9][0-9]*)\\n");
     private static final char HASH_CHAR = '#';
     private static final char LF_CHAR = '\n';
+    protected static final String ON_REQUEST = "on request";
 
     private OutputStreamWriter outputStream;
     private final InputStream err;
@@ -300,17 +301,19 @@
     }
 
     public void close() {
-        close("on request");
+        close(ON_REQUEST);
     }
 
     private void close(String deviceReply) {
         log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
                 netconfDeviceInfo, deviceReply);
-        NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
-                NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
-                null, null, Optional.of(-1), netconfDeviceInfo);
-        netconfDeviceEventListeners.forEach(
-                listener -> listener.event(event));
+        if (!deviceReply.equals(ON_REQUEST)) {
+            NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
+                    NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
+                    null, null, Optional.of(-1), netconfDeviceInfo);
+            netconfDeviceEventListeners.forEach(
+                    listener -> listener.event(event));
+        }
         this.interrupt();
     }