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 javax.annotation.Nonnull;
23 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification;
26 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Purpose: verify whether provided netconf node is already connected or wait if not.
47 * VppNetconfConnectionProbe registers istener which catches node-related changes from topology-netconf.
48 * A {@link SettableFuture} is set {@link Boolean#TRUE}, if the node is connected within {@link
49 * VppNetconfConnectionProbe#NODE_CONNECTION_TIMER} seconds. Else, proper exception is throws.
53 public class VppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<Node> {
54 private static final Logger LOG = LoggerFactory.getLogger(VppNodeReader.class);
56 public static final int NODE_CONNECTION_TIMER = 60;
57 private final DataBroker dataBroker;
58 private ListenerRegistration<VppNetconfConnectionProbe> registeredListener;
59 private final SettableFuture<Boolean> connectionStatusFuture = SettableFuture.create();
61 private static final String TOPOLOGY_IDENTIFIER = "topology-netconf";
63 private final DataTreeIdentifier<Node> path;
65 public VppNetconfConnectionProbe(final NodeId nodeId, final DataBroker dataBroker) {
66 this.dataBroker = dataBroker;
67 final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
68 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_IDENTIFIER)))
69 .child(Node.class, new NodeKey(nodeId))
72 path = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, nodeIid);
75 public boolean startProbing() throws ExecutionException, InterruptedException, TimeoutException {
76 registeredListener = dataBroker.registerDataTreeChangeListener(path, this);
77 return connectionStatusFuture.get(NODE_CONNECTION_TIMER, TimeUnit.SECONDS);
81 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
82 changes.forEach(modification -> {
83 final DataObjectModification<Node> rootNode = modification.getRootNode();
84 final Node node = rootNode.getDataAfter();
85 final NetconfNode netconfNode = getNodeAugmentation(node);
86 if (node == null || node.getNodeId() == null) {
89 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
90 connectionStatusFuture.set(false);
93 final NetconfNodeConnectionStatus.ConnectionStatus status = netconfNode.getConnectionStatus();
94 if (status.equals(Connected)) {
95 connectionStatusFuture.set(true);
97 } else if (!status.equals(Connecting)) {
98 connectionStatusFuture.set(false);
105 private NetconfNode getNodeAugmentation(Node node) {
106 NetconfNode netconfNode = node.augmentation(NetconfNode.class);
107 if (netconfNode == null) {
113 private void unregister() {
114 if (registeredListener != null) {
115 registeredListener.close();