Update MRI projects for Aluminium
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / utils / NetconfConsoleUtils.java
index 0845764f21ce81cf4ee7a3e9ba7b518c5fa9b0a9..691d793ea9da2d4ae55e8ca607a89c6cf920d29d 100644 (file)
@@ -5,17 +5,19 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.console.utils;
 
-import com.google.common.base.Optional;
+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 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 java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+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;
@@ -23,7 +25,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfConsoleUtils {
+public final class NetconfConsoleUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
 
@@ -40,16 +42,14 @@ public class NetconfConsoleUtils {
     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<>();
-        if (isNetconfNodesPresent(topology)) {
-            for (Node node : topology.getNode()) {
-                final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
-                if (netconfNode != null
-                        && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
-                    nodes.add(node);
-                }
+        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;
+        return nodes.isEmpty() ? null : nodes;
     }
 
     /**
@@ -76,14 +76,11 @@ public class NetconfConsoleUtils {
     public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort,
                                                    final DataBroker db) {
         final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
-        if (isNetconfNodesPresent(topology)) {
-            for (Node node : topology.getNode()) {
-                final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
-                if (netconfNode != null
-                        && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
-                        && devicePort.equals(netconfNode.getPort().getValue().toString())) {
-                    return node;
-                }
+        for (Node node : netconfNodes(topology)) {
+            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
+            if (netconfNode != null && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
+                    && devicePort.equals(netconfNode.getPort().getValue().toString())) {
+                return node;
             }
         }
         return null;
@@ -94,23 +91,8 @@ public class NetconfConsoleUtils {
      * @param topology :NETCONF topology instance
      * @return :<code>true</code> if not empty, else, <code>false</code>
      */
-    private static boolean isNetconfNodesPresent(final Topology topology) {
-        if (topology == null || topology.getNode() == null || topology.getNode().isEmpty()) {
-            return false;
-        }
-        return true;
-    }
-
-    /**
-     * Wait for datastore to populate NETCONF data.
-     * @param deviceIp :IP address of NETCONF device
-     */
-    public static void waitForUpdate(final String deviceIp) {
-        try {
-            Thread.sleep(NetconfConsoleConstants.DEFAULT_TIMEOUT_MILLIS);
-        } catch (final InterruptedException e) {
-            LOG.warn("Interrupted while waiting after Netconf node {}", deviceIp, e);
-        }
+    private static Collection<Node> netconfNodes(final Topology topology) {
+        return topology == null ? ImmutableList.of() : topology.nonnullNode().values();
     }
 
     /**
@@ -122,20 +104,24 @@ public 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 (ReadTransaction 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;
     }
 }