268e45ad85891f0c1d274447dfca0e66520bb1c7
[controller.git] / opendaylight / adsal / 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.HashSet;
15 import java.util.Hashtable;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.apache.felix.service.command.Descriptor;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.core.Property;
24 import org.opendaylight.controller.sal.utils.GlobalConstants;
25 import org.opendaylight.controller.sal.utils.ServiceHelper;
26 import org.opendaylight.controller.switchmanager.ISwitchManager;
27 import org.osgi.framework.ServiceRegistration;
28
29 /**
30  * This class provides osgi cli commands for developers to debug Switch Manager
31  * functionality
32  */
33 public class SwitchManagerCLI {
34     @SuppressWarnings("rawtypes")
35     private ServiceRegistration sr = null;
36
37     public void init() {
38     }
39
40     public void destroy() {
41     }
42
43     public void start() {
44         final Dictionary<String, Object> props = new Hashtable<String, Object>();
45         props.put("osgi.command.scope", "odpcontroller");
46         props.put("osgi.command.function", new String[] { "showNodes", "showNodeConnectors" });
47         this.sr = ServiceHelper.registerGlobalServiceWReg(SwitchManagerCLI.class, this, props);
48     }
49
50     public void stop() {
51         if (this.sr != null) {
52             this.sr.unregister();
53             this.sr = null;
54         }
55     }
56
57     @Descriptor("Retrieves the nodes information present in Switch Manager DB")
58     public void showNodes(
59             @Descriptor("Container in which to query Switch Manager") String container) {
60         final ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, container, this);
61
62         if (sm == null) {
63             System.out.println("Cannot find the switch manager instance on container: " + container);
64             return;
65         }
66
67         Set<Node> nodes = sm.getNodes();
68         if (nodes == null || nodes.isEmpty()) {
69             return;
70         }
71
72         Set<String> propertyList = new HashSet<String>();
73         for (Node node : nodes) {
74             Map<String, Property> propList = sm.getNodeProps(node);
75             propertyList.addAll(propList.keySet());
76         }
77         List<String> sortedProps = new ArrayList<String>(propertyList);
78         Collections.sort(sortedProps);
79         String properties = String.format("%-26s  ", "Node");
80         for (String s : sortedProps) {
81             properties = properties.concat(String.format("%-18s ", s));
82         }
83         System.out.println(properties);
84         for (Node node : nodes) {
85             String nodeProp = String.format("%-26s  ", node);
86             Map<String, Property> propList = sm.getNodeProps(node);
87             for (String s : sortedProps) {
88                 if (propList.containsKey(s)) {
89                     nodeProp = nodeProp.concat(String.format("%-18s ", propList.get(s).getStringValue()));
90                 } else {
91                     nodeProp = nodeProp.concat(String.format("%-18s ", "null"));
92                 }
93             }
94             System.out.println(nodeProp);
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         Set<NodeConnector> nodeConnectorSet = sm.getNodeConnectors(target);
117         if (nodeConnectorSet == null || nodeConnectorSet.isEmpty()) {
118             return;
119         }
120
121         Set<String> propertyList = new HashSet<String>();
122         for (NodeConnector nodeConnector : nodeConnectorSet) {
123             Map<String, Property> propList = sm.getNodeConnectorProps(nodeConnector);
124             propertyList.addAll(propList.keySet());
125         }
126         List<String> sortedProps = new ArrayList<String>(propertyList);
127         Collections.sort(sortedProps);
128         String properties = String.format("%-33s  ", "NodeConnector");
129         for (String s : sortedProps) {
130             properties = properties.concat(String.format("%-18s ", s));
131         }
132         System.out.println(properties);
133         for (NodeConnector nodeConnector : nodeConnectorSet) {
134             String ncProp = String.format("%-33s  ", nodeConnector);
135             Map<String, Property> ncProperties = sm.getNodeConnectorProps(nodeConnector);
136             for (String s : sortedProps) {
137                 if (ncProperties.containsKey(s)) {
138                     ncProp = ncProp.concat(String.format("%-18s ", ncProperties.get(s).getStringValue()));
139                 } else {
140                     ncProp = ncProp.concat(String.format("%-18s ", "null"));
141                 }
142             }
143             System.out.println(ncProp);
144         }
145         System.out.println("Total number of NodeConnectors: " + nodeConnectorSet.size());
146     }
147 }