Bump versions by x.(y+1).z
[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 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
9
10 import static java.util.Objects.requireNonNull;
11
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;
17
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;
21
22     public GetAllNodesCommandProvider(final DpnTracker dpnTracker) {
23         this.dpnTracker = requireNonNull(dpnTracker);
24     }
25
26     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
27     @Override
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");
32             return null;
33         }
34
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);
44             }
45         }
46         return null;
47     }
48
49     private static String getAllLocalNodesHeaderOutput() {
50         try (var formatter = new Formatter()) {
51             return formatter.format("%-15s %3s %-15s", "NodeId", "", "NodeName").toString();
52         }
53     }
54 }