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
8 package org.opendaylight.netconf.console.utils;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public final class NetconfConsoleUtils {
27 private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
29 private NetconfConsoleUtils() {
34 * Returns the NETCONF node associated with the given nodeId.
35 * @param nodeId :Id of the NETCONF device
36 * @param db :An instance of the {@link DataBroker}
37 * @return A node, or null if it is not present
39 public static Node getNetconfNodeFromId(final String nodeId, final DataBroker db) {
40 return read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
44 * Returns a list with one NETCONF node for the IP and Port.
45 * @param deviceIp :IP address of NETCONF device
46 * @param devicePort :Port of NETCONF device
47 * @param db :An instance of the {@link DataBroker}
48 * @return :NETCONF node instance
50 public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort,
51 final DataBroker db) {
52 final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
53 for (Node node : netconfNodes(topology)) {
54 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
55 if (netconfNode != null && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
56 && devicePort.equals(netconfNode.getPort().getValue().toString())) {
64 * Checks if the NETCONF topology contains nodes.
65 * @param topology :NETCONF topology instance
66 * @return :<code>true</code> if not empty, else, <code>false</code>
68 private static Collection<Node> netconfNodes(final Topology topology) {
69 return topology == null ? List.of() : topology.nonnullNode().values();
73 * Blocking read transaction.
74 * @param store :DatastoreType
75 * @param path :InstanceIdentifier
76 * @param db :An instance of the {@link DataBroker}
77 * @return :data read from path
79 public static <D extends DataObject> D read(final LogicalDatastoreType store, final InstanceIdentifier<D> path,
80 final DataBroker db) {
81 final ListenableFuture<Optional<D>> future;
82 try (ReadTransaction transaction = db.newReadOnlyTransaction()) {
83 future = transaction.read(store, path);
86 final Optional<D> optionalData;
88 optionalData = future.get();
89 } catch (InterruptedException | ExecutionException e) {
90 LOG.warn("Failed to read {} ", path, e);
94 if (optionalData.isPresent()) {
95 return optionalData.orElseThrow();
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);