Add contextual logging to WebSocketServer and clean up formatting 69/38569/1
authorRyan Goulding <ryandgoulding@gmail.com>
Mon, 9 May 2016 11:36:27 +0000 (07:36 -0400)
committerRyan Goulding <ryandgoulding@gmail.com>
Mon, 9 May 2016 11:39:18 +0000 (07:39 -0400)
Just adds some logging to WebSocketServer and cleans up formatting.  Since
the port is assigned as part of createInstance(...), the DEFAULT_PORT public
constant was removed to further support encapsulation.

Change-Id: I7f6922fd08d54d982d267ed50c860b76a31d0014
Signed-off-by: Ryan Goulding <ryandgoulding@gmail.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServer.java

index fd39da65ba574849975b094c39b068e3b7f718c3..9a58a1c1626ce87a6a42c365bb6a4c6b4101af9d 100644 (file)
@@ -106,6 +106,9 @@ public class RestconfImpl implements RestconfService {
 
     private static final RestconfImpl INSTANCE = new RestconfImpl();
 
+    /**
+     * Notifications are served on port 8181.
+     */
     private static final int NOTIFICATION_PORT = 8181;
 
     private static final int CHAR_NOT_FOUND = -1;
@@ -114,8 +117,6 @@ public class RestconfImpl implements RestconfService {
 
     private static final String SAL_REMOTE_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote";
 
-    private static final String SAL_REMOTE_RPC_SUBSRCIBE = "create-data-change-event-subscription";
-
     private BrokerFacade broker;
 
     private ControllerContext controllerContext;
index 011dc5c7838b885b0a87437c5e4c1f3e9f712ee5..ab4fd7ee78061f4ddf8b92728995ef1f32f6d63c 100644 (file)
@@ -19,29 +19,32 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * {@link WebSocketServer} is responsible to start and stop web socket server
+ * {@link WebSocketServer} is the singleton responsible for starting and stopping the
+ * web socket server.
  */
 public class WebSocketServer implements Runnable {
 
-    private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
-    public static final int DEFAULT_PORT = 8181;
+    private static final Logger LOG = LoggerFactory.getLogger(WebSocketServer.class);
+
+    private static WebSocketServer instance = null;
+
+    private final int port;
+
     private EventLoopGroup bossGroup;
     private EventLoopGroup workerGroup;
-    private static WebSocketServer instance = null;
-    private int port = DEFAULT_PORT;
 
-    private WebSocketServer(int port) {
+
+    private WebSocketServer(final int port) {
         this.port = port;
     }
 
     /**
-     * Create instance of {@link WebSocketServer}
+     * Create singleton instance of {@link WebSocketServer}
      *
-     * @param port
-     *            TCP port used for this server
+     * @param port TCP port used for this server
      * @return instance of {@link WebSocketServer}
      */
-    public static WebSocketServer createInstance(int port) {
+    public static WebSocketServer createInstance(final int port) {
         Preconditions.checkState(instance == null, "createInstance() has already been called");
         Preconditions.checkArgument(port >= 1024, "Privileged port (below 1024) is not allowed");
 
@@ -50,7 +53,7 @@ public class WebSocketServer implements Runnable {
     }
 
     /**
-     * Return websocket TCP port
+     * @return websocket TCP port
      */
     public int getPort() {
         return port;
@@ -67,7 +70,7 @@ public class WebSocketServer implements Runnable {
     }
 
     /**
-     * Destroy this already created instance
+     * Destroy the existing instance
      */
     public static void destroyInstance() {
         Preconditions.checkState(instance != null, "createInstance() must be called prior to destroyInstance()");
@@ -81,16 +84,16 @@ public class WebSocketServer implements Runnable {
         bossGroup = new NioEventLoopGroup();
         workerGroup = new NioEventLoopGroup();
         try {
-            ServerBootstrap b = new ServerBootstrap();
-            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
+            final ServerBootstrap serverBootstrap = new ServerBootstrap();
+            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                     .childHandler(new WebSocketServerInitializer());
 
-            Channel ch = b.bind(port).sync().channel();
-            logger.info("Web socket server started at port {}.", port);
+            final Channel channel = serverBootstrap.bind(port).sync().channel();
+            LOG.info("Web socket server started at port {}.", port);
 
-            ch.closeFuture().sync();
-        } catch (InterruptedException e) {
-            // NOOP
+            channel.closeFuture().sync();
+        } catch (final InterruptedException e) {
+            LOG.error("Web socket server encountered an error during startup attempt on port {}", port, e);
         } finally {
             stop();
         }
@@ -100,6 +103,7 @@ public class WebSocketServer implements Runnable {
      * Stops the web socket server and removes all listeners.
      */
     private void stop() {
+        LOG.debug("Stopping the web socket server instance on port {}", port);
         Notificator.removeAllListeners();
         if (bossGroup != null) {
             bossGroup.shutdownGracefully();