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 java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class NetconfConsoleUtils {
28 private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
30 private NetconfConsoleUtils() {
31 throw new IllegalStateException("Instantiating utility class.");
35 * Returns a list of NETCONF nodes for the IP
36 * @param deviceIp :IP address of NETCONF device
37 * @param db :An instance of the {@link DataBroker}
38 * @return :list on NETCONF nodes
40 public static List<Node> getNetconfNodeFromIp(final String deviceIp, final DataBroker db) {
41 final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
42 List<Node> nodes = new ArrayList<>();
43 if (isNetconfNodesPresent(topology)) {
44 for (Node node : topology.getNode()) {
45 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
46 if (netconfNode != null
47 && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
52 return (nodes.isEmpty()) ? null : nodes;
56 * Returns the NETCONF node associated with the given nodeId.
57 * @param nodeId :Id of the NETCONF device
58 * @param db :An instance of the {@link DataBroker}
59 * @return :list on NETCONF nodes
61 public static List<Node> getNetconfNodeFromId(final String nodeId, final DataBroker db) {
62 final Node node = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
64 return Arrays.asList(node);
70 * Returns a list with one NETCONF node for the IP and Port
71 * @param deviceIp :IP address of NETCONF device
72 * @param devicePort :Port of NETCONF device
73 * @param db :An instance of the {@link DataBroker}
74 * @return :NETCONF node instance
76 public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort, final DataBroker db) {
77 final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
78 if (isNetconfNodesPresent(topology)) {
79 for (Node node : topology.getNode()) {
80 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
81 if (netconfNode != null
82 && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
83 && devicePort.equals(netconfNode.getPort().getValue().toString()))
91 * Checks if the NETCONF topology contains nodes
92 * @param topology :NETCONF topology instance
93 * @return :<code>true</code> if not empty, else, <code>false</code>
95 private static boolean isNetconfNodesPresent(final Topology topology) {
96 if (topology == null || topology.getNode() == null || topology.getNode().isEmpty()) {
103 * Wait for datastore to populate NETCONF data
104 * @param deviceIp :IP address of NETCONF device
106 public static void waitForUpdate(final String deviceIp) {
108 Thread.sleep(NetconfConsoleConstants.DEFAULT_TIMEOUT_MILLIS);
109 } catch (final InterruptedException e) {
110 LOG.warn("Interrupted while waiting after Netconf node {}", deviceIp, e);
115 * Blocking read transaction
116 * @param store :DatastoreType
117 * @param path :InstanceIdentifier
118 * @param db :An instance of the {@link DataBroker}
119 * @return :data read from path
121 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(final LogicalDatastoreType store,
122 final InstanceIdentifier<D> path, final DataBroker db) {
124 final ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
125 Optional<D> optionalData;
127 optionalData = transaction.read(store, path).checkedGet();
128 if (optionalData.isPresent()) {
129 result = optionalData.get();
131 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);
133 } catch (ReadFailedException e) {
134 LOG.warn("Failed to read {} ", path, e);