Integration test with hadshake
[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          * First component in pipeline - detecting TLS connections
49          */
50         TLS_DETECTOR,
51         /**
52          * Component for handling TLS frames
53          */
54         SSL_HANDLER,
55         /**
56          * Decodes incoming messages into message frames
57          */
58         OF_FRAME_DECODER,
59         /**
60          * Detects version of incoming OpenFlow Protocol message
61          */
62         OF_VERSION_DETECTOR,
63         /**
64          * Transforms OpenFlow Protocol byte messages into POJOs
65          */
66         OF_DECODER,
67         /**
68          * Transforms POJOs into OpenFlow Protocol byte messages
69          */
70         OF_ENCODER,
71         /**
72          * Delegates translated POJOs into MessageConsumer
73          */
74         DELEGATING_INBOUND_HANDLER,
75     }
76     
77
78     /**
79      * Constructor of TCPHandler that listens on selected port.
80      *
81      * @param port listening port of TCPHandler server
82      */
83     public TcpHandler(int port) {
84         this(null, port);
85     }
86     
87     /**
88      * Constructor of TCPHandler that listens on selected address and port.
89      * @param address listening address of TCPHandler server
90      * @param port listening port of TCPHandler server
91      */
92     public TcpHandler(InetAddress address, int port) {
93         this.port = port;
94         this.startupAddress = address;
95         channelInitializer = new PublishingChannelInitializer();
96         isOnlineFuture = SettableFuture.create();
97     }
98
99     /**
100      * Starts server on selected port.
101      */
102     @Override
103     public void run() {
104         LOGGER.info("Switch ");
105         bossGroup = new NioEventLoopGroup();
106         workerGroup = new NioEventLoopGroup();
107         try {
108             ServerBootstrap b = new ServerBootstrap();
109             b.group(bossGroup, workerGroup)
110                     .channel(NioServerSocketChannel.class)
111                     .handler(new LoggingHandler(LogLevel.DEBUG))
112                     .childHandler(channelInitializer)
113                     .option(ChannelOption.SO_BACKLOG, 128)
114                     .childOption(ChannelOption.SO_KEEPALIVE, true);
115
116             ChannelFuture f;
117             if (startupAddress != null) {
118                 f = b.bind(startupAddress.getHostAddress(), port).sync();
119             } else {
120                 f = b.bind(port).sync();
121             }
122             
123             InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
124             address = isa.getHostString();
125             LOGGER.debug("address from tcphandler: " + address);
126             port = isa.getPort();
127             isOnlineFuture.set(true);
128             LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
129             f.channel().closeFuture().sync();
130         } catch (InterruptedException ex) {
131             LOGGER.error(ex.getMessage(), ex);
132         } finally {
133             shutdown();
134         }
135     }
136
137     /**
138      * Shuts down {@link TcpHandler}}
139      */
140     @Override
141     public ListenableFuture<Boolean> shutdown() {
142         final SettableFuture<Boolean> result = SettableFuture.create();
143         workerGroup.shutdownGracefully();
144         // boss will shutdown as soon, as worker is down
145         bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
146
147             @Override
148             public void operationComplete(
149                     io.netty.util.concurrent.Future<Object> downResult) throws Exception {
150                 result.set(downResult.isSuccess());
151                 result.setException(downResult.cause());
152             }
153             
154         });
155         return result;
156     }
157     
158     /**
159      * 
160      * @return number of connected clients / channels
161      */
162     public int getNumberOfConnections() {
163         return channelInitializer.size();
164     }
165     
166     /**
167      * @return channelInitializer providing channels
168      */
169     public PublishingChannelInitializer getChannelInitializer() {
170         return channelInitializer;
171     }
172     
173     @Override
174     public ListenableFuture<Boolean> getIsOnlineFuture() {
175         return isOnlineFuture;
176     }
177     
178     /**
179      * @return the port
180      */
181     public int getPort() {
182         return port;
183     }
184     
185     /**
186      * @return the address
187      */
188     public String getAddress() {
189         return address;
190     }
191
192     /**
193      * @param switchConnectionHandler
194      */
195     public void setSwitchConnectionHandler(
196             SwitchConnectionHandler switchConnectionHandler) {
197         channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
198     }
199     
200 }