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