d75ced1e69bb35ab93476ede8e14b8b50d6a6f98
[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.mdsal.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     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
35     @Deprecated
36     @Override
37     protected Object doExecute() {
38         if (nodeId == null) {
39             System.out.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             System.out.println("No node available for this NodeID");
49         }
50         return null;
51     }
52
53     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
54     private static void printNodeHeaderOutput() {
55         Formatter formatter = new Formatter();
56         String header = formatter.format(OUTPUT_FORMAT, "NodeId", "Name", "Ports").toString();
57         formatter.close();
58         System.out.println(header);
59     }
60
61     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
62     private static void printHeaderSeparator() {
63         System.out.println(HEADER_SEPARATOR);
64     }
65
66     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
67     private static void printNodeOutput(final OFNode ofNode) {
68         String ofNodeId = ofNode.getNodeId().toString();
69         String ofNodeName = ofNode.getNodeName();
70         System.out.print(new Formatter().format(NEW_LINE, ofNodeId, ofNodeName, ofNode.getPorts()).toString());
71     }
72 }