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