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