* 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;
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;
}
/**
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;
* @param topology :NETCONF topology instance
* @return :<code>true</code> if not empty, else, <code>false</code>
*/
- private static boolean isNetconfNodesPresent(final Topology topology) {
- return topology != null && topology.getNode() != null && !topology.getNode().isEmpty();
- }
-
- /**
- * 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();
}
/**
*/
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;
}
}