Eliminate blueprint for southbound-cli commands
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / GetAllNodesCommand.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.lifecycle.Reference;
16 import org.apache.karaf.shell.api.action.lifecycle.Service;
17 import org.apache.karaf.shell.api.console.Session;
18 import org.opendaylight.openflowplugin.applications.southboundcli.DpnTracker;
19 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
20
21 @Service
22 @Command(scope = "openflow", name = "getallnodes", description = "Print all nodes from the operational datastore")
23 public final class GetAllNodesCommand implements Action {
24     @Reference
25     Session session;
26     @Reference
27     DpnTracker dpnTracker;
28
29     @Override
30     public Object execute() throws Exception {
31         if (dpnTracker == null) {
32             // not initialized
33             return null;
34         }
35
36         final var ofNodeList = dpnTracker.currentNodes();
37         if (ofNodeList.isEmpty()) {
38             session.getConsole().println("No node is connected yet");
39             return null;
40         }
41
42         final var stringBuilder = new StringBuilder();
43         try (var formatter = new Formatter(stringBuilder)) {
44             session.getConsole().println("Number of nodes: " + ofNodeList.size());
45             session.getConsole().println(getAllLocalNodesHeaderOutput());
46             session.getConsole().println(LINE_SEPARATOR);
47             for (OFNode ofNode : ofNodeList) {
48                 session.getConsole().println(formatter.format("%-15s %3s %-15s %n",
49                     ofNode.getNodeId(), "", ofNode.getNodeName()).toString());
50                 stringBuilder.setLength(0);
51             }
52         }
53         return null;
54     }
55
56     private static String getAllLocalNodesHeaderOutput() {
57         try (var formatter = new Formatter()) {
58             return formatter.format("%-15s %3s %-15s", "NodeId", "", "NodeName").toString();
59         }
60     }
61 }