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
8 package org.opendaylight.netconf.console.commands;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Strings;
12 import java.util.List;
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;
26 @Command(name = "show-device", scope = "netconf", description = "Shows netconf device attributes.")
27 public class NetconfShowDeviceCommand implements Action {
30 private NetconfCommands service;
32 public NetconfShowDeviceCommand() {
37 NetconfShowDeviceCommand(final NetconfCommands service) {
38 this.service = service;
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;
51 aliases = { "--identifier" },
52 description = "Node Identifier of the netconf device",
55 private String deviceId;
58 aliases = { "--ipaddress" },
59 description = "IP address of the netconf device",
62 private String deviceIp;
65 aliases = { "--port" },
66 description = "Port of the netconf device",
69 private String devicePort;
72 public Object execute() {
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";
78 Map<String, Map<String, List<String>>> devices = null;
80 if (!Strings.isNullOrEmpty(deviceId)) {
81 devices = service.showDevice(deviceId);
82 printDeviceData(devices);
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.";
91 devices = service.showDevice(deviceIp, devicePort);
92 printDeviceData(devices);
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();
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);
116 table.print(System.out);
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);
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;