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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Strings;
14 import java.util.List;
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;
27 @Command(name = "show-device", scope = "netconf", description = "Shows netconf device attributes.")
28 public class NetconfShowDeviceCommand implements Action {
30 private NetconfCommands service;
33 aliases = { "--identifier" },
34 description = "Node Identifier of the netconf device",
40 aliases = { "--ipaddress" },
41 description = "IP address of the netconf device",
47 aliases = { "--port" },
48 description = "Port of the netconf device",
53 public NetconfShowDeviceCommand() {
54 // Nothing here, uses injection
58 NetconfShowDeviceCommand(final NetconfCommands service) {
59 this.service = requireNonNull(service);
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;
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";
76 if (!Strings.isNullOrEmpty(deviceId)) {
77 return printDeviceData(service.showDevice(deviceId));
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.";
83 return printDeviceData(service.showDevice(deviceIp, devicePort));
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();
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);
105 table.print(System.out);
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);
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;