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 * First component in pipeline - detecting TLS connections
52 * Component for handling TLS frames
56 * Decodes incoming messages into message frames
60 * Detects version of incoming OpenFlow Protocol message
64 * Transforms OpenFlow Protocol byte messages into POJOs
68 * Transforms POJOs into OpenFlow Protocol byte messages
72 * Delegates translated POJOs into MessageConsumer
74 DELEGATING_INBOUND_HANDLER,
79 * Constructor of TCPHandler that listens on selected port.
81 * @param port listening port of TCPHandler server
83 public TcpHandler(int port) {
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
92 public TcpHandler(InetAddress address, int port) {
94 this.startupAddress = address;
95 channelInitializer = new PublishingChannelInitializer();
96 isOnlineFuture = SettableFuture.create();
100 * Starts server on selected port.
104 LOGGER.info("Switch ");
105 bossGroup = new NioEventLoopGroup();
106 workerGroup = new NioEventLoopGroup();
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);
117 if (startupAddress != null) {
118 f = b.bind(startupAddress.getHostAddress(), port).sync();
120 f = b.bind(port).sync();
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);
138 * Shuts down {@link TcpHandler}}
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>>() {
148 public void operationComplete(
149 io.netty.util.concurrent.Future<Object> downResult) throws Exception {
150 result.set(downResult.isSuccess());
151 result.setException(downResult.cause());
160 * @return number of connected clients / channels
162 public int getNumberOfConnections() {
163 return channelInitializer.size();
167 * @return channelInitializer providing channels
169 public PublishingChannelInitializer getChannelInitializer() {
170 return channelInitializer;
174 * Sets and starts TCPHandler.
179 public static void main(String[] args) throws Exception {
181 if (args.length > 0) {
182 port = Integer.parseInt(args[0]);
186 new Thread(new TcpHandler(port)).start();
190 public ListenableFuture<Boolean> getIsOnlineFuture() {
191 return isOnlineFuture;
197 public int getPort() {
202 * @return the address
204 public String getAddress() {
209 * @param switchConnectionHandler
211 public void setSwitchConnectionHandler(
212 SwitchConnectionHandler switchConnectionHandler) {
213 channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);