ONOS-7920 - mutex around access to prevent reentrant creation

Change-Id: I9492b2733686e0da4c05f304883f958f92598c79
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 ab639e9..c559a03 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
@@ -30,6 +30,7 @@
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
+import java.nio.channels.ClosedByInterruptException;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -247,7 +248,7 @@
         try {
             boolean socketClosed = false;
             StringBuilder deviceReplyBuilder = new StringBuilder();
-            while (!socketClosed) {
+            while (!socketClosed && !this.isInterrupted()) {
                 int cInt = bufferReader.read();
                 if (cInt == -1) {
                     log.debug("Netconf device {}  sent error char in session," +
@@ -288,6 +289,8 @@
                     }
                 }
             }
+        } catch (ClosedByInterruptException i) {
+            log.debug("Connection to device {} was terminated on request", netconfDeviceInfo.toString());
         } catch (IOException e) {
             log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
             throw new IllegalStateException(new NetconfException("Error in reading from the session for device {}" +
@@ -296,6 +299,10 @@
         }
     }
 
+    public void close() {
+        close("on request");
+    }
+
     private void close(String deviceReply) {
         log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
                 netconfDeviceInfo, deviceReply);
@@ -403,4 +410,4 @@
     public void setEnableNotifications(boolean enableNotifications) {
         this.enableNotifications = enableNotifications;
     }
-}
\ No newline at end of file
+}