Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
index 2d12c5c7657f1fd2fb7c2070282e2963ee4012e0..941518d69c8de81600ec4582f1c5635a04971f2a 100644 (file)
@@ -1,4 +1,11 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.openflowjava.protocol.impl.core;
 
 import io.netty.bootstrap.ServerBootstrap;
@@ -10,8 +17,10 @@ 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;
@@ -28,6 +37,7 @@ 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);
@@ -42,7 +52,11 @@ public class TcpHandler implements ServerFacade {
     public static enum COMPONENT_NAMES {
 
         /**
-         * First component in pipeline - detecting TLS connections
+         * Detects switch idle state
+         */
+        IDLE_HANDLER,
+        /**
+         * Detects TLS connections
          */
         TLS_DETECTOR,
         /**
@@ -58,13 +72,17 @@ public class TcpHandler implements ServerFacade {
          */
         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,
     }
     
 
@@ -74,7 +92,17 @@ public class TcpHandler implements ServerFacade {
      * @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();
     }
@@ -84,7 +112,6 @@ public class TcpHandler implements ServerFacade {
      */
     @Override
     public void run() {
-        LOGGER.info("Switch ");
         bossGroup = new NioEventLoopGroup();
         workerGroup = new NioEventLoopGroup();
         try {
@@ -94,12 +121,19 @@ public class TcpHandler implements ServerFacade {
                     .handler(new LoggingHandler(LogLevel.DEBUG))
                     .childHandler(channelInitializer)
                     .option(ChannelOption.SO_BACKLOG, 128)
+                    .option(ChannelOption.SO_REUSEADDR, true)
                     .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);
@@ -125,7 +159,9 @@ public class TcpHandler implements ServerFacade {
             public void operationComplete(
                     io.netty.util.concurrent.Future<Object> downResult) throws Exception {
                 result.set(downResult.isSuccess());
-                result.setException(downResult.cause());
+                if (downResult.cause() != null) {
+                    result.setException(downResult.cause());
+                }
             }
             
         });
@@ -147,22 +183,6 @@ public class TcpHandler implements ServerFacade {
         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 Thread(new TcpHandler(port)).start();
-    }
-    
     @Override
     public ListenableFuture<Boolean> getIsOnlineFuture() {
         return isOnlineFuture;
@@ -181,5 +201,27 @@ public class TcpHandler implements ServerFacade {
     public String getAddress() {
         return address;
     }
+
+    /**
+     * @param switchConnectionHandler
+     */
+    public void setSwitchConnectionHandler(
+            SwitchConnectionHandler switchConnectionHandler) {
+        channelInitializer.setSwitchConnectionHandler(switchConnectionHandler);
+    }
+    
+    /**
+     * @param switchIdleTimeout in milliseconds
+     */
+    public void setSwitchIdleTimeout(long switchIdleTimeout) {
+        channelInitializer.setSwitchIdleTimeout(switchIdleTimeout);
+    }
+
+    /**
+     * @param tlsSupported
+     */
+    public void setEncryption(boolean tlsSupported) {
+        channelInitializer.setEncryption(tlsSupported);
+    }
     
 }