ofp_packet_queue structure - implemented missing fields
[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.InetAddress;
14 import java.net.InetSocketAddress;
15
16 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
17 import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
23
24 /**
25  * Class implementing server over TCP for handling incoming connections.
26  *
27  * @author michal.polkorab
28  */
29 public class TcpHandler implements ServerFacade {
30
31     private int port;
32     private String address;
33     private InetAddress startupAddress;
34     private NioEventLoopGroup workerGroup;
35     private NioEventLoopGroup bossGroup;
36     private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
37     private SettableFuture<Boolean> isOnlineFuture;
38     
39     
40     private PublishingChannelInitializer channelInitializer;
41
42     /**
43      * Enum used for storing names of used components (in pipeline).
44      */
45     public static enum COMPONENT_NAMES {
46
47         /**
48          * Detects switch idle state
49          */
50         IDLE_HANDLER,
51         /**
52          * Detects TLS connections
53          */
54         TLS_DETECTOR,
55         /**
56          * Component for handling TLS frames
57          */
58         SSL_HANDLER,
59         /**
60          * Decodes incoming messages into message frames
61          */
62         OF_FRAME_DECODER,
63         /**
64          * Detects version of incoming OpenFlow Protocol message
65          */
66         OF_VERSION_DETECTOR,
67         /**
68          * Transforms OpenFlow Protocol byte messages into POJOs
69          */
70         OF_DECODER,
71         /**
72          * Transforms POJOs into OpenFlow Protocol byte messages
73          */
74         OF_ENCODER,
75         /**
76          * Delegates translated POJOs into MessageConsumer
77          */
78         DELEGATING_INBOUND_HANDLER,
79     }
80     
81
82     /**
83      * Constructor of TCPHandler that listens on selected port.
84      *
85      * @param port listening port of TCPHandler server
86      */
87     public TcpHandler(int port) {
88         this(null, port);
89     }
90     
91     /**
92      * Constructor of TCPHandler that listens on selected address and port.
93      * @param address listening address of TCPHandler server
94      * @param port listening port of TCPHandler server
95      */
96     public TcpHandler(InetAddress address, int port) {
97         this.port = port;
98         this.startupAddress = address;
99         channelInitializer = new PublishingChannelInitializer();
100         isOnlineFuture = SettableFuture.create();
101     }
102
103     /**
104      * Starts server on selected port.
105      */
106     @Override
107     public void run() {
108         bossGroup = new NioEventLoopGroup();
109         workerGroup = new NioEventLoopGroup();
110         try {
111             ServerBootstrap b = new ServerBootstrap();
112             b.group(bossGroup, workerGroup)
113                     .channel(NioServerSocketChannel.class)
114                     .handler(new LoggingHandler(LogLevel.DEBUG))
115                     .childHandler(channelInitializer)
116                     .option(ChannelOption.SO_BACKLOG, 128)
117                     .option(ChannelOption.SO_REUSEADDR, true)
118                     .childOption(ChannelOption.SO_KEEPALIVE, true);
119
120             ChannelFuture f;
121             if (startupAddress != null) {
122                 f = b.bind(startupAddress.getHostAddress(), port).sync();
123             } else {
124                 f = b.bind(port).sync();
125             }
126             
127             InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
128             address = isa.getHostString();
129             LOGGER.debug("address from tcphandler: " + address);
130             port = isa.getPort();
131             isOnlineFuture.set(true);
132             LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
133             f.channel().closeFuture().sync();
134         } catch (InterruptedException ex) {
135             LOGGER.error(ex.getMessage(), ex);
136         } finally {
137             shutdown();
138         }
139     }
140
141     /**
142      * Shuts down {@link TcpHandler}}
143      */
144     @Override
145     public ListenableFuture<Boolean> shutdown() {
146         final SettableFuture<Boolean> result = SettableFuture.create();
147         workerGroup.shutdownGracefully();
148         // boss will shutdown as soon, as worker is down
149         bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
150
151             @Override
152             public void operationComplete(
153                     io.netty.util.concurrent.Future<Object> downResult) throws Exception {
154                 result.set(downResult.isSuccess());
155                 if (downResult.cause() != null) {
156                     result.setException(downResult.cause());
157                 }
158             }
159             
160         });
161         return result;
162     }
163     
164     /**
165      * 
166      * @return number of connected clients / channels
167      */
168     public int getNumberOfConnections() {
169         return channelInitializer.size();
170     }
171     
172     /**
173      * @return channelInitializer providing channels
174      */
175     public PublishingChannelInitializer getChannelInitializer() {
176         return channelInitializer;
177     }
178     
179     @Override
180     public ListenableFuture<Boolean> getIsOnlineFuture() {
181         return isOnlineFuture;
182     }
183     
184     /**
185      * @return the port
186      */
187     public int getPort() {
188         return port;
189     }
190     
191     /**
192      * @return the address
193      */
194     public String getAddress() {
195         return address;
196     }
197
198     /**
199      * @param switchConnectionHandler
200      */
201     public void setSwitchConnectionHandler(
202             SwitchConnectionHandler switchConnectionHandler) {
203         channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
204     }
205     
206     /**
207      * @param switchIdleTimeout in milliseconds
208      */
209     public void setSwitchIdleTimeout(long switchIdleTimeout) {
210         channelInitializer.setSwitchIdleTimeout(switchIdleTimeout);
211     }
212     
213 }