1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
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;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
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;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
25 * Class implementing server over TCP for handling incoming connections.
27 * @author michal.polkorab
29 public class TcpHandler implements ServerFacade {
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;
40 private PublishingChannelInitializer channelInitializer;
43 * Enum used for storing names of used components (in pipeline).
45 public static enum COMPONENT_NAMES {
48 * Detects switch idle state
52 * Detects TLS connections
56 * Component for handling TLS frames
60 * Decodes incoming messages into message frames
64 * Detects version of incoming OpenFlow Protocol message
68 * Transforms OpenFlow Protocol byte messages into POJOs
72 * Transforms POJOs into OpenFlow Protocol byte messages
76 * Delegates translated POJOs into MessageConsumer
78 DELEGATING_INBOUND_HANDLER,
83 * Constructor of TCPHandler that listens on selected port.
85 * @param port listening port of TCPHandler server
87 public TcpHandler(int port) {
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
96 public TcpHandler(InetAddress address, int port) {
98 this.startupAddress = address;
99 channelInitializer = new PublishingChannelInitializer();
100 isOnlineFuture = SettableFuture.create();
104 * Starts server on selected port.
108 bossGroup = new NioEventLoopGroup();
109 workerGroup = new NioEventLoopGroup();
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);
121 if (startupAddress != null) {
122 f = b.bind(startupAddress.getHostAddress(), port).sync();
124 f = b.bind(port).sync();
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);
142 * Shuts down {@link TcpHandler}}
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>>() {
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());
166 * @return number of connected clients / channels
168 public int getNumberOfConnections() {
169 return channelInitializer.size();
173 * @return channelInitializer providing channels
175 public PublishingChannelInitializer getChannelInitializer() {
176 return channelInitializer;
180 public ListenableFuture<Boolean> getIsOnlineFuture() {
181 return isOnlineFuture;
187 public int getPort() {
192 * @return the address
194 public String getAddress() {
199 * @param switchConnectionHandler
201 public void setSwitchConnectionHandler(
202 SwitchConnectionHandler switchConnectionHandler) {
203 channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
207 * @param switchIdleTimeout in milliseconds
209 public void setSwitchIdleTimeout(long switchIdleTimeout) {
210 channelInitializer.setSwitchIdleTimeout(switchIdleTimeout);