bcf9fd6d0b33e47da103e5646a86be8566b78176
[controller.git] / opendaylight / switchmanager / implementation / src / main / java / org / opendaylight / controller / switchmanager / internal / SwitchManagerCLI.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.switchmanager.internal;
10
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;
16 import java.util.Map;
17 import java.util.Set;
18
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;
34
35 /**
36  * This class provides osgi cli commands for developers to debug Switch Manager
37  * functionality
38  */
39 public class SwitchManagerCLI {
40     @SuppressWarnings("rawtypes")
41     private ServiceRegistration sr = null;
42
43     public void init() {
44     }
45
46     public void destroy() {
47     }
48
49     public void start() {
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);
54     }
55
56     public void stop() {
57         if (this.sr != null) {
58             this.sr.unregister();
59             this.sr = null;
60         }
61     }
62
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);
67
68         if (sm == null) {
69             System.out.println("Cannot find the switch manager instance on container: " + container);
70             return;
71         }
72
73         System.out.println("           Node               Type           MAC            Name      Tier");
74
75         Set<Node> nodes = sm.getNodes();
76         if (nodes == null || nodes.isEmpty()) {
77             return;
78         }
79
80         List<String> nodeArray = new ArrayList<String>();
81         for (Node node : nodes) {
82             nodeArray.add(node.toString());
83         }
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 + "     "
94                     + tierNum);
95         }
96         System.out.println("Total number of Nodes: " + nodes.size());
97     }
98
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);
104
105         if (sm == null) {
106             System.out.println("Cannot find the switch manager instance on container: " + containerName);
107             return;
108         }
109
110         Node target = Node.fromString(node);
111         if (target == null) {
112             System.out.println("Please enter a valid node id");
113             return;
114         }
115
116         System.out.println("          NodeConnector               BandWidth(Gbps)     Admin     State");
117         Set<NodeConnector> nodeConnectorSet = sm.getNodeConnectors(target);
118         if (nodeConnectorSet == null) {
119             return;
120         }
121         for (NodeConnector nodeConnector : nodeConnectorSet) {
122             if (nodeConnector == null) {
123                 continue;
124             }
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) : "    ";
131             out += "             ";
132             out += (config != null) ? config.getValue() : " ";
133             out += "          ";
134             out += (state != null) ? state.getValue() : " ";
135             System.out.println(out);
136         }
137         System.out.println("Total number of NodeConnectors: " + nodeConnectorSet.size());
138     }
139 }