Cli to display all the connected DPNs
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / ShowNodeCommandProvider.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
9
10 import java.util.Formatter;
11 import java.util.HashSet;
12 import java.util.Set;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.commands.Option;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
18 import org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil;
19
20 @Command(scope = "openflow", name = "shownode", description = "shownode -d <NodeID>")
21 public class ShowNodeCommandProvider extends OsgiCommandSupport {
22     public static final String OUTPUT_FORMAT = "%-24s %-20s %-15s";
23     public static final String NEW_LINE = "%-24s %-20s %-15s %n";
24     public static final String HEADER_SEPARATOR = "---------------------------------------------"
25             + "---------------------------------------";
26
27     @Option(name = "-d", description = "Node Id", required = true, multiValued = false)
28     String nodeId;
29
30     private DataBroker dataBroker;
31
32     public void setDataBroker(final DataBroker dataBroker) {
33         this.dataBroker = dataBroker;
34     }
35
36     @Override
37     protected Object doExecute() throws Exception {
38         if (nodeId == null) {
39             session.getConsole().println("NodeID not specified");
40             return null;
41         }
42         OFNode node = ShellUtil.getNode(Long.parseLong(nodeId), dataBroker);
43         if (node != null) {
44             printNodeHeaderOutput();
45             printHeaderSeparator();
46             printNodeOutput(node);
47         } else {
48             session.getConsole().println("No node available for this NodeID");
49         }
50         return null;
51     }
52
53     private void printNodeHeaderOutput() {
54         Formatter formatter = new Formatter();
55         String header = formatter.format(OUTPUT_FORMAT, "OFNode", "Name", "Ports").toString();
56         formatter.close();
57         session.getConsole().println(header);
58     }
59
60     private void printHeaderSeparator() {
61         session.getConsole().println(HEADER_SEPARATOR);
62     }
63
64     private void printNodeOutput(final OFNode ofNode) {
65         Set<String> portNames = new HashSet<>();
66         String ofNodeId = ofNode.getNodeId().toString();
67         String ofNodeName = ofNode.getNodeName();
68         for (String port : ofNode.getPorts()) {
69             if (port != null) {
70                 session.getConsole().println(new Formatter().format(NEW_LINE, ofNodeId, ofNodeName, port).toString());
71                 ofNodeId = "";
72                 ofNodeName = "";
73             }
74         }
75     }
76 }