From a60baada98048174f312e1f3b98757c9e0841e0c Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Mon, 12 Aug 2013 16:25:48 -0700 Subject: [PATCH] Add container context debug osgi cli command to Switch Manager 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 --- .../switchmanager/implementation/pom.xml | 1 + .../switchmanager/internal/Activator.java | 10 ++ .../internal/SwitchManagerCLI.java | 139 ++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerCLI.java diff --git a/opendaylight/switchmanager/implementation/pom.xml b/opendaylight/switchmanager/implementation/pom.xml index 28bb6f274c..bcb9e0b192 100644 --- a/opendaylight/switchmanager/implementation/pom.xml +++ b/opendaylight/switchmanager/implementation/pom.xml @@ -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 diff --git a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java index 2420d609b7..b574269e45 100644 --- a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java +++ b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java @@ -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> props = new Hashtable>(); @@ -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 index 0000000000..bcf9fd6d0b --- /dev/null +++ b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerCLI.java @@ -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 props = new Hashtable(); + 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 nodes = sm.getNodes(); + if (nodes == null || nodes.isEmpty()) { + return; + } + + List nodeArray = new ArrayList(); + 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 nodeConnectorSet = sm.getNodeConnectors(target); + if (nodeConnectorSet == null) { + return; + } + for (NodeConnector nodeConnector : nodeConnectorSet) { + if (nodeConnector == null) { + continue; + } + Map 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()); + } +} -- 2.36.6