Enable SSL connection for ovsdb server. 62/12362/4
authorHsin-Yi Shen <syshen66@gmail.com>
Wed, 29 Oct 2014 23:52:57 +0000 (16:52 -0700)
committerHsin-Yi Shen <syshen66@gmail.com>
Fri, 31 Oct 2014 21:59:56 +0000 (14:59 -0700)
Change-Id: I61213343be3aec2907b9b5d3b1f3446335d55fde
Signed-off-by: Hsin-Yi Shen <syshen66@gmail.com>
library/src/main/java/org/opendaylight/ovsdb/lib/OvsdbConnection.java
library/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java

index 5aa34da16fe0dc5d4e4f649f432f6612312c8d1e..05e94ccb22fa7604701e8fb7277ab4503a59b0ca 100644 (file)
@@ -12,6 +12,7 @@ package org.opendaylight.ovsdb.lib;
 
 import java.net.InetAddress;
 import java.util.Collection;
+import io.netty.handler.ssl.SslContext;
 
 /**
  * OvsDBConnection Interface provides OVSDB connection management APIs which includes
@@ -34,7 +35,18 @@ public interface OvsdbConnection {
      * @param port Layer 4 port on which the remote ovsdb server is listening on.
      * @return OvsDBClient The primary Client interface for the ovsdb connection.
      */
-    public OvsdbClient connect(InetAddress address, int port);
+    public OvsdbClient connect(final InetAddress address, final int port);
+
+    /**
+     * connect API can be used by the applications to initiate Active ssl
+     * connection from the controller towards ovsdb-server
+     * @param address IP Address of the remote server that hosts the ovsdb server.
+     * @param port Layer 4 port on which the remote ovsdb server is listening on.
+     * @param sslContext Netty sslContext for channel configuration
+     * @return OvsDBClient The primary Client interface for the ovsdb connection.
+     */
+    public OvsdbClient connectWithSsl(final InetAddress address, final int port,
+                                      final SslContext sslContext);
 
     /**
      * Method to disconnect an existing connection.
@@ -47,6 +59,12 @@ public interface OvsdbConnection {
      */
     public boolean startOvsdbManager(final int ovsdbListenPort);
 
+    /**
+     * Method to start ovsdb server for passive connection with SSL
+     */
+    public boolean startOvsdbManagerWithSsl(final int ovsdbListenPort,
+                                     final SslContext sslContext);
+
     /**
      * Method to register a Passive Connection Listener with the ConnectionService.
      * @param listener Passive Connection listener interested in Passive OVSDB connection requests.
index c379cf74159bc1a85515cd7dc992a7ab33362f07..fd88677d4c677742d261a2bd7ca2181887e7a05c 100644 (file)
@@ -26,6 +26,7 @@ import io.netty.handler.codec.string.StringEncoder;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
 import io.netty.util.CharsetUtil;
+import io.netty.handler.ssl.SslContext;
 
 import java.net.InetAddress;
 import java.util.Collection;
@@ -86,7 +87,12 @@ public class OvsdbConnectionService implements OvsdbConnection {
         return connectionService;
     }
     @Override
-    public OvsdbClient connect(InetAddress address, int port) {
+    public OvsdbClient connect(final InetAddress address, final int port) {
+        return connectWithSsl(address, port, null /* SslContext */);
+    }
+    @Override
+    public OvsdbClient connectWithSsl(final InetAddress address, final int port,
+                               final SslContext sslContext) {
         try {
             Bootstrap bootstrap = new Bootstrap();
             bootstrap.group(new NioEventLoopGroup());
@@ -97,6 +103,11 @@ public class OvsdbConnectionService implements OvsdbConnection {
             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel channel) throws Exception {
+                    if (sslContext != null) {
+                        /* First add ssl handler if ssl context is given */
+                        channel.pipeline().addLast(sslContext.newHandler(channel.alloc(),
+                                                   address.toString(), port));
+                    }
                     channel.pipeline().addLast(
                             //new LoggingHandler(LogLevel.INFO),
                             new JsonRpcDecoder(100000),
@@ -177,10 +188,41 @@ public class OvsdbConnectionService implements OvsdbConnection {
     }
 
     /**
-     * OVSDB Passive listening thread that uses Netty ServerBootstrap to open passive connection
-     * and handle channel callbacks.
+     * Method that initiates the Passive OVSDB channel listening functionality
+     * with ssl.By default the ovsdb passive connection will listen in port
+     * 6640 which can be overridden using the ovsdb.listenPort system property.
+     */
+    @Override
+    synchronized
+    public boolean startOvsdbManagerWithSsl(final int ovsdbListenPort,
+                                     final SslContext sslContext) {
+        if (!singletonCreated) {
+            new Thread() {
+                @Override
+                public void run() {
+                    ovsdbManagerWithSsl(ovsdbListenPort, sslContext);
+                }
+            }.start();
+            singletonCreated = true;
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * OVSDB Passive listening thread that uses Netty ServerBootstrap to open
+     * passive connection handle channel callbacks.
      */
     private static void ovsdbManager(int port) {
+        ovsdbManagerWithSsl(port, null /* SslContext */);
+    }
+
+    /**
+     * OVSDB Passive listening thread that uses Netty ServerBootstrap to open
+     * passive connection with Ssl and handle channel callbacks.
+     */
+    private static void ovsdbManagerWithSsl(int port, final SslContext sslContext) {
         EventLoopGroup bossGroup = new NioEventLoopGroup();
         EventLoopGroup workerGroup = new NioEventLoopGroup();
         try {
@@ -193,6 +235,11 @@ public class OvsdbConnectionService implements OvsdbConnection {
                  @Override
                  public void initChannel(SocketChannel channel) throws Exception {
                      logger.debug("New Passive channel created : "+ channel.toString());
+                     if (sslContext != null) {
+                         /* Add SSL handler first if SSL context is provided */
+                         channel.pipeline().addLast(sslContext.newHandler(channel.alloc()));
+                     }
+
                      channel.pipeline().addLast(
                              new JsonRpcDecoder(100000),
                              new StringEncoder(CharsetUtil.UTF_8),