Check for transport protocol confi
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / UdpHandler.java
index 0d7bb4e5b13783625d9dc138184e762f2c3e9143..1dac7a593f6eb2414ad17fe7d5b4b3267bb27e38 100644 (file)
@@ -12,7 +12,10 @@ import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollDatagramChannel;
+import io.netty.channel.epoll.EpollEventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.DatagramChannel;
 import io.netty.channel.socket.nio.NioDatagramChannel;
 import io.netty.util.concurrent.GenericFutureListener;
 
@@ -28,12 +31,12 @@ import com.google.common.util.concurrent.SettableFuture;
 
 /**
  * Class implementing server over UDP for handling incoming connections.
- * 
+ *
  * @author michal.polkorab
  */
 public final class UdpHandler implements ServerFacade {
 
-    private static final Logger LOGGER = LoggerFactory
+    private static final Logger LOG = LoggerFactory
             .getLogger(UdpHandler.class);
     private int port;
     private EventLoopGroup group;
@@ -41,6 +44,7 @@ public final class UdpHandler implements ServerFacade {
     private final SettableFuture<Boolean> isOnlineFuture;
     private UdpChannelInitializer channelInitializer;
     private ThreadConfiguration threadConfig;
+    private Class<? extends DatagramChannel> datagramChannelClass;
 
     /**
      * Constructor of UdpHandler that listens on selected port.
@@ -64,16 +68,11 @@ public final class UdpHandler implements ServerFacade {
 
     @Override
     public void run() {
-        if (threadConfig != null) {
-            group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
-        } else {
-            group = new NioEventLoopGroup();
-        }
         final ChannelFuture f;
         try {
             Bootstrap b = new Bootstrap();
             b.group(group)
-             .channel(NioDatagramChannel.class)
+             .channel(datagramChannelClass)
              .option(ChannelOption.SO_BROADCAST, false)
              .handler(channelInitializer);
 
@@ -83,7 +82,7 @@ public final class UdpHandler implements ServerFacade {
                 f = b.bind(port).sync();
             }
         } catch (InterruptedException e) {
-            LOGGER.error("Interrupted while binding port {}", port, e);
+            LOG.error("Interrupted while binding port {}", port, e);
             return;
         }
 
@@ -94,12 +93,12 @@ public final class UdpHandler implements ServerFacade {
             // Update port, as it may have been specified as 0
             this.port = isa.getPort();
 
-            LOGGER.debug("Address from udpHandler: {}", address);
+            LOG.debug("Address from udpHandler: {}", address);
             isOnlineFuture.set(true);
-            LOGGER.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
+            LOG.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
             f.channel().closeFuture().sync();
         } catch (InterruptedException e) {
-            LOGGER.error("Interrupted while waiting for port {} shutdown", port, e);
+            LOG.error("Interrupted while waiting for port {} shutdown", port, e);
         } finally {
             shutdown();
         }
@@ -146,4 +145,51 @@ public final class UdpHandler implements ServerFacade {
     public void setThreadConfig(ThreadConfiguration threadConfig) {
         this.threadConfig = threadConfig;
     }
+
+    /**
+     * Initiate event loop groups
+     * @param threadConfiguration number of threads to be created, if not specified in threadConfig
+     */
+    public void initiateEventLoopGroups(ThreadConfiguration threadConfiguration, boolean isEpollEnabled) {
+
+        if(isEpollEnabled) {
+            initiateEpollEventLoopGroups(threadConfiguration);
+        } else {
+            initiateNioEventLoopGroups(threadConfiguration);
+        }
+    }
+
+    /**
+     * Initiate Nio event loop groups
+     * @param threadConfiguration number of threads to be created, if not specified in threadConfig
+     */
+    public void initiateNioEventLoopGroups(ThreadConfiguration threadConfiguration) {
+        datagramChannelClass = NioDatagramChannel.class;
+        if (threadConfiguration != null) {
+            group = new NioEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+        } else {
+            group = new NioEventLoopGroup();
+        }
+    }
+
+    /**
+     * Initiate Epoll event loop groups with Nio as fall back
+     * @param threadConfiguration
+     */
+    protected void initiateEpollEventLoopGroups(ThreadConfiguration threadConfiguration) {
+        try {
+            datagramChannelClass = EpollDatagramChannel.class;
+            if (threadConfiguration != null) {
+                group = new EpollEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+            } else {
+                group = new EpollEventLoopGroup();
+            }
+            return;
+        } catch (Throwable ex) {
+            LOG.debug("Epoll initiation failed");
+        }
+
+        //Fallback mechanism
+        initiateNioEventLoopGroups(threadConfiguration);
+    }
 }
\ No newline at end of file