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
8 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Formatter;
13 import org.apache.felix.gogo.commands.Command;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.openflowplugin.applications.southboundcli.DpnTracker;
16 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
18 @Command(scope = "openflow", name = "getallnodes", description = "Print all nodes from the operational datastore")
19 public class GetAllNodesCommandProvider extends OsgiCommandSupport {
20 private final DpnTracker dpnTracker;
22 public GetAllNodesCommandProvider(final DpnTracker dpnTracker) {
23 this.dpnTracker = requireNonNull(dpnTracker);
26 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
28 protected Object doExecute() throws Exception {
29 final var ofNodeList = dpnTracker.currentNodes();
30 if (ofNodeList.isEmpty()) {
31 System.out.println("No node is connected yet");
35 final var stringBuilder = new StringBuilder();
36 try (var formatter = new Formatter(stringBuilder)) {
37 System.out.println("Number of nodes: " + ofNodeList.size());
38 System.out.println(getAllLocalNodesHeaderOutput());
39 System.out.println("--------------------------------------------------------------------------");
40 for (OFNode ofNode : ofNodeList) {
41 System.out.println(formatter.format("%-15s %3s %-15s %n",
42 ofNode.getNodeId(), "", ofNode.getNodeName()).toString());
43 stringBuilder.setLength(0);
49 private static String getAllLocalNodesHeaderOutput() {
50 try (var formatter = new Formatter()) {
51 return formatter.format("%-15s %3s %-15s", "NodeId", "", "NodeName").toString();