Merge "Container aware osgi commands for FRM"
authorGiovanni Meo <gmeo@cisco.com>
Wed, 13 Nov 2013 09:42:44 +0000 (09:42 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 13 Nov 2013 09:42:44 +0000 (09:42 +0000)
opendaylight/forwardingrulesmanager/implementation/pom.xml
opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/Activator.java
opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/ForwardingRulesManager.java
opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/ForwardingRulesManagerCLI.java [new file with mode: 0644]

index b236d7f2d0526c81591b852daca95a44f7bd5f40..613c2f164feae0b7452b03f37d4f66799485078d 100644 (file)
@@ -51,9 +51,9 @@
               javax.xml.bind.annotation,
               javax.xml.bind,
               org.apache.felix.dm,
+              org.apache.felix.service.command,
               org.osgi.service.component,
               org.slf4j,
-              org.eclipse.osgi.framework.console,
               org.osgi.framework
             </Import-Package>
             <Bundle-Activator>
       <artifactId>forwardingrulesmanager</artifactId>
       <version>0.5.0-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <groupId>equinoxSDK381</groupId>
+      <artifactId>org.apache.felix.gogo.runtime</artifactId>
+    </dependency>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
index 48651f7e60d7a7ed5be2a108c2c304e7ada5cb5b..ec8112035f4c89805ff987cfd2cfd31643bdef39 100644 (file)
@@ -104,4 +104,10 @@ public class Activator extends ComponentActivatorAbstractBase {
             }
         }
     }
+
+    @Override
+    protected Object[] getGlobalImplementations() {
+        final Object[] res = { ForwardingRulesManagerCLI.class };
+        return res;
+    }
 }
index 665ba7c635781968458dce7b026fc4bf6477d449..34088607b11e1cf4fae079a240568319998c42f6 100644 (file)
@@ -11,7 +11,6 @@ package org.opendaylight.controller.forwardingrulesmanager.internal;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.ObjectInputStream;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.EnumSet;
@@ -29,8 +28,6 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 
-import org.eclipse.osgi.framework.console.CommandInterpreter;
-import org.eclipse.osgi.framework.console.CommandProvider;
 import org.opendaylight.controller.clustering.services.CacheConfigException;
 import org.opendaylight.controller.clustering.services.CacheExistException;
 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
@@ -69,7 +66,6 @@ import org.opendaylight.controller.sal.match.MatchType;
 import org.opendaylight.controller.sal.utils.EtherTypes;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
 import org.opendaylight.controller.sal.utils.IObjectReader;
-import org.opendaylight.controller.sal.utils.NodeCreator;
 import org.opendaylight.controller.sal.utils.ObjectReader;
 import org.opendaylight.controller.sal.utils.ObjectWriter;
 import org.opendaylight.controller.sal.utils.Status;
@@ -78,8 +74,6 @@ import org.opendaylight.controller.switchmanager.IInventoryListener;
 import org.opendaylight.controller.switchmanager.ISwitchManager;
 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;
 import org.opendaylight.controller.switchmanager.Subnet;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -97,7 +91,6 @@ public class ForwardingRulesManager implements
         IInventoryListener,
         IObjectReader,
         ICacheUpdateAware<Object,Object>,
-        CommandProvider,
         IFlowProgrammerListener {
 
     private static final Logger log = LoggerFactory.getLogger(ForwardingRulesManager.class);
@@ -1178,11 +1171,6 @@ public class ForwardingRulesManager implements
         inactiveFlows = new ConcurrentHashMap<FlowEntry, FlowEntry>();
     }
 
-    private void registerWithOSGIConsole() {
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bundleContext.registerService(CommandProvider.class.getName(), this, null);
-    }
-
     @Override
     public void setTSPolicyData(String policyname, Object o, boolean add) {
 
@@ -2470,8 +2458,6 @@ public class ForwardingRulesManager implements
 
         cacheStartup();
 
-        registerWithOSGIConsole();
-
         /*
          * If we are not the first cluster node to come up, do not initialize
          * the static flow entries ordinal
@@ -2892,102 +2878,11 @@ public class ForwardingRulesManager implements
         }
     }
 
-    /*
-     * OSGI COMMANDS
-     */
-    @Override
-    public String getHelp() {
-        StringBuffer help = new StringBuffer();
-        return help.toString();
-    }
-
     @Override
     public Status saveConfiguration() {
         return saveConfig();
     }
 
-    public void _frmNodeFlows(CommandInterpreter ci) {
-        String nodeId = ci.nextArgument();
-        Node node = Node.fromString(nodeId);
-        if (node == null) {
-            ci.println("frmNodeFlows <node> [verbose]");
-            return;
-        }
-        boolean verbose = false;
-        String verboseCheck = ci.nextArgument();
-        if (verboseCheck != null) {
-            verbose = verboseCheck.equals("true");
-        }
-
-        if (!nodeFlows.containsKey(node)) {
-            return;
-        }
-        // Dump per node database
-        for (FlowEntryInstall entry : nodeFlows.get(node)) {
-            if (!verbose) {
-                ci.println(node + " " + installedSwView.get(entry).getFlowName());
-            } else {
-                ci.println(node + " " + installedSwView.get(entry).toString());
-            }
-        }
-    }
-
-    public void _frmGroupFlows(CommandInterpreter ci) {
-        String group = ci.nextArgument();
-        if (group == null) {
-            ci.println("frmGroupFlows <group> [verbose]");
-            return;
-        }
-        boolean verbose = false;
-        String verboseCheck = ci.nextArgument();
-        if (verboseCheck != null) {
-            verbose = verboseCheck.equalsIgnoreCase("true");
-        }
-
-        if (!groupFlows.containsKey(group)) {
-            return;
-        }
-        // Dump per node database
-        ci.println("Group " + group + ":\n");
-        for (FlowEntryInstall flowEntry : groupFlows.get(group)) {
-            if (!verbose) {
-                ci.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
-            } else {
-                ci.println(flowEntry.getNode() + " " + flowEntry.toString());
-            }
-        }
-    }
-
-    public void _frmProcessErrorEvent(CommandInterpreter ci) throws UnknownHostException {
-        Node node = null;
-        long reqId = 0L;
-        String nodeId = ci.nextArgument();
-        if (nodeId == null) {
-            ci.print("Node id not specified");
-            return;
-        }
-        String requestId = ci.nextArgument();
-        if (requestId == null) {
-            ci.print("Request id not specified");
-            return;
-        }
-        try {
-            node = NodeCreator.createOFNode(Long.valueOf(nodeId));
-        } catch (NumberFormatException e) {
-            ci.print("Node id not a number");
-            return;
-        }
-        try {
-            reqId = Long.parseLong(requestId);
-        } catch (NumberFormatException e) {
-            ci.print("Request id not a number");
-            return;
-        }
-        // null for error object is good enough for now
-        ErrorReportedEvent event = new ErrorReportedEvent(reqId, node, null);
-        this.processErrorEvent(event);
-    }
-
     @Override
     public void flowRemoved(Node node, Flow flow) {
         log.trace("Received flow removed notification on {} for {}", node, flow);
diff --git a/opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/ForwardingRulesManagerCLI.java b/opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/ForwardingRulesManagerCLI.java
new file mode 100644 (file)
index 0000000..3a99ed7
--- /dev/null
@@ -0,0 +1,136 @@
+package org.opendaylight.controller.forwardingrulesmanager.internal;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.felix.service.command.Descriptor;
+import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
+import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * This class provides osgi cli commands for developers to debug
+ * ForwardingRulesManager functionality
+ */
+public class ForwardingRulesManagerCLI {
+    @SuppressWarnings("rawtypes")
+    private ServiceRegistration sr = null;
+
+    public void init() {
+    }
+
+    public void destroy() {
+    }
+
+    public void start() {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put("osgi.command.scope", "odpcontroller");
+        props.put("osgi.command.function", new String[] { "showRequestedGroupFlows", "showInstalledGroupFlows",
+                "showRequestedNodeFlows", "showInstalledNodeFlows" });
+        this.sr = ServiceHelper.registerGlobalServiceWReg(ForwardingRulesManagerCLI.class, this, props);
+    }
+
+    public void stop() {
+        if (this.sr != null) {
+            this.sr.unregister();
+            this.sr = null;
+        }
+    }
+
+    @Descriptor("Displays all the flow entries in a given group")
+    public void showRequestedGroupFlows(@Descriptor("Container in which to query FRM") String container,
+            @Descriptor("Group name") String group, @Descriptor("True for verbose else false") Boolean verbose) {
+        IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
+                IForwardingRulesManager.class, container, this);
+        if (frm == null) {
+            System.out.println("Cannot find the FRM instance on container: " + container);
+            return;
+        }
+        List<FlowEntry> groupFlows = frm.getFlowEntriesForGroup(group);
+        System.out.println("Group " + group);
+        for (FlowEntry flowEntry : groupFlows) {
+            if (!verbose) {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
+            } else {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.toString());
+            }
+        }
+    }
+
+    @Descriptor("Displays all the installed flow entries in a given group")
+    public void showInstalledGroupFlows(@Descriptor("Container in which to query FRM") String container,
+            @Descriptor("Group name") String group, @Descriptor("True for verbose else false") Boolean verbose) {
+        IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
+                IForwardingRulesManager.class, container, this);
+        if (frm == null) {
+            System.out.println("Cannot find the FRM instance on container: " + container);
+            return;
+        }
+        List<FlowEntry> groupFlows = frm.getInstalledFlowEntriesForGroup(group);
+        System.out.println("Group " + group);
+        for (FlowEntry flowEntry : groupFlows) {
+            if (!verbose) {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
+            } else {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.toString());
+            }
+        }
+    }
+
+    @Descriptor("Displays all the flow entries for a network node")
+    public void showRequestedNodeFlows(
+            @Descriptor("Container in which to query FRM") String container,
+            @Descriptor("String representation of the Node, this need to be consumable from Node.fromString()") String nodeId,
+            @Descriptor("True for verbose else false") Boolean verbose) {
+        IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
+                IForwardingRulesManager.class, container, this);
+        if (frm == null) {
+            System.out.println("Cannot find the FRM instance on container: " + container);
+            return;
+        }
+        Node node = Node.fromString(nodeId);
+        if (node == null) {
+            System.out.println("Please enter a valid node id");
+            return;
+        }
+        List<FlowEntry> groupFlows = frm.getFlowEntriesForNode(node);
+        System.out.println("Node " + nodeId);
+        for (FlowEntry flowEntry : groupFlows) {
+            if (!verbose) {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
+            } else {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.toString());
+            }
+        }
+    }
+
+    @Descriptor("Displays all the flow entries installed in a network node")
+    public void showInstalledNodeFlows(
+            @Descriptor("Container in which to query FRM") String container,
+            @Descriptor("String representation of the Node, this need to be consumable from Node.fromString()") String nodeId,
+            @Descriptor("True for verbose else false") Boolean verbose) {
+        IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
+                IForwardingRulesManager.class, container, this);
+        if (frm == null) {
+            System.out.println("Cannot find the FRM instance on container: " + container);
+            return;
+        }
+        Node node = Node.fromString(nodeId);
+        if (node == null) {
+            System.out.println("Please enter a valid node id");
+            return;
+        }
+        List<FlowEntry> groupFlows = frm.getInstalledFlowEntriesForNode(node);
+        System.out.println("Node " + nodeId);
+        for (FlowEntry flowEntry : groupFlows) {
+            if (!verbose) {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.getFlowName());
+            } else {
+                System.out.println(flowEntry.getNode() + " " + flowEntry.toString());
+            }
+        }
+    }
+}