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