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