Eliminate blueprint for southbound-cli commands
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / ShowNodeCommand.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 static org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil.LINE_SEPARATOR;
11
12 import java.util.Formatter;
13 import org.apache.karaf.shell.api.action.Action;
14 import org.apache.karaf.shell.api.action.Command;
15 import org.apache.karaf.shell.api.action.Option;
16 import org.apache.karaf.shell.api.action.lifecycle.Reference;
17 import org.apache.karaf.shell.api.action.lifecycle.Service;
18 import org.apache.karaf.shell.api.console.Session;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
21 import org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil;
22
23 @Service
24 @Command(scope = "openflow", name = "shownode", description = "shownode -d <NodeID>")
25 @Deprecated
26 public class ShowNodeCommand implements Action {
27     public static final String OUTPUT_FORMAT = "%-24s %-20s %-15s";
28     public static final String NEW_LINE = "%-24s %-20s %-15s %n";
29
30     @Option(name = "-d", description = "Node Id", required = true)
31     String nodeId;
32     @Reference
33     Session session;
34     @Reference
35     DataBroker dataBroker;
36
37     @Override
38     public Object execute() {
39         if (nodeId == null) {
40             session.getConsole().println("NodeID not specified");
41             return null;
42         }
43         OFNode node = ShellUtil.getNode(Long.parseLong(nodeId), dataBroker);
44         if (node != null) {
45             printNodeHeaderOutput();
46             session.getConsole().println(LINE_SEPARATOR);
47             printNodeOutput(node);
48         } else {
49             session.getConsole().println("No node available for this NodeID");
50         }
51         return null;
52     }
53
54     private void printNodeHeaderOutput() {
55         Formatter formatter = new Formatter();
56         String header = formatter.format(OUTPUT_FORMAT, "NodeId", "Name", "Ports").toString();
57         formatter.close();
58         session.getConsole().println(header);
59     }
60
61     private void printNodeOutput(final OFNode ofNode) {
62         String ofNodeId = ofNode.getNodeId().toString();
63         String ofNodeName = ofNode.getNodeName();
64         session.getConsole().print(new Formatter().format(NEW_LINE, ofNodeId, ofNodeName, ofNode.getPorts()));
65     }
66 }