Add container context debug osgi cli command to Switch Manager 59/859/2
authorAlessandro Boch <aboch@cisco.com>
Mon, 12 Aug 2013 23:25:48 +0000 (16:25 -0700)
committerAlessandro Boch <aboch@cisco.com>
Mon, 12 Aug 2013 23:36:15 +0000 (16:36 -0700)
CHANGES:
Available osgi comamnds implemented in the service classes
do not allow to access the desired container instance of service.
This change, following example in Gerrit #831, allow to dump the
nodes and node connectors database contained in Switch Manager
for the desired container.

Change-Id: I4cf959763e06facd135a1a5c3ebca59ee85bcf22
Signed-off-by: Alessandro Boch <aboch@cisco.com>
opendaylight/switchmanager/implementation/pom.xml
opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java
opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerCLI.java [new file with mode: 0644]

index 28bb6f274c791e94ca4c43f5202d43159f13d170..bcb9e0b19254402ba732b7f6fb74b0a20525b24a 100644 (file)
@@ -53,6 +53,7 @@
               org.apache.felix.dm,
               org.eclipse.osgi.framework.console,
               org.osgi.framework,
+              org.apache.felix.service.command,
               javax.xml.bind.annotation,
               org.apache.commons.lang3.builder
             </Import-Package>
index 2420d609b7c2363b415de4e77aeb659a8806097d..b574269e450e46b2ee39b532208aadc00c024a4d 100644 (file)
@@ -43,6 +43,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      * ComponentActivatorAbstractBase.
      *
      */
+    @Override
     public void init() {
 
     }
@@ -52,6 +53,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      * cleanup done by ComponentActivatorAbstractBase
      *
      */
+    @Override
     public void destroy() {
 
     }
@@ -65,6 +67,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      * instantiated in order to get an fully working implementation
      * Object
      */
+    @Override
     public Object[] getImplementations() {
         Object[] res = { SwitchManager.class };
         return res;
@@ -83,6 +86,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      * also optional per-container different behavior if needed, usually
      * should not be the case though.
      */
+    @Override
     public void configureInstance(Component c, Object imp, String containerName) {
         if (imp.equals(SwitchManager.class)) {
             Dictionary<String, Set<String>> props = new Hashtable<String, Set<String>>();
@@ -119,4 +123,10 @@ public class Activator extends ComponentActivatorAbstractBase {
                     "unsetClusterContainerService").setRequired(true));
         }
     }
+
+    @Override
+    protected Object[] getGlobalImplementations() {
+        final Object[] res = { SwitchManagerCLI.class };
+        return res;
+    }
 }
diff --git a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerCLI.java b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerCLI.java
new file mode 100644 (file)
index 0000000..bcf9fd6
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.switchmanager.internal;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.felix.service.command.Descriptor;
+import org.opendaylight.controller.sal.core.Bandwidth;
+import org.opendaylight.controller.sal.core.Config;
+import org.opendaylight.controller.sal.core.Description;
+import org.opendaylight.controller.sal.core.MacAddress;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.core.Property;
+import org.opendaylight.controller.sal.core.State;
+import org.opendaylight.controller.sal.core.Tier;
+import org.opendaylight.controller.sal.utils.GlobalConstants;
+import org.opendaylight.controller.sal.utils.HexEncode;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+import org.opendaylight.controller.switchmanager.ISwitchManager;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * This class provides osgi cli commands for developers to debug Switch Manager
+ * functionality
+ */
+public class SwitchManagerCLI {
+    @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[] { "showNodes", "showNodeConnectors" });
+        this.sr = ServiceHelper.registerGlobalServiceWReg(SwitchManagerCLI.class, this, props);
+    }
+
+    public void stop() {
+        if (this.sr != null) {
+            this.sr.unregister();
+            this.sr = null;
+        }
+    }
+
+    @Descriptor("Retrieves the nodes information present in Switch Manager DB")
+    public void showNodes(
+            @Descriptor("Container in which to query Switch Manager") String container) {
+        final ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, container, this);
+
+        if (sm == null) {
+            System.out.println("Cannot find the switch manager instance on container: " + container);
+            return;
+        }
+
+        System.out.println("           Node               Type           MAC            Name      Tier");
+
+        Set<Node> nodes = sm.getNodes();
+        if (nodes == null || nodes.isEmpty()) {
+            return;
+        }
+
+        List<String> nodeArray = new ArrayList<String>();
+        for (Node node : nodes) {
+            nodeArray.add(node.toString());
+        }
+        Collections.sort(nodeArray);
+        for (String str : nodeArray) {
+            Node node = Node.fromString(str);
+            Description desc = ((Description) sm.getNodeProp(node, Description.propertyName));
+            Tier tier = ((Tier) sm.getNodeProp(node, Tier.TierPropName));
+            String nodeName = (desc == null) ? "" : desc.getValue();
+            MacAddress mac = (MacAddress) sm.getNodeProp(node, MacAddress.name);
+            String macAddr = (mac == null) ? "" : HexEncode.bytesToHexStringFormat(mac.getMacAddress());
+            int tierNum = (tier == null) ? 0 : tier.getValue();
+            System.out.println(node + "     " + node.getType() + "     " + macAddr + "     " + nodeName + "     "
+                    + tierNum);
+        }
+        System.out.println("Total number of Nodes: " + nodes.size());
+    }
+
+    @Descriptor("Retrieves the node connectors information present in Switch Manager DB for the specified node")
+    public void showNodeConnectors(@Descriptor("Container in which to query Switch Manager") String container,
+            @Descriptor("String representation of the Node, this need to be consumable from Node.fromString()") String node) {
+        final String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
+        final ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
+
+        if (sm == null) {
+            System.out.println("Cannot find the switch manager instance on container: " + containerName);
+            return;
+        }
+
+        Node target = Node.fromString(node);
+        if (target == null) {
+            System.out.println("Please enter a valid node id");
+            return;
+        }
+
+        System.out.println("          NodeConnector               BandWidth(Gbps)     Admin     State");
+        Set<NodeConnector> nodeConnectorSet = sm.getNodeConnectors(target);
+        if (nodeConnectorSet == null) {
+            return;
+        }
+        for (NodeConnector nodeConnector : nodeConnectorSet) {
+            if (nodeConnector == null) {
+                continue;
+            }
+            Map<String, Property> propMap = sm.getNodeConnectorProps(nodeConnector);
+            Bandwidth bw = (Bandwidth) propMap.get(Bandwidth.BandwidthPropName);
+            Config config = (Config) propMap.get(Config.ConfigPropName);
+            State state = (State) propMap.get(State.StatePropName);
+            String out = nodeConnector + "           ";
+            out += (bw != null) ? bw.getValue() / Math.pow(10, 9) : "    ";
+            out += "             ";
+            out += (config != null) ? config.getValue() : " ";
+            out += "          ";
+            out += (state != null) ? state.getValue() : " ";
+            System.out.println(out);
+        }
+        System.out.println("Total number of NodeConnectors: " + nodeConnectorSet.size());
+    }
+}