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