Merge "BUG-509: migrate to TreeNodes"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / NetconfSSHServer.java
index c2d067915516b27efb44bd5a7644f942744a13d2..c6974d4982db051f1cb6a66d3f67394ee2d57817 100644 (file)
@@ -20,21 +20,21 @@ import java.net.ServerSocket;
 import java.util.concurrent.atomic.AtomicLong;
 
 @ThreadSafe
-public class NetconfSSHServer implements Runnable {
+public final class NetconfSSHServer implements Runnable {
 
     private ServerSocket ss = null;
     private static final Logger logger =  LoggerFactory.getLogger(NetconfSSHServer.class);
     private static final AtomicLong sesssionId = new AtomicLong();
     private final InetSocketAddress clientAddress;
     private final AuthProvider authProvider;
-    private boolean up = false;
+    private volatile boolean up = false;
 
-    private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws Exception{
+    private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws IllegalStateException, IOException {
 
         logger.trace("Creating SSH server socket on port {}",serverPort);
         this.ss = new ServerSocket(serverPort);
         if (!ss.isBound()){
-            throw new Exception("Socket can't be bound to requested port :"+serverPort);
+            throw new IllegalStateException("Socket can't be bound to requested port :"+serverPort);
         }
         logger.trace("Server socket created.");
         this.clientAddress = clientAddress;
@@ -42,11 +42,11 @@ public class NetconfSSHServer implements Runnable {
         this.up = true;
     }
 
-    public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws Exception {
+    public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws IllegalStateException, IOException {
         return new NetconfSSHServer(serverPort, clientAddress,authProvider);
     }
 
-    public void stop() throws Exception {
+    public void stop() throws IOException {
         up = false;
         logger.trace("Closing SSH server socket.");
         ss.close();
@@ -68,9 +68,17 @@ public class NetconfSSHServer implements Runnable {
         while (up) {
             logger.trace("Starting new socket thread.");
             try {
-               SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(),authProvider);
-            } catch (IOException e) {
-                logger.error("Exception occurred during socket thread initialization {}",e);
+                SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(), authProvider);
+            }
+            catch (IOException e) {
+                if( up ) {
+                    logger.error("Exception occurred during socket thread initialization", e);
+                }
+                else {
+                    // We're shutting down so an exception is expected as the socket's been closed.
+                    // Log to debug.
+                    logger.debug("Shutting down - got expected exception: " + e);
+                }
             }
         }
     }