fixed unreliable unit test in ios-xe
[groupbasedpolicy.git] / renderers / ios-xe / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / manager / NodeManager.java
index 3214c5753a876722d082065e8ef0ea880d0af4dd..406ab5c53e5c8f310220f8ad64ef87d91a23cbbf 100644 (file)
@@ -10,18 +10,28 @@ package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.CheckedFuture;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.NodeWriter;
+import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NetconfTransactionCreator;
+import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NodeWriter;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.RendererName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
@@ -32,9 +42,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
@@ -45,7 +53,6 @@ public class NodeManager {
     public static final RendererName iosXeRenderer = new RendererName("ios-xe-renderer");
     private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
     private static final Logger LOG = LoggerFactory.getLogger(NodeManager.class);
-    private static final Map<InstanceIdentifier, DataBroker> netconfNodeCache = new HashMap<>();
     private final DataBroker dataBroker;
     private final MountPointService mountService;
     private final List<String> requiredCapabilities;
@@ -56,10 +63,6 @@ public class NodeManager {
         requiredCapabilities = new RequiredCapabilities().initializeRequiredCapabilities();
     }
 
-    static DataBroker getDataBrokerFromCache(InstanceIdentifier iid) {
-        return netconfNodeCache.get(iid); // TODO read from DS
-    }
-
     public void syncNodes(Node dataAfter, Node dataBefore) {
         // New node
         if (dataBefore == null && dataAfter != null) {
@@ -127,14 +130,19 @@ public class NodeManager {
         InstanceIdentifier mountPointIid = getMountpointIid(node);
         // Mountpoint iid == path in renderer-node
         RendererNode rendererNode = remapNode(mountPointIid);
-        DataBroker mountpoint = getNodeMountPoint(mountPointIid);
         NodeWriter nodeWriter = new NodeWriter();
         nodeWriter.cache(rendererNode);
         if (isCapableNetconfDevice(node, netconfNode)) {
+            resolveDisconnectedNode(node);
+            return;
+        }
+        IpAddress managementIpAddress = netconfNode.getHost().getIpAddress();
+        if (managementIpAddress == null) {
+            LOG.warn("Node {} does not contain management ip address", node.getNodeId().getValue());
+            resolveDisconnectedNode(node);
             return;
         }
         nodeWriter.commitToDatastore(dataBroker);
-        netconfNodeCache.put(mountPointIid, mountpoint);
     }
 
     private void resolveDisconnectedNode(Node node) {
@@ -143,7 +151,6 @@ public class NodeManager {
         NodeWriter nodeWriter = new NodeWriter();
         nodeWriter.cache(rendererNode);
         nodeWriter.removeFromDatastore(dataBroker);
-        netconfNodeCache.remove(mountPointIid);
     }
 
     private RendererNode remapNode(InstanceIdentifier path) {
@@ -183,7 +190,10 @@ public class NodeManager {
         return true;
     }
 
-    private DataBroker getNodeMountPoint(InstanceIdentifier mountPointIid) {
+    DataBroker getNodeMountPoint(InstanceIdentifier mountPointIid) {
+        if (mountPointIid == null) {
+            return null;
+        }
         Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
         MountPoint mountPoint;
         if (optionalObject.isPresent()) {
@@ -202,6 +212,38 @@ public class NodeManager {
         return null;
     }
 
+    NodeId getNodeIdByMountpointIid(InstanceIdentifier mountpointIid) {
+        NodeKey identifier = (NodeKey) mountpointIid.firstKeyOf(Node.class);
+        return identifier.getNodeId();
+    }
+
+    String getNodeManagementIpByMountPointIid(InstanceIdentifier<?> mountpointIid) {
+        NodeId nodeId = getNodeIdByMountpointIid(mountpointIid);
+        InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(new TopologyId(NodeManager.TOPOLOGY_ID)))
+                .child(Node.class, new NodeKey(nodeId))
+                .build();
+        ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
+        CheckedFuture<Optional<Node>, ReadFailedException> submitFuture =
+                transaction.read(LogicalDatastoreType.CONFIGURATION, nodeIid);
+        transaction.close();
+        try {
+            Optional<Node> nodeOptional = submitFuture.checkedGet();
+            if (nodeOptional.isPresent()) {
+                NetconfNode netconfNode = getNodeAugmentation(nodeOptional.get());
+                return java.util.Optional.ofNullable(netconfNode)
+                        .map(NetconfNodeConnectionParameters::getHost)
+                        .map(Host::getIpAddress)
+                        .map(IpAddress::getIpv4Address)
+                        .map(Ipv4Address::getValue)
+                        .orElse(null);
+            }
+        } catch (ReadFailedException e) {
+            LOG.warn("Read node failed {}", nodeId, e);
+        }
+        return null;
+    }
+
     private NetconfNode getNodeAugmentation(Node node) {
         NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
         if (netconfNode == null) {
@@ -228,9 +270,8 @@ public class NodeManager {
          * @return list of string representations of required capabilities
          */
         List<String> initializeRequiredCapabilities() {
-            String writableDataStore = "urn:ietf:params:netconf:capability:writable-running:1.0";
             String capabilityEntries[] = {ned, tailfCommon, tailfCliExtension, tailfMetaExtension, ietfYangTypes,
-                    ietfInetTypes, writableDataStore};
+                    ietfInetTypes};
             return Arrays.asList(capabilityEntries);
         }
     }