6ce3071bbe56c9397d42ce1f5f50a02f3cbd6cec
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / GetAllNodesCommandProvider.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
9 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
10 import java.util.Formatter;
11 import java.util.List;
12 import org.apache.felix.gogo.commands.Command;
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 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 @Command(scope = "openflow", name = "getallnodes", description = "Print all nodes from the operational datastore")
21 public class GetAllNodesCommandProvider extends OsgiCommandSupport {
22     private static final Logger LOG = LoggerFactory.getLogger(GetAllNodesCommandProvider.class);
23
24     private DataBroker dataBroker;
25
26     public void setDataBroker(final DataBroker dataBroker) {
27         this.dataBroker = dataBroker;
28     }
29
30     @Override
31     protected Object doExecute() throws Exception {
32         List<OFNode> ofNodeList = ShellUtil.getAllNodes(dataBroker);
33         if (ofNodeList.isEmpty()) {
34             session.getConsole().println("No node is connected yet");
35         } else {
36             StringBuilder stringBuilder = new StringBuilder();
37             Formatter formatter = new Formatter(stringBuilder);
38             session.getConsole().println("Number of nodes: " + ofNodeList.size());
39             session.getConsole().println(getAllLocalNodesHeaderOutput());
40             session.getConsole().println("--------------------------------------------------------------------------");
41             for (OFNode ofNode : ofNodeList) {
42                 session.getConsole().println(formatter.format("%-15s %3s %-15s %n",
43                         ofNode.getNodeId(), "", ofNode.getNodeName()).toString());
44                 stringBuilder.setLength(0);
45             }
46             formatter.close();
47         }
48         return null;
49     }
50
51     private String getAllLocalNodesHeaderOutput() {
52         Formatter formatter = new Formatter();
53         String header = formatter.format("%-15s %3s %-15s %n", "NodeId", "", "NodeName").toString();
54         formatter.close();
55         return header;
56     }
57 }