Eliminate jsr305 dependencies
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / intenthandler / util / VppNetconfConnectionProbe.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc.  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.lispflowmapping.neutron.intenthandler.util;
9
10 /**
11  * Created by Shakib Ahmed on 1/23/17.
12  */
13
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;
16
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;
41
42 /**
43  * Purpose: verify whether provided netconf node is already connected or wait if not.
44  *
45  * <p>
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.
49  * </p>
50  *
51  */
52 public class VppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<Node> {
53     private static final Logger LOG = LoggerFactory.getLogger(VppNodeReader.class);
54
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();
59
60     private static final String TOPOLOGY_IDENTIFIER = "topology-netconf";
61
62     private final DataTreeIdentifier<Node> path;
63
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))
69                 .build();
70
71         path = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, nodeIid);
72     }
73
74     public boolean startProbing() throws ExecutionException, InterruptedException, TimeoutException {
75         registeredListener = dataBroker.registerDataTreeChangeListener(path, this);
76         return connectionStatusFuture.get(NODE_CONNECTION_TIMER, TimeUnit.SECONDS);
77     }
78
79     @Override
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) {
86                 return;
87             }
88             if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
89                 connectionStatusFuture.set(false);
90                 unregister();
91             } else {
92                 final NetconfNodeConnectionStatus.ConnectionStatus status = netconfNode.getConnectionStatus();
93                 if (status.equals(Connected)) {
94                     connectionStatusFuture.set(true);
95                     unregister();
96                 } else if (!status.equals(Connecting)) {
97                     connectionStatusFuture.set(false);
98                     unregister();
99                 }
100             }
101         });
102     }
103
104     private NetconfNode getNodeAugmentation(Node node) {
105         NetconfNode netconfNode = node.augmentation(NetconfNode.class);
106         if (netconfNode == null) {
107             return null;
108         }
109         return netconfNode;
110     }
111
112     private void unregister() {
113         if (registeredListener != null) {
114             registeredListener.close();
115         }
116     }
117 }