2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.console.utils;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class NetconfConsoleUtils {
29 private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
31 private NetconfConsoleUtils() {
32 throw new IllegalStateException("Instantiating utility class.");
36 * Returns a list of NETCONF nodes for the IP.
37 * @param deviceIp :IP address of NETCONF device
38 * @param db :An instance of the {@link DataBroker}
39 * @return :list on NETCONF nodes
41 public static List<Node> getNetconfNodeFromIp(final String deviceIp, final DataBroker db) {
42 final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
43 List<Node> nodes = new ArrayList<>();
44 if (isNetconfNodesPresent(topology)) {
45 for (Node node : topology.getNode()) {
46 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
47 if (netconfNode != null
48 && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
53 return nodes.isEmpty() ? null : nodes;
57 * Returns the NETCONF node associated with the given nodeId.
58 * @param nodeId :Id of the NETCONF device
59 * @param db :An instance of the {@link DataBroker}
60 * @return :list on NETCONF nodes
62 public static List<Node> getNetconfNodeFromId(final String nodeId, final DataBroker db) {
63 final Node node = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
65 return Arrays.asList(node);
71 * Returns a list with one NETCONF node for the IP and Port.
72 * @param deviceIp :IP address of NETCONF device
73 * @param devicePort :Port of NETCONF device
74 * @param db :An instance of the {@link DataBroker}
75 * @return :NETCONF node instance
77 public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort,
78 final DataBroker db) {
79 final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
80 if (isNetconfNodesPresent(topology)) {
81 for (Node node : topology.getNode()) {
82 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
83 if (netconfNode != null
84 && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
85 && devicePort.equals(netconfNode.getPort().getValue().toString())) {
94 * Checks if the NETCONF topology contains nodes.
95 * @param topology :NETCONF topology instance
96 * @return :<code>true</code> if not empty, else, <code>false</code>
98 private static boolean isNetconfNodesPresent(final Topology topology) {
99 return topology != null && topology.getNode() != null && !topology.getNode().isEmpty();
103 * Blocking read transaction.
104 * @param store :DatastoreType
105 * @param path :InstanceIdentifier
106 * @param db :An instance of the {@link DataBroker}
107 * @return :data read from path
109 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
110 final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
111 final ListenableFuture<Optional<D>> future;
112 try (ReadOnlyTransaction transaction = db.newReadOnlyTransaction()) {
113 future = transaction.read(store, path);
116 final Optional<D> optionalData;
118 optionalData = future.get();
119 } catch (InterruptedException | ExecutionException e) {
120 LOG.warn("Failed to read {} ", path, e);
124 if (optionalData.isPresent()) {
125 return optionalData.get();
128 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);