2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
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;
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);
26 private DataBroker dataBroker;
27 private NodeListener nodeListener;
29 public void setDataBroker(final DataBroker dataBroker) {
30 this.dataBroker = dataBroker;
33 public void setNodeListener(final NodeListener nodeListener) {
34 this.nodeListener = nodeListener;
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");
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);
58 private String getAllLocalNodesHeaderOutput() {
59 Formatter formatter = new Formatter();
60 String header = formatter.format("%-15s %3s %-15s %n", "NodeId", "", "NodeName").toString();