<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>
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;
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;
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.";
}
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;
private String deviceId;
@Override
- protected Object doExecute() throws Exception {
+ public Object execute() {
boolean status = false;
if (!Strings.isNullOrEmpty(deviceId)) {
status = service.disconnectDevice(deviceId);
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;
}
@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();
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;
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";
}
@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();
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
}
}
- 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;
}
}
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;
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);
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);
+ }
}
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;
*/
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;
}
}
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());
}
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());
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());
}
final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
- netconfListDeviceCommand.doExecute();
+ netconfListDeviceCommand.execute();
verify(netconfCommands, times(1)).listDevices();
}
@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());
}
doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
- netconfUpdateDeviceCommand.doExecute();
+ netconfUpdateDeviceCommand.execute();
verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(),
hashMapArgumentCaptor.capture());
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"));