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 LOGGER.info("Switch ");
109 bossGroup = new NioEventLoopGroup();
110 workerGroup = new NioEventLoopGroup();
112 ServerBootstrap b = new ServerBootstrap();
113 b.group(bossGroup, workerGroup)
114 .channel(NioServerSocketChannel.class)
115 .handler(new LoggingHandler(LogLevel.DEBUG))
116 .childHandler(channelInitializer)
117 .option(ChannelOption.SO_BACKLOG, 128)
118 .option(ChannelOption.SO_REUSEADDR, true)
119 .childOption(ChannelOption.SO_KEEPALIVE, true);
122 if (startupAddress != null) {
123 f = b.bind(startupAddress.getHostAddress(), port).sync();
125 f = b.bind(port).sync();
128 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
129 address = isa.getHostString();
130 LOGGER.debug("address from tcphandler: " + address);
131 port = isa.getPort();
132 isOnlineFuture.set(true);
133 LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
134 f.channel().closeFuture().sync();
135 } catch (InterruptedException ex) {
136 LOGGER.error(ex.getMessage(), ex);
143 * Shuts down {@link TcpHandler}}
146 public ListenableFuture<Boolean> shutdown() {
147 final SettableFuture<Boolean> result = SettableFuture.create();
148 workerGroup.shutdownGracefully();
149 // boss will shutdown as soon, as worker is down
150 bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
153 public void operationComplete(
154 io.netty.util.concurrent.Future<Object> downResult) throws Exception {
155 result.set(downResult.isSuccess());
156 result.setException(downResult.cause());
165 * @return number of connected clients / channels
167 public int getNumberOfConnections() {
168 return channelInitializer.size();
172 * @return channelInitializer providing channels
174 public PublishingChannelInitializer getChannelInitializer() {
175 return channelInitializer;
179 public ListenableFuture<Boolean> getIsOnlineFuture() {
180 return isOnlineFuture;
186 public int getPort() {
191 * @return the address
193 public String getAddress() {
198 * @param switchConnectionHandler
200 public void setSwitchConnectionHandler(
201 SwitchConnectionHandler switchConnectionHandler) {
202 channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
205 public void setSwitchIdleTimeout(long switchIdleTimeout) {
206 channelInitializer.setSwitchIdleTimeout(switchIdleTimeout);