First Plugin integration with the Library starting with ConnectionService and Invento... 61/2261/1
authorMadhu Venugopal <mavenugo@gmail.com>
Tue, 29 Oct 2013 20:09:13 +0000 (13:09 -0700)
committerMadhu Venugopal <mavenugo@gmail.com>
Tue, 29 Oct 2013 20:09:13 +0000 (13:09 -0700)
Also,
1. removed the UnitTest dependancy on ChannelHandlers and moved directly under ConnectionService.
2. renamed OVSDB to OvsdbRPC
3. Modified OVSDBNettyFactoryTest to accomodate the above integration and refactoring.

Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
ovsdb/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java
ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OvsdbRPC.java [moved from ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OVSDB.java with 98% similarity]
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Connection.java
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/InventoryService.java
ovsdb/src/test/java/org/opendaylight/ovsdb/lib/message/OVSDBNettyFactoryTest.java

index 2b9bab06c1a14dfab261947457e04dbf7a3e8562..67f4535d5df45a6a63be7cbdeab6a57bfd16de37 100644 (file)
@@ -12,7 +12,7 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 
 import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.ovsdb.lib.message.OVSDB;
+import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -54,7 +54,7 @@ public class JsonRpcEndpoint {
     ObjectMapper objectMapper;
     Channel nettyChannel;
     Map<String, CallContext> methodContext = Maps.newHashMap();
-    Map<Node, OVSDB.Callback> requestCallbacks = Maps.newHashMap();
+    Map<Node, OvsdbRPC.Callback> requestCallbacks = Maps.newHashMap();
 
     public JsonRpcEndpoint(ObjectMapper objectMapper, Channel channel) {
         this.objectMapper = objectMapper;
@@ -66,9 +66,9 @@ public class JsonRpcEndpoint {
         return Reflection.newProxy(klazz, new InvocationHandler() {
             @Override
             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-                if (method.getName().equals(OVSDB.REGISTER_CALLBACK_METHOD)) {
-                    if ((args == null) || args.length != 1 || !(args[0] instanceof OVSDB.Callback)) return false;
-                    requestCallbacks.put(node, (OVSDB.Callback)args[0]);
+                if (method.getName().equals(OvsdbRPC.REGISTER_CALLBACK_METHOD)) {
+                    if ((args == null) || args.length != 1 || !(args[0] instanceof OvsdbRPC.Callback)) return false;
+                    requestCallbacks.put(node, (OvsdbRPC.Callback)args[0]);
                     return true;
                 }
 
@@ -137,7 +137,7 @@ public class JsonRpcEndpoint {
         JsonRpc10Request request = new JsonRpc10Request(requestJson.get("id").asText());
         request.setMethod(requestJson.get("method").asText());
         logger.debug("Request : {} {}", requestJson.get("method"), requestJson.get("params"));
-        OVSDB.Callback callback = requestCallbacks.get(node);
+        OvsdbRPC.Callback callback = requestCallbacks.get(node);
         if (callback != null) {
             Method[] methods = callback.getClass().getDeclaredMethods();
             for (Method m : methods) {
similarity index 98%
rename from ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OVSDB.java
rename to ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OvsdbRPC.java
index cf5d582fccd0ae96d85885d5b8f4c6e1192b3f39..ad17b3c4932cd5cd417c1fe15154087dd1a148be 100644 (file)
@@ -8,7 +8,7 @@ import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.ovsdb.lib.database.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.message.operations.OperationResult;
 
-public interface OVSDB {
+public interface OvsdbRPC {
     public static final String REGISTER_CALLBACK_METHOD = "registerCallback";
 
     public ListenableFuture<DatabaseSchema> get_schema(List<String> db_names);
index 2f94cce667181f3f53e29b7c662e239062748b29..ddb5fbce76e959852a72b789528e7cd49772a137 100755 (executable)
@@ -1,9 +1,11 @@
 package org.opendaylight.ovsdb.plugin;
 
 import io.netty.channel.Channel;
+
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.sal.utils.StatusCode;
+import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -13,6 +15,7 @@ public class Connection {
     private Node node;
     private String identifier;
     private Channel channel;
+    private OvsdbRPC rpc;
 
     public Long getIdCounter() {
         return idCounter;
@@ -65,6 +68,14 @@ public class Connection {
         this.node = node;
     }
 
+    public OvsdbRPC getRpc() {
+        return rpc;
+    }
+
+    public void setRpc(OvsdbRPC rpc) {
+        this.rpc = rpc;
+    }
+
     public void sendMessage(String message) throws IOException {
         try {
             channel.writeAndFlush(message);
@@ -76,7 +87,6 @@ public class Connection {
 
     public Status disconnect() {
         try {
-            channel.pipeline().get("messageHandler");
             channel.close();
         } catch (Exception e) {
             e.printStackTrace();
index 623d899ea340935e76f156710b609a3235ce5721..28c7671d75ce5c1b982603340180b4ee6ab34eec 100755 (executable)
@@ -18,15 +18,20 @@ import org.opendaylight.controller.sal.utils.StatusCode;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
+import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
+import org.opendaylight.ovsdb.lib.message.UpdateNotification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.collect.Lists;
 
 import java.net.InetAddress;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -35,13 +40,17 @@ import java.util.concurrent.ConcurrentMap;
  * Represents the openflow plugin component in charge of programming the flows
  * the flow programming and relay them to functional modules above SAL.
  */
-public class ConnectionService implements IPluginInConnectionService, IConnectionServiceInternal {
+public class ConnectionService implements IPluginInConnectionService, IConnectionServiceInternal, OvsdbRPC.Callback {
     protected static final Logger logger = LoggerFactory.getLogger(ConnectionService.class);
 
     private static final Integer defaultOvsdbPort = 6632;
-    ConcurrentMap<String, Connection> ovsdbConnections;
-    List<ChannelHandler> handlers = null;
-    InventoryServiceInternal inventoryServiceInternal;
+    private ConcurrentMap<String, Connection> ovsdbConnections;
+    private List<ChannelHandler> handlers = null;
+    private InventoryServiceInternal inventoryServiceInternal;
+
+    public InventoryServiceInternal getInventoryServiceInternal() {
+        return inventoryServiceInternal;
+    }
 
     public void setInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
         this.inventoryServiceInternal = inventoryServiceInternal;
@@ -117,20 +126,15 @@ public class ConnectionService implements IPluginInConnectionService, IConnectio
             bootstrap.channel(NioSocketChannel.class);
             bootstrap.option(ChannelOption.TCP_NODELAY, true);
             bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
+
             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel channel) throws Exception {
-//                  ObjectMapper objectMapper = new ObjectMapper();
-//                  JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, ConnectionService.this);
-                  /*Add new Handlers here.
-                  Break out into todo break out into channel Init Class*/
                     if (handlers == null) {
                         channel.pipeline().addLast(
                                 new LoggingHandler(LogLevel.INFO),
                                 new JsonRpcDecoder(100000),
-                                new StringEncoder(CharsetUtil.UTF_8),
-    //                            new JsonRpcServiceBinderHandler(factory),
-                                new MessageHandler());
+                                new StringEncoder(CharsetUtil.UTF_8));
                     } else {
                         for (ChannelHandler handler : handlers) {
                             channel.pipeline().addLast(handler);
@@ -138,14 +142,27 @@ public class ConnectionService implements IPluginInConnectionService, IConnectio
                     }
                 }
             });
+
             ChannelFuture future = bootstrap.connect(address, port).sync();
 
             Channel channel = future.channel();
             Connection connection = new Connection(identifier, channel);
+            Node node = connection.getNode();
 
-            ovsdbConnections.put(identifier, connection);
-            return connection.getNode();
+            ObjectMapper objectMapper = new ObjectMapper();
+            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
+            JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
+            JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
+            binderHandler.setNode(node);
+            channel.pipeline().addLast(binderHandler);
+
+            OvsdbRPC ovsdb = factory.getClient(node, OvsdbRPC.class);
+            connection.setRpc(ovsdb);
+            ovsdb.registerCallback(this);
+
+            ovsdbConnections.put(identifier, connection);
+            return node;
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -174,4 +191,21 @@ public class ConnectionService implements IPluginInConnectionService, IConnectio
     @Override
     public void notifyNodeDisconnectFromMaster(Node arg0) {
     }
+
+    @Override
+    public void update(Node node, UpdateNotification updateNotification) {
+        inventoryServiceInternal.processTableUpdates(node, updateNotification.getUpdate());
+        inventoryServiceInternal.printCache(node);
+    }
+
+    @Override
+    public void locked(Node node, List<String> ids) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void stolen(Node node, List<String> ids) {
+        // TODO Auto-generated method stub
+    }
+
 }
index 40918d893dcaf1eec135e6d0df5a341888580811..710b68cc04f36884b3e42861dfb624877aabbfef 100755 (executable)
@@ -37,10 +37,9 @@ public class InventoryService implements IPluginInInventoryService, InventorySer
      * dependencies are satisfied
      *
      */
-    void init() {
+    public void init() {
         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
-        dbCache = new ConcurrentHashMap<Node, NodeDB<Table<?>>>();
         Node.NodeIDType.registerIDType("OVS", String.class);
         NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class, "OVS");
     }
@@ -51,7 +50,7 @@ public class InventoryService implements IPluginInInventoryService, InventorySer
      * example bundle is being stopped.
      *
      */
-    void destroy() {
+    public void destroy() {
     }
 
     /**
@@ -59,7 +58,7 @@ public class InventoryService implements IPluginInInventoryService, InventorySer
      * the services provided by the class are registered in the service registry
      *
      */
-    void start() {
+    public void start() {
     }
 
     /**
@@ -68,7 +67,7 @@ public class InventoryService implements IPluginInInventoryService, InventorySer
      * calls
      *
      */
-    void stop() {
+    public void stop() {
     }
 
     /**
index 8ba757ed753b6ce41c478114950188a7b75e55c8..2f59f9dfb6b40e983fa0eeafc9c644f8c5a544a4 100644 (file)
@@ -20,7 +20,7 @@ import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
-import org.opendaylight.ovsdb.lib.message.OVSDB;
+import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.message.operations.InsertOperation;
 import org.opendaylight.ovsdb.lib.message.operations.MutateOperation;
@@ -37,6 +37,7 @@ import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
 import org.opendaylight.ovsdb.lib.table.Port;
 import org.opendaylight.ovsdb.lib.table.internal.Table;
 import org.opendaylight.ovsdb.lib.table.internal.Tables;
+import org.opendaylight.ovsdb.plugin.Connection;
 import org.opendaylight.ovsdb.plugin.ConnectionService;
 import org.opendaylight.ovsdb.plugin.InventoryService;
 import org.opendaylight.ovsdb.plugin.InventoryServiceInternal;
@@ -48,21 +49,17 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 
-public class OVSDBNettyFactoryTest implements OVSDB.Callback {
-    InventoryServiceInternal inventoryService;
+public class OVSDBNettyFactoryTest {
+    InventoryService inventoryService;
     private static String bridgeIdentifier = "br1";
 
     @Test
     public void testSome() throws InterruptedException, ExecutionException {
         ConnectionService connectionService = new ConnectionService();
         connectionService.init();
-        List<ChannelHandler> _handlers = Lists.newArrayList();
-        _handlers.add(new LoggingHandler(LogLevel.INFO));
-        _handlers.add(new JsonRpcDecoder(100000));
-        _handlers.add(new StringEncoder(CharsetUtil.UTF_8));
-        connectionService.setHandlers(_handlers);
-
         inventoryService = new InventoryService();
+        inventoryService.init();
+        connectionService.setInventoryServiceInternal(inventoryService);
         Node.NodeIDType.registerIDType("OVS", String.class);
         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
         params.put(ConnectionConstants.ADDRESS, "192.168.56.101");
@@ -73,16 +70,17 @@ public class OVSDBNettyFactoryTest implements OVSDB.Callback {
             return;
         }
 
-        ObjectMapper objectMapper = new ObjectMapper();
-        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-        Channel channel = connectionService.getConnection(node).getChannel();
-        JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
-        JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
-        binderHandler.setNode(node);
-        channel.pipeline().addLast(binderHandler);
+        Connection connection = connectionService.getConnection(node);
+        if (connection == null) {
+            System.out.println("ERROR : Unable to connect to the host");
+            return;
+        }
 
-        OVSDB ovsdb = factory.getClient(node, OVSDB.class);
-        ovsdb.registerCallback(this);
+        OvsdbRPC ovsdb = connection.getRpc();
+        if (ovsdb == null) {
+            System.out.println("ERROR : Unable to obtain RPC instance");
+            return;
+        }
 
         //GET DB-SCHEMA
         List<String> dbNames = Arrays.asList(Open_vSwitch.NAME.getName());
@@ -179,20 +177,4 @@ public class OVSDBNettyFactoryTest implements OVSDB.Callback {
 
         connectionService.disconnect(node);
     }
-
-    @Override
-    public void update(Node node, UpdateNotification updateNotification) {
-        inventoryService.processTableUpdates(node, updateNotification.getUpdate());
-        inventoryService.printCache(node);
-    }
-
-    @Override
-    public void locked(Node node, List<String> ids) {
-        // TODO Auto-generated method stub
-    }
-
-    @Override
-    public void stolen(Node node, List<String> ids) {
-        // TODO Auto-generated method stub
-    }
 }