874dbc3c1b0db4ffcf8c6e59c295408cc3d769da
[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 java.util.Map;
12 import javax.annotation.Nonnull;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.console.AbstractAction;
15 import org.apache.karaf.shell.table.ShellTable;
16 import org.opendaylight.netconf.console.api.NetconfCommands;
17 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
18
19 @Command(name = "netconf:list-devices", scope = "netconf", description = "List all netconf devices in the topology.")
20 public class NetconfListDevicesCommand extends AbstractAction {
21
22     protected final NetconfCommands service;
23
24     public NetconfListDevicesCommand(final NetconfCommands service) {
25         this.service = service;
26     }
27
28     @Override
29     protected Object doExecute() throws Exception {
30         final Map<String, Map<String, String>> allDevices = service.listDevices();
31         printDevicesList(allDevices);
32         return null;
33     }
34
35     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
36     private void printDevicesList(@Nonnull final Map<String, Map<String, String>> allDevices) {
37         final ShellTable table = new ShellTable();
38         table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft();
39         table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft();
40         table.column(NetconfConsoleConstants.NETCONF_PORT).alignLeft();
41         table.column(NetconfConsoleConstants.STATUS).alignLeft();
42
43         for (final String nodeIds : allDevices.keySet()) {
44             final Map<String, String> attributes = allDevices.get(nodeIds);
45             table.addRow().addContent(attributes.get(NetconfConsoleConstants.NETCONF_ID),
46                     attributes.get(NetconfConsoleConstants.NETCONF_IP),
47                     attributes.get(NetconfConsoleConstants.NETCONF_PORT),
48                     attributes.get(NetconfConsoleConstants.STATUS));
49         }
50         table.print(System.out);
51     }
52 }