Fix license header violations in sal-rest-connector
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / streams / websockets / WebSocketServer.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.sal.streams.websockets;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.Channel;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import io.netty.channel.socket.nio.NioServerSocketChannel;
17 import org.opendaylight.controller.sal.streams.listeners.Notificator;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * {@link WebSocketServer} is responsible to start and stop web socket server
23  */
24 public class WebSocketServer implements Runnable {
25
26     private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
27     public static final int DEFAULT_PORT = 8181;
28     private EventLoopGroup bossGroup;
29     private EventLoopGroup workerGroup;
30     private static WebSocketServer instance = null;
31     private int port = DEFAULT_PORT;
32
33     private WebSocketServer(int port) {
34         this.port = port;
35     }
36
37     /**
38      * Create instance of {@link WebSocketServer}
39      *
40      * @param port
41      *            TCP port used for this server
42      * @return instance of {@link WebSocketServer}
43      */
44     public static WebSocketServer createInstance(int port) {
45         Preconditions.checkState(instance == null, "createInstance() has already been called");
46         Preconditions.checkArgument(port > 1024, "Privileged port (below 1024) is not allowed");
47
48         instance = new WebSocketServer(port);
49         return instance;
50     }
51
52     /**
53      * Return websocket TCP port
54      */
55     public int getPort() {
56         return port;
57     }
58
59     /**
60      * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}
61      *
62      * @return instance of {@link WebSocketServer}
63      */
64     public static WebSocketServer getInstance() {
65         Preconditions.checkNotNull(instance, "createInstance() must be called prior to getInstance()");
66         return instance;
67     }
68
69     /**
70      * Destroy this already created instance
71      */
72     public static void destroyInstance() {
73         Preconditions.checkState(instance != null, "createInstance() must be called prior to destroyInstance()");
74
75         instance.stop();
76         instance = null;
77     }
78
79     @Override
80     public void run() {
81         bossGroup = new NioEventLoopGroup();
82         workerGroup = new NioEventLoopGroup();
83         try {
84             ServerBootstrap b = new ServerBootstrap();
85             b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
86                     .childHandler(new WebSocketServerInitializer());
87
88             Channel ch = b.bind(port).sync().channel();
89             logger.info("Web socket server started at port {}.", port);
90
91             ch.closeFuture().sync();
92         } catch (InterruptedException e) {
93             // NOOP
94         } finally {
95             stop();
96         }
97     }
98
99     /**
100      * Stops the web socket server and removes all listeners.
101      */
102     private void stop() {
103         Notificator.removeAllListeners();
104         if (bossGroup != null) {
105             bossGroup.shutdownGracefully();
106             bossGroup = null;
107         }
108         if (workerGroup != null) {
109             workerGroup.shutdownGracefully();
110             workerGroup = null;
111         }
112     }
113
114 }