Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / streams / websockets / WebSocketServer.java
index fcfa8858ee940065baee9f4c8c4df2fb3a73089c..0a5f5f0ff03be26086ede6a78d3ba2dd156e812c 100644 (file)
@@ -1,27 +1,72 @@
 package org.opendaylight.controller.sal.streams.websockets;
 
+import com.google.common.base.Preconditions;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.Channel;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
-
 import org.opendaylight.controller.sal.streams.listeners.Notificator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * {@link WebSocketServer} is responsible to start and stop web socket server at
- * {@link #PORT}.
+ * {@link WebSocketServer} is responsible to start and stop web socket server
  */
 public class WebSocketServer implements Runnable {
 
-    private static final Logger logger = LoggerFactory
-            .getLogger(WebSocketServer.class);
-
-    public static final int PORT = 8181;
+    private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
+    public static final int DEFAULT_PORT = 8181;
     private EventLoopGroup bossGroup;
     private EventLoopGroup workerGroup;
+    private static WebSocketServer instance = null;
+    private int port = DEFAULT_PORT;
+
+    private WebSocketServer(int port) {
+        this.port = port;
+    }
+
+    /**
+     * Create instance of {@link WebSocketServer}
+     *
+     * @param port
+     *            TCP port used for this server
+     * @return instance of {@link WebSocketServer}
+     */
+    public static WebSocketServer createInstance(int port) {
+        Preconditions.checkState(instance == null, "createInstance() has already been called");
+        Preconditions.checkArgument(port > 1024, "Privileged port (below 1024) is not allowed");
+
+        instance = new WebSocketServer(port);
+        return instance;
+    }
+
+    /**
+     * Return websocket TCP port
+     */
+    public int getPort() {
+        return port;
+    }
+
+    /**
+     * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}
+     *
+     * @return instance of {@link WebSocketServer}
+     */
+    public static WebSocketServer getInstance() {
+        Preconditions.checkNotNull(instance, "createInstance() must be called prior to getInstance()");
+        return instance;
+    }
+
+    /**
+     * Destroy this already created instance
+     */
+    public static void destroyInstance() {
+        Preconditions.checkState(instance != null, "createInstance() must be called prior to destroyInstance()");
+
+        instance.stop();
+        instance = null;
+    }
 
     @Override
     public void run() {
@@ -29,12 +74,11 @@ public class WebSocketServer implements Runnable {
         workerGroup = new NioEventLoopGroup();
         try {
             ServerBootstrap b = new ServerBootstrap();
-            b.group(bossGroup, workerGroup)
-                    .channel(NioServerSocketChannel.class)
+            b.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);
+            Channel ch = b.bind(port).sync().channel();
+            logger.info("Web socket server started at port {}.", port);
 
             ch.closeFuture().sync();
         } catch (InterruptedException e) {
@@ -51,9 +95,11 @@ public class WebSocketServer implements Runnable {
         Notificator.removeAllListeners();
         if (bossGroup != null) {
             bossGroup.shutdownGracefully();
+            bossGroup = null;
         }
         if (workerGroup != null) {
             workerGroup.shutdownGracefully();
+            workerGroup = null;
         }
     }