2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.netconf.console.commands;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Strings;
13 import java.util.List;
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;
24 @Command(name = "netconf:show-device", scope = "netconf", description = "Shows netconf device attributes.")
25 public class NetconfShowDeviceCommand implements Action {
27 protected final NetconfCommands service;
29 public NetconfShowDeviceCommand(final NetconfCommands service) {
30 this.service = service;
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;
43 aliases = { "--identifier" },
44 description = "Node Identifier of the netconf device",
47 private String deviceId;
50 aliases = { "--ipaddress" },
51 description = "IP address of the netconf device",
54 private String deviceIp;
57 aliases = { "--port" },
58 description = "Port of the netconf device",
61 private String devicePort;
64 public Object execute() {
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";
70 Map<String, Map<String, List<String>>> devices = null;
72 if (!Strings.isNullOrEmpty(deviceId)) {
73 devices = service.showDevice(deviceId);
74 printDeviceData(devices);
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.";
83 devices = service.showDevice(deviceIp, devicePort);
84 printDeviceData(devices);
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();
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);
108 table.print(System.out);
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);
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;