Cleanup NetconfConsoleUtils 06/96906/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 16 Jul 2021 16:56:35 +0000 (18:56 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 16 Jul 2021 17:25:19 +0000 (19:25 +0200)
We have a few strange methods here, clean them up to streamline.

Change-Id: Icf0ca4661d4b62cf9e3855af99a6611d1b719869
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/impl/NetconfCommandsImpl.java
netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java
netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/impl/NetconfCommandsImplTest.java

index 9f284f2093848c09fd1c9d2b25b0349fc1a27c60..d51c803cdf15c8828e2f2ed605b1956d406e1ff6 100644 (file)
@@ -10,11 +10,8 @@ package org.opendaylight.netconf.console.impl;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.Strings;
-import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.MoreExecutors;
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -93,35 +90,30 @@ public class NetconfCommandsImpl implements NetconfCommands {
     @Override
     public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
         final Map<String, Map<String, List<String>>> device = new HashMap<>();
-        List<Node> nodeList = new ArrayList<>();
+        final Node node;
         if (devicePort != null) {
-            nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker));
+            node = NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker);
         } else {
-            nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
+            node = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
         }
-        if (nodeList != null) {
-            for (final Node node : nodeList) {
-                if (node != null) {
-                    final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
-                    final Map<String, List<String>> attributes = new HashMap<>();
-                    attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
-                    attributes.put(NetconfConsoleConstants.NETCONF_IP,
-                            ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
-                    attributes.put(NetconfConsoleConstants.NETCONF_PORT,
-                            ImmutableList.of(netconfNode.getPort().getValue().toString()));
-                    attributes.put(NetconfConsoleConstants.STATUS,
-                            ImmutableList.of(netconfNode.getConnectionStatus().name()));
-                    if (netconfNode.getConnectionStatus().equals(
-                            NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
-                        attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
-                                netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
-                                        .map(AvailableCapability::getCapability).collect(Collectors.toList()));
-                    } else {
-                        attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
-                    }
-                    device.put(node.getNodeId().getValue(), attributes);
-                }
+        if (node != null) {
+            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
+            final Map<String, List<String>> attributes = new HashMap<>();
+            attributes.put(NetconfConsoleConstants.NETCONF_ID, List.of(node.getNodeId().getValue()));
+            attributes.put(NetconfConsoleConstants.NETCONF_IP,
+                List.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
+            attributes.put(NetconfConsoleConstants.NETCONF_PORT, List.of(netconfNode.getPort().getValue().toString()));
+            attributes.put(NetconfConsoleConstants.STATUS, List.of(netconfNode.getConnectionStatus().name()));
+            if (netconfNode.getConnectionStatus().equals(
+                NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
+                attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities()
+                    .getAvailableCapability().stream()
+                        .map(AvailableCapability::getCapability)
+                        .collect(Collectors.toList()));
+            } else {
+                attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, List.of(""));
             }
+            device.put(node.getNodeId().getValue(), attributes);
         }
         return device;
     }
@@ -129,27 +121,23 @@ public class NetconfCommandsImpl implements NetconfCommands {
     @Override
     public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
         final Map<String, Map<String, List<String>>> device = new HashMap<>();
-        final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
-        if (nodeList != null && nodeList.size() > 0) {
-            for (final Node node : nodeList) {
-                final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
-                final Map<String, List<String>> attributes = new HashMap<>();
-                attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
-                attributes.put(NetconfConsoleConstants.NETCONF_IP,
-                        ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
-                attributes.put(NetconfConsoleConstants.NETCONF_PORT,
-                        ImmutableList.of(netconfNode.getPort().getValue().toString()));
-                attributes.put(NetconfConsoleConstants.STATUS,
-                        ImmutableList.of(netconfNode.getConnectionStatus().name()));
-                if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
-                    attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
-                            netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
-                                    .map(AvailableCapability::getCapability).collect(Collectors.toList()));
-                } else {
-                    attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
-                }
-                device.put(node.getNodeId().getValue(), attributes);
+        final Node node = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
+        if (node != null) {
+            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
+            final Map<String, List<String>> attributes = new HashMap<>();
+            attributes.put(NetconfConsoleConstants.NETCONF_ID, List.of(node.getNodeId().getValue()));
+            attributes.put(NetconfConsoleConstants.NETCONF_IP,
+                List.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
+            attributes.put(NetconfConsoleConstants.NETCONF_PORT, List.of(netconfNode.getPort().getValue().toString()));
+            attributes.put(NetconfConsoleConstants.STATUS, List.of(netconfNode.getConnectionStatus().name()));
+            if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
+                attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities()
+                    .getAvailableCapability().stream()
+                        .map(AvailableCapability::getCapability).collect(Collectors.toList()));
+            } else {
+                attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, List.of(""));
             }
+            device.put(node.getNodeId().getValue(), attributes);
         }
         return device;
     }
index 691d793ea9da2d4ae55e8ca607a89c6cf920d29d..f21385ebc4713ac3a459b0103b279da474f673a9 100644 (file)
@@ -7,10 +7,7 @@
  */
 package org.opendaylight.netconf.console.utils;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
@@ -21,49 +18,26 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 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;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class NetconfConsoleUtils {
-
     private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
 
     private NetconfConsoleUtils() {
         throw new IllegalStateException("Instantiating utility class.");
     }
 
-    /**
-     * Returns a list of NETCONF nodes for the IP.
-     * @param deviceIp :IP address of NETCONF device
-     * @param db :An instance of the {@link DataBroker}
-     * @return :list on NETCONF nodes
-     */
-    public static List<Node> getNetconfNodeFromIp(final String deviceIp, final DataBroker db) {
-        final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
-        List<Node> nodes = new ArrayList<>();
-        for (Node node : netconfNodes(topology)) {
-            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
-            if (netconfNode != null
-                    && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
-                nodes.add(node);
-            }
-        }
-        return nodes.isEmpty() ? null : nodes;
-    }
-
     /**
      * Returns the NETCONF node associated with the given nodeId.
      * @param nodeId :Id of the NETCONF device
      * @param db :An instance of the {@link DataBroker}
-     * @return :list on NETCONF nodes
+     * @return A node, or null if it is not present
      */
-    public static List<Node> getNetconfNodeFromId(final String nodeId, final DataBroker db) {
-        final Node node = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
-        if (node != null) {
-            return Arrays.asList(node);
-        }
-        return null;
+    public static Node getNetconfNodeFromId(final String nodeId, final DataBroker db) {
+        return read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
     }
 
     /**
@@ -92,7 +66,7 @@ public final class NetconfConsoleUtils {
      * @return :<code>true</code> if not empty, else, <code>false</code>
      */
     private static Collection<Node> netconfNodes(final Topology topology) {
-        return topology == null ? ImmutableList.of() : topology.nonnullNode().values();
+        return topology == null ? List.of() : topology.nonnullNode().values();
     }
 
     /**
@@ -102,8 +76,8 @@ public final class NetconfConsoleUtils {
      * @param db :An instance of the {@link DataBroker}
      * @return :data read from path
      */
-    public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
-            final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
+    public static <D extends DataObject> D read(final LogicalDatastoreType store, final InstanceIdentifier<D> path,
+            final DataBroker db) {
         final ListenableFuture<Optional<D>> future;
         try (ReadTransaction transaction = db.newReadOnlyTransaction()) {
             future = transaction.read(store, path);
@@ -121,7 +95,9 @@ public final class NetconfConsoleUtils {
             return optionalData.get();
         }
 
-        LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);
+        }
         return null;
     }
 }
index 8b338d539b1a32037605a44d01626ab311ab1243..90a3924cf459dbef6e69bc9f9b67194aedd169d1 100644 (file)
@@ -8,10 +8,8 @@
 package org.opendaylight.netconf.console.impl;
 
 import static junit.framework.TestCase.assertFalse;
-import static junit.framework.TestCase.assertNull;
 import static junit.framework.TestCase.assertTrue;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -202,16 +200,6 @@ public class NetconfCommandsImplTest {
         });
     }
 
-    @Test
-    public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException {
-        final List<Node> nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
-        assertNull(nodesNotExist);
-        createTopology(LogicalDatastoreType.OPERATIONAL);
-        final List<Node> nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
-        assertNotNull(nodes);
-        assertEquals(1, nodes.size());
-    }
-
     private void createTopology(final LogicalDatastoreType dataStoreType)
             throws TimeoutException, InterruptedException, ExecutionException {
         final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);