From 39481e39b40b93444c401e9aedddfc612640d543 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 4 Jul 2018 18:42:42 +0200 Subject: [PATCH] Migrate shell components to new APIs Karaf has refactored its shell-related APIs, this patch updates netconf-console to use them. Change-Id: I8669c19c874d3e851379301712987ada626ef086 Signed-off-by: Robert Varga --- netconf/netconf-console/pom.xml | 7 ----- .../commands/NetconfConnectDeviceCommand.java | 12 +++---- .../NetconfDisconnectDeviceCommand.java | 10 +++--- .../commands/NetconfListDevicesCommand.java | 12 +++---- .../commands/NetconfShowDeviceCommand.java | 22 ++++++------- .../commands/NetconfUpdateDeviceCommand.java | 16 +++++----- .../console/utils/NetconfConsoleUtils.java | 31 +++++++++++-------- .../NetconfCommandsImplCallsTest.java | 22 ++++++------- 8 files changed, 63 insertions(+), 69 deletions(-) diff --git a/netconf/netconf-console/pom.xml b/netconf/netconf-console/pom.xml index 1a6b9c29ae..b1b77b1da0 100644 --- a/netconf/netconf-console/pom.xml +++ b/netconf/netconf-console/pom.xml @@ -26,13 +26,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL org.apache.karaf.shell org.apache.karaf.shell.console - ${karaf.version} - provided - - - org.apache.karaf.shell - org.apache.karaf.shell.table - ${karaf.version} provided diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java index be70b8155e..88186de9cc 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java @@ -10,12 +10,10 @@ package org.opendaylight.netconf.console.commands; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; - import java.util.Arrays; - -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.commands.Option; -import org.apache.karaf.shell.console.AbstractAction; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; import org.opendaylight.netconf.console.api.NetconfCommands; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; @@ -31,7 +29,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder; @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.") -public class NetconfConnectDeviceCommand extends AbstractAction { +public class NetconfConnectDeviceCommand implements Action { protected final NetconfCommands service; @@ -110,7 +108,7 @@ public class NetconfConnectDeviceCommand extends AbstractAction { private String deviceId; @Override - protected Object doExecute() throws Exception { + public Object execute() { if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) { return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed."; } diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfDisconnectDeviceCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfDisconnectDeviceCommand.java index ccb9c41bd6..b9bf10bb6d 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfDisconnectDeviceCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfDisconnectDeviceCommand.java @@ -10,13 +10,13 @@ package org.opendaylight.netconf.console.commands; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.commands.Option; -import org.apache.karaf.shell.console.AbstractAction; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; import org.opendaylight.netconf.console.api.NetconfCommands; @Command(name = "netconf:disconnect-device", scope = "netconf", description = "Disconnect netconf device.") -public class NetconfDisconnectDeviceCommand extends AbstractAction { +public class NetconfDisconnectDeviceCommand implements Action { protected final NetconfCommands service; @@ -55,7 +55,7 @@ public class NetconfDisconnectDeviceCommand extends AbstractAction { private String deviceId; @Override - protected Object doExecute() throws Exception { + public Object execute() { boolean status = false; if (!Strings.isNullOrEmpty(deviceId)) { status = service.disconnectDevice(deviceId); diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfListDevicesCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfListDevicesCommand.java index f34cbed001..0db091f926 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfListDevicesCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfListDevicesCommand.java @@ -10,14 +10,14 @@ package org.opendaylight.netconf.console.commands; import java.util.Map; import javax.annotation.Nonnull; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.console.AbstractAction; -import org.apache.karaf.shell.table.ShellTable; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.support.table.ShellTable; import org.opendaylight.netconf.console.api.NetconfCommands; import org.opendaylight.netconf.console.utils.NetconfConsoleConstants; @Command(name = "netconf:list-devices", scope = "netconf", description = "List all netconf devices in the topology.") -public class NetconfListDevicesCommand extends AbstractAction { +public class NetconfListDevicesCommand implements Action { protected final NetconfCommands service; @@ -26,14 +26,14 @@ public class NetconfListDevicesCommand extends AbstractAction { } @Override - protected Object doExecute() throws Exception { + public Object execute() { final Map> allDevices = service.listDevices(); printDevicesList(allDevices); return null; } @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private void printDevicesList(@Nonnull final Map> allDevices) { + private static void printDevicesList(@Nonnull final Map> allDevices) { final ShellTable table = new ShellTable(); table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft(); table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft(); diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfShowDeviceCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfShowDeviceCommand.java index 8ab84d7257..273d9b2208 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfShowDeviceCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfShowDeviceCommand.java @@ -14,15 +14,15 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.annotation.Nonnull; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.commands.Option; -import org.apache.karaf.shell.console.AbstractAction; -import org.apache.karaf.shell.table.ShellTable; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.support.table.ShellTable; import org.opendaylight.netconf.console.api.NetconfCommands; import org.opendaylight.netconf.console.utils.NetconfConsoleConstants; @Command(name = "netconf:show-device", scope = "netconf", description = "Shows netconf device attributes.") -public class NetconfShowDeviceCommand extends AbstractAction { +public class NetconfShowDeviceCommand implements Action { protected final NetconfCommands service; @@ -61,7 +61,7 @@ public class NetconfShowDeviceCommand extends AbstractAction { private String devicePort; @Override - protected Object doExecute() throws Exception { + public Object execute() { if ((Strings.isNullOrEmpty(deviceIp) || Strings.isNullOrEmpty(devicePort)) && Strings.isNullOrEmpty(deviceId)) { return "You must provide either the device Ip and the device Port or the device Id"; @@ -86,7 +86,7 @@ public class NetconfShowDeviceCommand extends AbstractAction { } @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private void printDeviceData(@Nonnull final Map>> devices) { + private static void printDeviceData(@Nonnull final Map>> devices) { final ShellTable table = new ShellTable(); table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft(); table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft(); @@ -108,8 +108,8 @@ public class NetconfShowDeviceCommand extends AbstractAction { table.print(System.out); } - private void formatCapabilities(final Map> device, final ShellTable table, - final String capabilityName) { + private static void formatCapabilities(final Map> device, final ShellTable table, + final String capabilityName) { for (final String availableCapability : device.get(capabilityName)) { // First row is already added to table with the first available capability // Process rows other than the first to only have remaining available capabilities @@ -120,8 +120,8 @@ public class NetconfShowDeviceCommand extends AbstractAction { } } - private boolean isFirstAvailableCapability(final Map> device, final String capabilityName, - final String availableCapability) { + private static boolean isFirstAvailableCapability(final Map> device, + final String capabilityName, final String availableCapability) { return device.get(capabilityName).indexOf(availableCapability) == NetconfConsoleConstants.DEFAULT_INDEX; } } diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfUpdateDeviceCommand.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfUpdateDeviceCommand.java index df2f6584bf..054d115411 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfUpdateDeviceCommand.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfUpdateDeviceCommand.java @@ -11,14 +11,14 @@ package org.opendaylight.netconf.console.commands; import com.google.common.annotations.VisibleForTesting; import java.util.HashMap; import java.util.Map; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.commands.Option; -import org.apache.karaf.shell.console.AbstractAction; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; import org.opendaylight.netconf.console.api.NetconfCommands; import org.opendaylight.netconf.console.utils.NetconfConsoleConstants; @Command(name = "netconf:update-device", scope = "netconf", description = "Update netconf device attributes.") -public class NetconfUpdateDeviceCommand extends AbstractAction { +public class NetconfUpdateDeviceCommand implements Action { protected final NetconfCommands service; @@ -96,7 +96,7 @@ public class NetconfUpdateDeviceCommand extends AbstractAction { private String newSchemaless = "false"; @Override - protected Object doExecute() throws Exception { + public Object execute() { Map updated = new HashMap<>(); updated.put(NetconfConsoleConstants.NETCONF_IP, newIp); @@ -109,10 +109,8 @@ public class NetconfUpdateDeviceCommand extends AbstractAction { if (updated.isEmpty()) { return "Nothing to update."; - } else { - String statusMessage = service.updateDevice(deviceId, username, password, updated); - return statusMessage; } - } + return service.updateDevice(deviceId, username, password, updated); + } } diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java index 7766847198..c967413884 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java @@ -9,13 +9,14 @@ package org.opendaylight.netconf.console.utils; import com.google.common.base.Optional; +import com.google.common.util.concurrent.ListenableFuture; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; @@ -119,20 +120,24 @@ public final class NetconfConsoleUtils { */ public static D read( final LogicalDatastoreType store, final InstanceIdentifier path, final DataBroker db) { - D result = null; - final ReadOnlyTransaction transaction = db.newReadOnlyTransaction(); - Optional optionalData; + final ListenableFuture> future; + try (ReadOnlyTransaction transaction = db.newReadOnlyTransaction()) { + future = transaction.read(store, path); + } + + final Optional optionalData; try { - optionalData = transaction.read(store, path).checkedGet(); - if (optionalData.isPresent()) { - result = optionalData.get(); - } else { - LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path); - } - } catch (ReadFailedException e) { + optionalData = future.get(); + } catch (InterruptedException | ExecutionException e) { LOG.warn("Failed to read {} ", path, e); + return null; } - transaction.close(); - return result; + + if (optionalData.isPresent()) { + return optionalData.get(); + } + + LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path); + return null; } } diff --git a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java index 45cbb85fa1..f0e0af565d 100644 --- a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java +++ b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java @@ -51,14 +51,14 @@ public class NetconfCommandsImplCallsTest { public void testConnectDeviceCommand() throws Exception { NetconfConnectDeviceCommand netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands); - netconfConnectDeviceCommand.doExecute(); + netconfConnectDeviceCommand.execute(); verify(netconfCommands, times(0)).connectDevice(any(), any()); netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777"); PowerMockito.mockStatic(Strings.class); given(Strings.isNullOrEmpty(any())).willReturn(false); - netconfConnectDeviceCommand.doExecute(); + netconfConnectDeviceCommand.execute(); doNothing().when(netconfCommands).connectDevice(any(), any()); verify(netconfCommands, times(1)).connectDevice(any(), any()); } @@ -67,14 +67,14 @@ public class NetconfCommandsImplCallsTest { public void testDisconnectDeviceCommand() throws Exception { NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands); - netconfDisconnectDeviceCommand.doExecute(); + netconfDisconnectDeviceCommand.execute(); verify(netconfCommands, times(0)).disconnectDevice(any(), any()); netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null); doReturn(true).when(netconfCommands).disconnectDevice(any()); - netconfDisconnectDeviceCommand.doExecute(); + netconfDisconnectDeviceCommand.execute(); verify(netconfCommands, times(1)).disconnectDevice(any()); @@ -82,7 +82,7 @@ public class NetconfCommandsImplCallsTest { new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777"); doReturn(true).when(netconfCommands).disconnectDevice(any(), any()); - netconfDisconnectDeviceCommand.doExecute(); + netconfDisconnectDeviceCommand.execute(); verify(netconfCommands, times(1)).disconnectDevice(any(), any()); } @@ -92,7 +92,7 @@ public class NetconfCommandsImplCallsTest { final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands); doReturn(getDeviceHashMap()).when(netconfCommands).listDevices(); - netconfListDeviceCommand.doExecute(); + netconfListDeviceCommand.execute(); verify(netconfCommands, times(1)).listDevices(); } @@ -100,21 +100,21 @@ public class NetconfCommandsImplCallsTest { @Test public void testShowDeviceCommand() throws Exception { NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands); - netconfShowDeviceCommand.doExecute(); + netconfShowDeviceCommand.execute(); verify(netconfCommands, times(0)).showDevice(any()); netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null); doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any()); - netconfShowDeviceCommand.doExecute(); + netconfShowDeviceCommand.execute(); verify(netconfCommands, times(1)).showDevice(any()); netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777"); doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any()); - netconfShowDeviceCommand.doExecute(); + netconfShowDeviceCommand.execute(); verify(netconfCommands, times(1)).showDevice(any(), any()); } @@ -129,7 +129,7 @@ public class NetconfCommandsImplCallsTest { doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any()); - netconfUpdateDeviceCommand.doExecute(); + netconfUpdateDeviceCommand.execute(); verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(), hashMapArgumentCaptor.capture()); @@ -138,7 +138,7 @@ public class NetconfCommandsImplCallsTest { assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP)); } - private HashMap getDeviceHashMap() { + private static HashMap>> getDeviceHashMap() { final HashMap>> devices = new HashMap<>(); final HashMap> deviceMap = new HashMap<>(); deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1")); -- 2.36.6