2d12c5c7657f1fd2fb7c2070282e2963ee4012e0
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
3
4 import io.netty.bootstrap.ServerBootstrap;
5 import io.netty.channel.ChannelFuture;
6 import io.netty.channel.ChannelOption;
7 import io.netty.channel.nio.NioEventLoopGroup;
8 import io.netty.channel.socket.nio.NioServerSocketChannel;
9 import io.netty.handler.logging.LogLevel;
10 import io.netty.handler.logging.LoggingHandler;
11 import io.netty.util.concurrent.GenericFutureListener;
12
13 import java.net.InetSocketAddress;
14
15 import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.SettableFuture;
21
22 /**
23  * Class implementing server over TCP for handling incoming connections.
24  *
25  * @author michal.polkorab
26  */
27 public class TcpHandler implements ServerFacade {
28
29     private int port;
30     private String address;
31     private NioEventLoopGroup workerGroup;
32     private NioEventLoopGroup bossGroup;
33     private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
34     private SettableFuture<Boolean> isOnlineFuture;
35     
36     
37     private PublishingChannelInitializer channelInitializer;
38
39     /**
40      * Enum used for storing names of used components (in pipeline).
41      */
42     public static enum COMPONENT_NAMES {
43
44         /**
45          * First component in pipeline - detecting TLS connections
46          */
47         TLS_DETECTOR,
48         /**
49          * Component for handling TLS frames
50          */
51         SSL_HANDLER,
52         /**
53          * Decodes incoming messages into message frames
54          */
55         OF_FRAME_DECODER,
56         /**
57          * Detects version of incoming OpenFlow Protocol message
58          */
59         OF_VERSION_DETECTOR,
60         /**
61          * Transforms OpenFlow Protocol messages
62          */
63         OF_CODEC,
64         /**
65          * Communicates with upper layers (outside OF Library)
66          */
67         OF_FACADE
68     }
69     
70
71     /**
72      * Constructor of TCPHandler that listens on selected port.
73      *
74      * @param port listening port of TCPHandler server
75      */
76     public TcpHandler(int port) {
77         this.port = port;
78         channelInitializer = new PublishingChannelInitializer();
79         isOnlineFuture = SettableFuture.create();
80     }
81
82     /**
83      * Starts server on selected port.
84      */
85     @Override
86     public void run() {
87         LOGGER.info("Switch ");
88         bossGroup = new NioEventLoopGroup();
89         workerGroup = new NioEventLoopGroup();
90         try {
91             ServerBootstrap b = new ServerBootstrap();
92             b.group(bossGroup, workerGroup)
93                     .channel(NioServerSocketChannel.class)
94                     .handler(new LoggingHandler(LogLevel.DEBUG))
95                     .childHandler(channelInitializer)
96                     .option(ChannelOption.SO_BACKLOG, 128)
97                     .childOption(ChannelOption.SO_KEEPALIVE, true);
98
99             ChannelFuture f = b.bind(port).sync();
100             
101             InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
102             address = isa.getHostName().toString();
103             port = isa.getPort();
104             isOnlineFuture.set(true);
105             LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
106             f.channel().closeFuture().sync();
107         } catch (InterruptedException ex) {
108             LOGGER.error(ex.getMessage(), ex);
109         } finally {
110             shutdown();
111         }
112     }
113
114     /**
115      * Shuts down {@link TcpHandler}}
116      */
117     @Override
118     public ListenableFuture<Boolean> shutdown() {
119         final SettableFuture<Boolean> result = SettableFuture.create();
120         workerGroup.shutdownGracefully();
121         // boss will shutdown as soon, as worker is down
122         bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
123
124             @Override
125             public void operationComplete(
126                     io.netty.util.concurrent.Future<Object> downResult) throws Exception {
127                 result.set(downResult.isSuccess());
128                 result.setException(downResult.cause());
129             }
130             
131         });
132         return result;
133     }
134     
135     /**
136      * 
137      * @return number of connected clients / channels
138      */
139     public int getNumberOfConnections() {
140         return channelInitializer.size();
141     }
142     
143     /**
144      * @return channelInitializer providing channels
145      */
146     public PublishingChannelInitializer getChannelInitializer() {
147         return channelInitializer;
148     }
149     
150     /**
151      * Sets and starts TCPHandler.
152      *
153      * @param args
154      * @throws Exception
155      */
156     public static void main(String[] args) throws Exception {
157         int port;
158         if (args.length > 0) {
159             port = Integer.parseInt(args[0]);
160         } else {
161             port = 6633;
162         }
163         new Thread(new TcpHandler(port)).start();
164     }
165     
166     @Override
167     public ListenableFuture<Boolean> getIsOnlineFuture() {
168         return isOnlineFuture;
169     }
170     
171     /**
172      * @return the port
173      */
174     public int getPort() {
175         return port;
176     }
177     
178     /**
179      * @return the address
180      */
181     public String getAddress() {
182         return address;
183     }
184     
185 }