2 * Copyright (c) 2017 Cisco Systems, Inc. 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.lispflowmapping.neutron.intenthandler.util;
11 * Created by Shakib Ahmed on 1/23/17.
14 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
15 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Collection;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.DataObjectModification;
25 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.mdsal.binding.api.DataTreeModification;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Purpose: verify whether provided netconf node is already connected or wait if not.
46 * VppNetconfConnectionProbe registers istener which catches node-related changes from topology-netconf.
47 * A {@link SettableFuture} is set {@link Boolean#TRUE}, if the node is connected within {@link
48 * VppNetconfConnectionProbe#NODE_CONNECTION_TIMER} seconds. Else, proper exception is throws.
52 public class VppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<Node> {
53 private static final Logger LOG = LoggerFactory.getLogger(VppNodeReader.class);
55 public static final int NODE_CONNECTION_TIMER = 60;
56 private final DataBroker dataBroker;
57 private ListenerRegistration<VppNetconfConnectionProbe> registeredListener;
58 private final SettableFuture<Boolean> connectionStatusFuture = SettableFuture.create();
60 private static final String TOPOLOGY_IDENTIFIER = "topology-netconf";
62 private final DataTreeIdentifier<Node> path;
64 public VppNetconfConnectionProbe(final NodeId nodeId, final DataBroker dataBroker) {
65 this.dataBroker = dataBroker;
66 final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
67 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_IDENTIFIER)))
68 .child(Node.class, new NodeKey(nodeId))
71 path = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, nodeIid);
74 public boolean startProbing() throws ExecutionException, InterruptedException, TimeoutException {
75 registeredListener = dataBroker.registerDataTreeChangeListener(path, this);
76 return connectionStatusFuture.get(NODE_CONNECTION_TIMER, TimeUnit.SECONDS);
80 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
81 changes.forEach(modification -> {
82 final DataObjectModification<Node> rootNode = modification.getRootNode();
83 final Node node = rootNode.getDataAfter();
84 final NetconfNode netconfNode = getNodeAugmentation(node);
85 if (node == null || node.getNodeId() == null) {
88 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
89 connectionStatusFuture.set(false);
92 final NetconfNodeConnectionStatus.ConnectionStatus status = netconfNode.getConnectionStatus();
93 if (status.equals(Connected)) {
94 connectionStatusFuture.set(true);
96 } else if (!status.equals(Connecting)) {
97 connectionStatusFuture.set(false);
104 private NetconfNode getNodeAugmentation(Node node) {
105 NetconfNode netconfNode = node.augmentation(NetconfNode.class);
106 if (netconfNode == null) {
112 private void unregister() {
113 if (registeredListener != null) {
114 registeredListener.close();