Migrate shell components to new APIs 57/73757/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Jul 2018 16:42:42 +0000 (18:42 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Jul 2018 17:42:12 +0000 (19:42 +0200)
Karaf has refactored its shell-related APIs, this patch updates
netconf-console to use them.

Change-Id: I8669c19c874d3e851379301712987ada626ef086
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-console/pom.xml
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfConnectDeviceCommand.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfDisconnectDeviceCommand.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfListDevicesCommand.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfShowDeviceCommand.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/commands/NetconfUpdateDeviceCommand.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java
netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/commands/NetconfCommandsImplCallsTest.java

index 1a6b9c29ae501d4f3e542438fa4455712c05db84..b1b77b1da01213a884d4e41c90c9c9438e85e368 100644 (file)
@@ -26,13 +26,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
       <dependency>
           <groupId>org.apache.karaf.shell</groupId>
           <artifactId>org.apache.karaf.shell.console</artifactId>
-          <version>${karaf.version}</version>
-          <scope>provided</scope>
-      </dependency>
-      <dependency>
-          <groupId>org.apache.karaf.shell</groupId>
-          <artifactId>org.apache.karaf.shell.table</artifactId>
-          <version>${karaf.version}</version>
           <scope>provided</scope>
       </dependency>
 
index be70b8155e473d28e95567129dcdd87ea97d9c08..88186de9cc62cb0961a3c5b3fee4ed5d97931605 100644 (file)
@@ -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.";
         }
index ccb9c41bd6c0cb26863d80d2f5bf71a33f225986..b9bf10bb6d365b8004725c30b345ee0efec1841f 100644 (file)
@@ -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);
index f34cbed0014489d34572178e41d8c612440bc635..0db091f92656875afcae870f6770568382d4a9ff 100644 (file)
@@ -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<String, Map<String, String>> allDevices = service.listDevices();
         printDevicesList(allDevices);
         return null;
     }
 
     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
-    private void printDevicesList(@Nonnull final Map<String, Map<String, String>> allDevices) {
+    private static void printDevicesList(@Nonnull final Map<String, Map<String, String>> allDevices) {
         final ShellTable table = new ShellTable();
         table.column(NetconfConsoleConstants.NETCONF_ID).alignLeft();
         table.column(NetconfConsoleConstants.NETCONF_IP).alignLeft();
index 8ab84d7257168c3bd6818f5a1307b8064d20015b..273d9b2208dd25e7d9eaa2f965af5f3fa823713a 100644 (file)
@@ -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<String, Map<String, List<String>>> devices) {
+    private static void printDeviceData(@Nonnull final Map<String, Map<String, List<String>>> 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<String, List<String>> device, final ShellTable table,
-                                    final String capabilityName) {
+    private static void formatCapabilities(final Map<String, List<String>> 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<String, List<String>> device, final String capabilityName,
-            final String availableCapability) {
+    private static boolean isFirstAvailableCapability(final Map<String, List<String>> device,
+            final String capabilityName, final String availableCapability) {
         return device.get(capabilityName).indexOf(availableCapability) == NetconfConsoleConstants.DEFAULT_INDEX;
     }
 }
index df2f6584bf9bbf3aa77fc58689be5d7648a535cc..054d1154118d7445f8bd39c9448e88dc2a598f52 100644 (file)
@@ -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<String, String> 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);
+    }
 }
index 7766847198fe20ce3f65ed50b11b5fcd447c9441..c967413884f45552c25f23c8ee2f11220742117c 100644 (file)
@@ -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 extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
             final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
-        D result = null;
-        final ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
-        Optional<D> optionalData;
+        final ListenableFuture<Optional<D>> future;
+        try (ReadOnlyTransaction transaction = db.newReadOnlyTransaction()) {
+            future = transaction.read(store, path);
+        }
+
+        final Optional<D> 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;
     }
 }
index 45cbb85fa1ba2e90bbeed96352914bf15e80ece8..f0e0af565d4935c45c077279e25396bfe33d2fab 100644 (file)
@@ -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<String, Map<String, List<String>>> getDeviceHashMap() {
         final HashMap<String, Map<String, List<String>>> devices = new HashMap<>();
         final HashMap<String, List<String>> deviceMap = new HashMap<>();
         deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1"));