Update MRI projects for Aluminium
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / utils / NetconfConsoleUtils.java
index 1ce147a5c7f0e8716606c6c5a80e7d229f059b1a..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);
 
@@ -32,7 +34,7 @@ public class NetconfConsoleUtils {
     }
 
     /**
-     * Returns a list of NETCONF nodes for the IP
+     * 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
@@ -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;
     }
 
     /**
@@ -67,73 +67,61 @@ public class NetconfConsoleUtils {
     }
 
     /**
-     * Returns a list with one NETCONF node for the IP and Port
+     * Returns a list with one NETCONF node for the IP and Port.
      * @param deviceIp :IP address of NETCONF device
      * @param devicePort :Port of NETCONF device
      * @param db :An instance of the {@link DataBroker}
      * @return :NETCONF node instance
      */
-    public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort, final DataBroker db) {
+    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;
     }
 
     /**
-     * Checks if the NETCONF topology contains nodes
+     * Checks if the NETCONF topology contains nodes.
      * @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();
     }
 
     /**
-     * Blocking read transaction
+     * Blocking read transaction.
      * @param store :DatastoreType
      * @param path :InstanceIdentifier
      * @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) {
-        D result = null;
-        final ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
-        Optional<D> optionalData;
+    public static <D extends org.opendaylight.yangtools.yang.binding.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);
+        }
+
+        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;
     }
 }