From: Madhu Venugopal Date: Tue, 29 Oct 2013 20:09:13 +0000 (-0700) Subject: First Plugin integration with the Library starting with ConnectionService and Invento... X-Git-Tag: master-tagforprepareonly-ovsdb-bulk-release-prepare-only-6~80^2~13 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F61%2F2261%2F1;p=ovsdb.git First Plugin integration with the Library starting with ConnectionService and InventoryService 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 --- diff --git a/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java b/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java index 2b9bab06c..67f4535d5 100644 --- a/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java +++ b/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java @@ -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 methodContext = Maps.newHashMap(); - Map requestCallbacks = Maps.newHashMap(); + Map 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) { diff --git a/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OVSDB.java b/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OvsdbRPC.java 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 cf5d582fc..ad17b3c49 100644 --- a/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OVSDB.java +++ b/ovsdb/src/main/java/org/opendaylight/ovsdb/lib/message/OvsdbRPC.java @@ -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 get_schema(List db_names); diff --git a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Connection.java b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Connection.java index 2f94cce66..ddb5fbce7 100755 --- a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Connection.java +++ b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Connection.java @@ -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(); diff --git a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java index 623d899ea..28c7671d7 100755 --- a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java +++ b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java @@ -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 ovsdbConnections; - List handlers = null; - InventoryServiceInternal inventoryServiceInternal; + private ConcurrentMap ovsdbConnections; + private List 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() { @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 ids) { + // TODO Auto-generated method stub + } + + @Override + public void stolen(Node node, List ids) { + // TODO Auto-generated method stub + } + } diff --git a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/InventoryService.java b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/InventoryService.java index 40918d893..710b68cc0 100755 --- a/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/InventoryService.java +++ b/ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/InventoryService.java @@ -37,10 +37,9 @@ public class InventoryService implements IPluginInInventoryService, InventorySer * dependencies are satisfied * */ - void init() { + public void init() { nodeProps = new ConcurrentHashMap>(); nodeConnectorProps = new ConcurrentHashMap>(); - dbCache = new ConcurrentHashMap>>(); 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() { } /** diff --git a/ovsdb/src/test/java/org/opendaylight/ovsdb/lib/message/OVSDBNettyFactoryTest.java b/ovsdb/src/test/java/org/opendaylight/ovsdb/lib/message/OVSDBNettyFactoryTest.java index 8ba757ed7..2f59f9dfb 100644 --- a/ovsdb/src/test/java/org/opendaylight/ovsdb/lib/message/OVSDBNettyFactoryTest.java +++ b/ovsdb/src/test/java/org/opendaylight/ovsdb/lib/message/OVSDBNettyFactoryTest.java @@ -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 _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 params = new HashMap(); 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 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 ids) { - // TODO Auto-generated method stub - } - - @Override - public void stolen(Node node, List ids) { - // TODO Auto-generated method stub - } }