Eliminate XmlUtil.createElement(Document, String)
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfShowDeviceCommand.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 com.google.common.base.Strings;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import javax.annotation.Nonnull;
17 import org.apache.karaf.shell.api.action.Action;
18 import org.apache.karaf.shell.api.action.Command;
19 import org.apache.karaf.shell.api.action.Option;
20 import org.apache.karaf.shell.support.table.ShellTable;
21 import org.opendaylight.netconf.console.api.NetconfCommands;
22 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
23
24 @Command(name = "netconf:show-device", scope = "netconf", description = "Shows netconf device attributes.")
25 public class NetconfShowDeviceCommand implements Action {
26
27     protected final NetconfCommands service;
28
29     public NetconfShowDeviceCommand(final NetconfCommands service) {
30         this.service = service;
31     }
32
33     @VisibleForTesting
34     NetconfShowDeviceCommand(final NetconfCommands service, final String deviceId, final String deviceIp,
35                              final String devicePort) {
36         this.service = service;
37         this.deviceId = deviceId;
38         this.deviceIp = deviceIp;
39         this.devicePort = devicePort;
40     }
41
42     @Option(name = "-id",
43             aliases = { "--identifier" },
44             description = "Node Identifier of the netconf device",
45             required = false,
46             multiValued = false)
47     private String deviceId;
48
49     @Option(name = "-i",
50             aliases = { "--ipaddress" },
51             description = "IP address of the netconf device",
52             required = false,
53             multiValued = false)
54     private String deviceIp;
55
56     @Option(name = "-p",
57             aliases = { "--port" },
58             description = "Port of the netconf device",
59             required = false,
60             multiValued = false)
61     private String devicePort;
62
63     @Override
64     public Object execute() {
65
66         if ((Strings.isNullOrEmpty(deviceIp) || Strings.isNullOrEmpty(devicePort)) && Strings.isNullOrEmpty(deviceId)) {
67             return "You must provide either the device Ip and the device Port or the device Id";
68         }
69
70         Map<String, Map<String, List<String>>> devices = null;
71
72         if (!Strings.isNullOrEmpty(deviceId)) {
73             devices = service.showDevice(deviceId);
74             printDeviceData(devices);
75             return null;
76         }
77
78         if (!NetconfCommandUtils.isIpValid(deviceIp)
79                 || devicePort != null && !NetconfCommandUtils.isPortValid(devicePort)) {
80             return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
81         }
82
83         devices = service.showDevice(deviceIp, devicePort);
84         printDeviceData(devices);
85         return null;
86     }
87
88     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
89     private static void printDeviceData(@Nonnull final Map<String, Map<String, List<String>>> devices) {
90         final ShellTable table = new ShellTable();
91         table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft();
92         table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft();
93         table.column(NetconfConsoleConstants.NETCONF_PORT).alignLeft();
94         table.column(NetconfConsoleConstants.STATUS).alignLeft();
95         table.column(NetconfConsoleConstants.AVAILABLE_CAPABILITIES).alignLeft();
96
97         for (final Entry<String, Map<String, List<String>>> entry : devices.entrySet()) {
98             final String nodeId = entry.getKey();
99             final Map<String, List<String>> device = entry.getValue();
100             table.addRow().addContent(nodeId,
101                     device.get(NetconfConsoleConstants.NETCONF_IP).get(NetconfConsoleConstants.DEFAULT_INDEX),
102                     device.get(NetconfConsoleConstants.NETCONF_PORT).get(NetconfConsoleConstants.DEFAULT_INDEX),
103                     device.get(NetconfConsoleConstants.STATUS).get(NetconfConsoleConstants.DEFAULT_INDEX),
104                     device.get(NetconfConsoleConstants.AVAILABLE_CAPABILITIES)
105                             .get(NetconfConsoleConstants.DEFAULT_INDEX));
106             formatCapabilities(device, table, NetconfConsoleConstants.AVAILABLE_CAPABILITIES);
107         }
108         table.print(System.out);
109     }
110
111     private static void formatCapabilities(final Map<String, List<String>> device, final ShellTable table,
112             final String capabilityName) {
113         for (final String availableCapability : device.get(capabilityName)) {
114             // First row is already added to table with the first available capability
115             // Process rows other than the first to only have remaining available capabilities
116             if (!Strings.isNullOrEmpty(availableCapability)
117                     && !isFirstAvailableCapability(device, capabilityName, availableCapability)) {
118                 table.addRow().addContent("", "", "", "", availableCapability);
119             }
120         }
121     }
122
123     private static boolean isFirstAvailableCapability(final Map<String, List<String>> device,
124             final String capabilityName, final String availableCapability) {
125         return device.get(capabilityName).indexOf(availableCapability) == NetconfConsoleConstants.DEFAULT_INDEX;
126     }
127 }