691d793ea9da2d4ae55e8ca607a89c6cf920d29d
[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.collect.ImmutableList;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.ReadTransaction;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public final class NetconfConsoleUtils {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class);
31
32     private NetconfConsoleUtils() {
33         throw new IllegalStateException("Instantiating utility class.");
34     }
35
36     /**
37      * Returns a list of NETCONF nodes for the IP.
38      * @param deviceIp :IP address of NETCONF device
39      * @param db :An instance of the {@link DataBroker}
40      * @return :list on NETCONF nodes
41      */
42     public static List<Node> getNetconfNodeFromIp(final String deviceIp, final DataBroker db) {
43         final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
44         List<Node> nodes = new ArrayList<>();
45         for (Node node : netconfNodes(topology)) {
46             final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
47             if (netconfNode != null
48                     && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) {
49                 nodes.add(node);
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         for (Node node : netconfNodes(topology)) {
80             final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
81             if (netconfNode != null && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)
82                     && devicePort.equals(netconfNode.getPort().getValue().toString())) {
83                 return node;
84             }
85         }
86         return null;
87     }
88
89     /**
90      * Checks if the NETCONF topology contains nodes.
91      * @param topology :NETCONF topology instance
92      * @return :<code>true</code> if not empty, else, <code>false</code>
93      */
94     private static Collection<Node> netconfNodes(final Topology topology) {
95         return topology == null ? ImmutableList.of() : topology.nonnullNode().values();
96     }
97
98     /**
99      * Blocking read transaction.
100      * @param store :DatastoreType
101      * @param path :InstanceIdentifier
102      * @param db :An instance of the {@link DataBroker}
103      * @return :data read from path
104      */
105     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
106             final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
107         final ListenableFuture<Optional<D>> future;
108         try (ReadTransaction transaction = db.newReadOnlyTransaction()) {
109             future = transaction.read(store, path);
110         }
111
112         final Optional<D> optionalData;
113         try {
114             optionalData = future.get();
115         } catch (InterruptedException | ExecutionException e) {
116             LOG.warn("Failed to read {} ", path, e);
117             return null;
118         }
119
120         if (optionalData.isPresent()) {
121             return optionalData.get();
122         }
123
124         LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path);
125         return null;
126     }
127 }