Added support for switch idle state
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
index 5d342fdecff5daa416c151621193a9765ca1ab33..712f309492b0913e88b320c6f1fd91084720fb73 100644 (file)
@@ -8,12 +8,17 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
+import io.netty.util.concurrent.GenericFutureListener;
 
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 
+import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
+import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 
 /**
@@ -21,10 +26,11 @@ import com.google.common.util.concurrent.SettableFuture;
  *
  * @author michal.polkorab
  */
-public class TcpHandler extends Thread {
+public class TcpHandler implements ServerFacade {
 
     private int port;
     private String address;
+    private InetAddress startupAddress;
     private NioEventLoopGroup workerGroup;
     private NioEventLoopGroup bossGroup;
     private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
@@ -39,7 +45,11 @@ public class TcpHandler extends Thread {
     public static enum COMPONENT_NAMES {
 
         /**
-         * First component in pipeline - detecting TLS connections
+         * Detects switch idle state
+         */
+        IDLE_HANDLER,
+        /**
+         * Detects TLS connections
          */
         TLS_DETECTOR,
         /**
@@ -55,13 +65,17 @@ public class TcpHandler extends Thread {
          */
         OF_VERSION_DETECTOR,
         /**
-         * Transforms OpenFlow Protocol messages
+         * Transforms OpenFlow Protocol byte messages into POJOs
+         */
+        OF_DECODER,
+        /**
+         * Transforms POJOs into OpenFlow Protocol byte messages
          */
-        OF_CODEC,
+        OF_ENCODER,
         /**
-         * Communicates with upper layers (outside OF Library)
+         * Delegates translated POJOs into MessageConsumer
          */
-        OF_FACADE
+        DELEGATING_INBOUND_HANDLER,
     }
     
 
@@ -71,7 +85,17 @@ public class TcpHandler extends Thread {
      * @param port listening port of TCPHandler server
      */
     public TcpHandler(int port) {
+        this(null, port);
+    }
+    
+    /**
+     * Constructor of TCPHandler that listens on selected address and port.
+     * @param address listening address of TCPHandler server
+     * @param port listening port of TCPHandler server
+     */
+    public TcpHandler(InetAddress address, int port) {
         this.port = port;
+        this.startupAddress = address;
         channelInitializer = new PublishingChannelInitializer();
         isOnlineFuture = SettableFuture.create();
     }
@@ -93,10 +117,16 @@ public class TcpHandler extends Thread {
                     .option(ChannelOption.SO_BACKLOG, 128)
                     .childOption(ChannelOption.SO_KEEPALIVE, true);
 
-            ChannelFuture f = b.bind(port).sync();
+            ChannelFuture f;
+            if (startupAddress != null) {
+                f = b.bind(startupAddress.getHostAddress(), port).sync();
+            } else {
+                f = b.bind(port).sync();
+            }
             
             InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
-            address = isa.getHostName().toString();
+            address = isa.getHostString();
+            LOGGER.debug("address from tcphandler: " + address);
             port = isa.getPort();
             isOnlineFuture.set(true);
             LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
@@ -111,9 +141,22 @@ public class TcpHandler extends Thread {
     /**
      * Shuts down {@link TcpHandler}}
      */
-    public void shutdown() {
+    @Override
+    public ListenableFuture<Boolean> shutdown() {
+        final SettableFuture<Boolean> result = SettableFuture.create();
         workerGroup.shutdownGracefully();
-        bossGroup.shutdownGracefully();
+        // boss will shutdown as soon, as worker is down
+        bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
+
+            @Override
+            public void operationComplete(
+                    io.netty.util.concurrent.Future<Object> downResult) throws Exception {
+                result.set(downResult.isSuccess());
+                result.setException(downResult.cause());
+            }
+            
+        });
+        return result;
     }
     
     /**
@@ -131,26 +174,8 @@ public class TcpHandler extends Thread {
         return channelInitializer;
     }
     
-    /**
-     * Sets and starts TCPHandler.
-     *
-     * @param args
-     * @throws Exception
-     */
-    public static void main(String[] args) throws Exception {
-        int port;
-        if (args.length > 0) {
-            port = Integer.parseInt(args[0]);
-        } else {
-            port = 6633;
-        }
-        new TcpHandler(port).start();
-    }
-    
-    /**
-     * @return the isOnlineFuture
-     */
-    public SettableFuture<Boolean> getIsOnlineFuture() {
+    @Override
+    public ListenableFuture<Boolean> getIsOnlineFuture() {
         return isOnlineFuture;
     }
     
@@ -167,5 +192,17 @@ public class TcpHandler extends Thread {
     public String getAddress() {
         return address;
     }
+
+    /**
+     * @param switchConnectionHandler
+     */
+    public void setSwitchConnectionHandler(
+            SwitchConnectionHandler switchConnectionHandler) {
+        channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
+    }
+    
+    public void setSwitchIdleTimeout(long switchIdleTimeout) {
+        channelInitializer.setSwitchIdleTimeout(switchIdleTimeout);
+    }
     
 }