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.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;
27 @Command(name = "show-device", scope = "netconf", description = "Shows netconf device attributes.")
28 public class NetconfShowDeviceCommand implements Action {
31 private NetconfCommands service;
33 public NetconfShowDeviceCommand() {
38 NetconfShowDeviceCommand(final NetconfCommands service) {
39 this.service = service;
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;
52 aliases = { "--identifier" },
53 description = "Node Identifier of the netconf device",
56 private String deviceId;
59 aliases = { "--ipaddress" },
60 description = "IP address of the netconf device",
63 private String deviceIp;
66 aliases = { "--port" },
67 description = "Port of the netconf device",
70 private String devicePort;
73 public Object execute() {
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";
79 Map<String, Map<String, List<String>>> devices = null;
81 if (!Strings.isNullOrEmpty(deviceId)) {
82 devices = service.showDevice(deviceId);
83 printDeviceData(devices);
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.";
92 devices = service.showDevice(deviceIp, devicePort);
93 printDeviceData(devices);
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();
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);
117 table.print(System.out);
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);
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;