2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.switchmanager.internal;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import java.util.List;
19 import org.apache.felix.service.command.Descriptor;
20 import org.opendaylight.controller.sal.core.Bandwidth;
21 import org.opendaylight.controller.sal.core.Config;
22 import org.opendaylight.controller.sal.core.Description;
23 import org.opendaylight.controller.sal.core.MacAddress;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.core.NodeConnector;
26 import org.opendaylight.controller.sal.core.Property;
27 import org.opendaylight.controller.sal.core.State;
28 import org.opendaylight.controller.sal.core.Tier;
29 import org.opendaylight.controller.sal.utils.GlobalConstants;
30 import org.opendaylight.controller.sal.utils.HexEncode;
31 import org.opendaylight.controller.sal.utils.ServiceHelper;
32 import org.opendaylight.controller.switchmanager.ISwitchManager;
33 import org.osgi.framework.ServiceRegistration;
36 * This class provides osgi cli commands for developers to debug Switch Manager
39 public class SwitchManagerCLI {
40 @SuppressWarnings("rawtypes")
41 private ServiceRegistration sr = null;
46 public void destroy() {
50 final Dictionary<String, Object> props = new Hashtable<String, Object>();
51 props.put("osgi.command.scope", "odpcontroller");
52 props.put("osgi.command.function", new String[] { "showNodes", "showNodeConnectors" });
53 this.sr = ServiceHelper.registerGlobalServiceWReg(SwitchManagerCLI.class, this, props);
57 if (this.sr != null) {
63 @Descriptor("Retrieves the nodes information present in Switch Manager DB")
64 public void showNodes(
65 @Descriptor("Container in which to query Switch Manager") String container) {
66 final ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, container, this);
69 System.out.println("Cannot find the switch manager instance on container: " + container);
73 System.out.println(" Node Type MAC Name Tier");
75 Set<Node> nodes = sm.getNodes();
76 if (nodes == null || nodes.isEmpty()) {
80 List<String> nodeArray = new ArrayList<String>();
81 for (Node node : nodes) {
82 nodeArray.add(node.toString());
84 Collections.sort(nodeArray);
85 for (String str : nodeArray) {
86 Node node = Node.fromString(str);
87 Description desc = ((Description) sm.getNodeProp(node, Description.propertyName));
88 Tier tier = ((Tier) sm.getNodeProp(node, Tier.TierPropName));
89 String nodeName = (desc == null) ? "" : desc.getValue();
90 MacAddress mac = (MacAddress) sm.getNodeProp(node, MacAddress.name);
91 String macAddr = (mac == null) ? "" : HexEncode.bytesToHexStringFormat(mac.getMacAddress());
92 int tierNum = (tier == null) ? 0 : tier.getValue();
93 System.out.println(node + " " + node.getType() + " " + macAddr + " " + nodeName + " "
96 System.out.println("Total number of Nodes: " + nodes.size());
99 @Descriptor("Retrieves the node connectors information present in Switch Manager DB for the specified node")
100 public void showNodeConnectors(@Descriptor("Container in which to query Switch Manager") String container,
101 @Descriptor("String representation of the Node, this need to be consumable from Node.fromString()") String node) {
102 final String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
103 final ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
106 System.out.println("Cannot find the switch manager instance on container: " + containerName);
110 Node target = Node.fromString(node);
111 if (target == null) {
112 System.out.println("Please enter a valid node id");
116 System.out.println(" NodeConnector BandWidth(Gbps) Admin State");
117 Set<NodeConnector> nodeConnectorSet = sm.getNodeConnectors(target);
118 if (nodeConnectorSet == null) {
121 for (NodeConnector nodeConnector : nodeConnectorSet) {
122 if (nodeConnector == null) {
125 Map<String, Property> propMap = sm.getNodeConnectorProps(nodeConnector);
126 Bandwidth bw = (Bandwidth) propMap.get(Bandwidth.BandwidthPropName);
127 Config config = (Config) propMap.get(Config.ConfigPropName);
128 State state = (State) propMap.get(State.StatePropName);
129 String out = nodeConnector + " ";
130 out += (bw != null) ? bw.getValue() / Math.pow(10, 9) : " ";
132 out += (config != null) ? config.getValue() : " ";
134 out += (state != null) ? state.getValue() : " ";
135 System.out.println(out);
137 System.out.println("Total number of NodeConnectors: " + nodeConnectorSet.size());