Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / utils / NetconfConsoleUtils.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.console.utils;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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;
25
26 public final class NetconfConsoleUtils {
27
28     private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
29
30     private NetconfConsoleUtils() {
31         throw new IllegalStateException("Instantiating utility class.");
32     }
33
34     /**
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
39      */
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.augmentation(NetconfNode.class);
46                 if (netconfNode != null
47                         && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
48                     nodes.add(node);
49                 }
50             }
51         }
52         return nodes.isEmpty() ? null : nodes;
53     }
54
55     /**
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
60      */
61     public static List<Node> getNetconfNodeFromId(final String nodeId, final DataBroker db) {
62         final Node node = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db);
63         if (node != null) {
64             return Arrays.asList(node);
65         }
66         return null;
67     }
68
69     /**
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
75      */
76     public static Node getNetconfNodeFromIpAndPort(final String deviceIp, final String devicePort,
77                                                    final DataBroker db) {
78         final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
79         if (isNetconfNodesPresent(topology)) {
80             for (Node node : topology.getNode()) {
81                 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
82                 if (netconfNode != null
83                         && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
84                         && devicePort.equals(netconfNode.getPort().getValue().toString())) {
85                     return node;
86                 }
87             }
88         }
89         return null;
90     }
91
92     /**
93      * Checks if the NETCONF topology contains nodes.
94      * @param topology :NETCONF topology instance
95      * @return :<code>true</code> if not empty, else, <code>false</code>
96      */
97     private static boolean isNetconfNodesPresent(final Topology topology) {
98         return topology != null && topology.getNode() != null && !topology.getNode().isEmpty();
99     }
100
101     /**
102      * Blocking read transaction.
103      * @param store :DatastoreType
104      * @param path :InstanceIdentifier
105      * @param db :An instance of the {@link DataBroker}
106      * @return :data read from path
107      */
108     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
109             final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
110         final ListenableFuture<Optional<D>> future;
111         try (ReadTransaction transaction = db.newReadOnlyTransaction()) {
112             future = transaction.read(store, path);
113         }
114
115         final Optional<D> optionalData;
116         try {
117             optionalData = future.get();
118         } catch (InterruptedException | ExecutionException e) {
119             LOG.warn("Failed to read {} ", path, e);
120             return null;
121         }
122
123         if (optionalData.isPresent()) {
124             return optionalData.get();
125         }
126
127         LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);
128         return null;
129     }
130 }