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.HashSet;
15 import java.util.Hashtable;
16 import java.util.List;
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;
30 * This class provides osgi cli commands for developers to debug Switch Manager
33 public class SwitchManagerCLI {
34 @SuppressWarnings("rawtypes")
35 private ServiceRegistration sr = null;
40 public void destroy() {
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);
51 if (this.sr != null) {
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);
63 System.out.println("Cannot find the switch manager instance on container: " + container);
67 Set<Node> nodes = sm.getNodes();
68 if (nodes == null || nodes.isEmpty()) {
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());
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));
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()));
91 nodeProp = nodeProp.concat(String.format("%-18s ", "null"));
94 System.out.println(nodeProp);
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 Set<NodeConnector> nodeConnectorSet = sm.getNodeConnectors(target);
117 if (nodeConnectorSet == null || nodeConnectorSet.isEmpty()) {
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());
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));
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()));
140 ncProp = ncProp.concat(String.format("%-18s ", "null"));
143 System.out.println(ncProp);
145 System.out.println("Total number of NodeConnectors: " + nodeConnectorSet.size());