Merge "Eliminate XmlUtil.createElement(Document, String)"
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfListDevicesCommand.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.netconf.console.commands;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.Map;
13 import javax.annotation.Nonnull;
14 import org.apache.karaf.shell.api.action.Action;
15 import org.apache.karaf.shell.api.action.Command;
16 import org.apache.karaf.shell.api.action.lifecycle.Reference;
17 import org.apache.karaf.shell.api.action.lifecycle.Service;
18 import org.apache.karaf.shell.support.table.ShellTable;
19 import org.opendaylight.netconf.console.api.NetconfCommands;
20 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
21
22 @Service
23 @Command(name = "list-devices", scope = "netconf", description = "List all netconf devices in the topology.")
24 public class NetconfListDevicesCommand implements Action {
25
26     @Reference
27     private NetconfCommands service;
28
29     public NetconfListDevicesCommand() {
30
31     }
32
33     @VisibleForTesting
34     NetconfListDevicesCommand(final NetconfCommands service) {
35         this.service = service;
36     }
37
38     @Override
39     public Object execute() {
40         final Map<String, Map<String, String>> allDevices = service.listDevices();
41         printDevicesList(allDevices);
42         return null;
43     }
44
45     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
46     private static void printDevicesList(@Nonnull final Map<String, Map<String, String>> allDevices) {
47         final ShellTable table = new ShellTable();
48         table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft();
49         table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft();
50         table.column(NetconfConsoleConstants.NETCONF_PORT).alignLeft();
51         table.column(NetconfConsoleConstants.STATUS).alignLeft();
52
53         for (final Map<String, String> attributes : allDevices.values()) {
54             table.addRow().addContent(attributes.get(NetconfConsoleConstants.NETCONF_ID),
55                     attributes.get(NetconfConsoleConstants.NETCONF_IP),
56                     attributes.get(NetconfConsoleConstants.NETCONF_PORT),
57                     attributes.get(NetconfConsoleConstants.STATUS));
58         }
59         table.print(System.out);
60     }
61 }