More reliable netconf transaction handling
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / GbpVppNetconfConnectionProbe.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
10
11 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
12 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
13
14 import javax.annotation.Nonnull;
15 import java.util.Collection;
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.SettableFuture;
18 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 class GbpVppNetconfConnectionProbe implements ClusteredDataTreeChangeListener<Node> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(GbpVppNetconfConnectionProbe.class);
41
42     private final ListenerRegistration<GbpVppNetconfConnectionProbe> registeredListener;
43     private final DataTreeIdentifier<Node> path;
44     private SettableFuture<Boolean> futureStatus;
45     @SuppressWarnings("FieldCanBeLocal")
46     private final String TOPOLOGY_ID = "topology-netconf";
47
48     GbpVppNetconfConnectionProbe(final NodeKey nodekey, final SettableFuture<Boolean> futureStatus, final DataBroker dataBroker) {
49         this.futureStatus = Preconditions.checkNotNull(futureStatus);
50         Preconditions.checkArgument(!futureStatus.isDone());
51         Preconditions.checkNotNull(nodekey);
52         Preconditions.checkNotNull(dataBroker);
53         final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
54                 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID)))
55                 .child(Node.class, nodekey)
56                 .build();
57         path = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodeIid);
58         LOG.debug("Registering listener for node {}", nodekey);
59         registeredListener = dataBroker.registerDataTreeChangeListener(path, this);
60     }
61
62     @Override
63     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
64         changes.forEach(modification -> {
65             final DataObjectModification<Node> rootNode = modification.getRootNode();
66             final Node node = rootNode.getDataAfter();
67             final NetconfNode netconfNode = getNodeAugmentation(node);
68             if (node == null || node.getNodeId() == null) {
69                 return;
70             }
71             final NodeId nodeId = node.getNodeId();
72             if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
73                 LOG.warn("Node {} does not contain netconf augmentation", nodeId);
74                 futureStatus.set(false);
75                 unregister();
76             } else {
77                 final NetconfNodeConnectionStatus.ConnectionStatus status = netconfNode.getConnectionStatus();
78                 if (status.equals(Connecting)) {
79                     LOG.debug("Node {} is connecting", nodeId);
80                 } else if (status.equals(Connected)) {
81                     LOG.debug("Node {} connected", nodeId);
82                     futureStatus.set(true);
83                     unregister();
84                 } else { // Unable to connect
85                     LOG.warn("Unable to connect node {}", nodeId);
86                     futureStatus.set(false);
87                     unregister();
88                 }
89             }
90         });
91     }
92
93     private NetconfNode getNodeAugmentation(Node node) {
94         NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
95         if (netconfNode == null) {
96             LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
97             return null;
98         }
99         return netconfNode;
100     }
101
102     private void unregister() {
103         LOG.debug("Listener for path {} unregistered", path.getRootIdentifier());
104         if (registeredListener != null) {
105             registeredListener.close();
106         }
107     }
108
109 }