Repopulate router, port and floating ip related caches
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SouthboundImpl.java
index 383e0c55dee7a2c4ae83318e68bd1891e67ece7c..2ea54897d85d5eb0169b6299b855fc643e474415 100644 (file)
@@ -21,6 +21,8 @@ import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeDpdk;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
@@ -36,6 +38,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
@@ -85,9 +88,9 @@ public class SouthboundImpl implements Southbound {
         return databroker;
     }
 
-    public ConnectionInfo getConnectionInfo(Node node) {
+    public ConnectionInfo getConnectionInfo(Node ovsdbNode) {
         ConnectionInfo connectionInfo = null;
-        OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
+        OvsdbNodeAugmentation ovsdbNodeAugmentation = extractOvsdbNode(ovsdbNode);
         if (ovsdbNodeAugmentation != null) {
             connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
         }
@@ -125,25 +128,32 @@ public class SouthboundImpl implements Southbound {
         return ovsdbNodes;
     }
 
-    public OvsdbNodeAugmentation readOvsdbNode(Node bridgeNode) {
-        OvsdbNodeAugmentation nodeAugmentation = null;
+    public List<Node> readOvsdbTopologyBridgeNodes() {
+        List<Node> ovsdbNodes = new ArrayList<>();
+        InstanceIdentifier<Topology> topologyInstanceIdentifier = MdsalHelper.createInstanceIdentifier();
+        Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, topologyInstanceIdentifier);
+        if (topology != null && topology.getNode() != null) {
+            for (Node node : topology.getNode()) {
+                OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
+                if (ovsdbBridgeAugmentation != null) {
+                    ovsdbNodes.add(node);
+                }
+            }
+        }
+        return ovsdbNodes;
+    }
+
+    public Node readOvsdbNode(Node bridgeNode) {
+        Node ovsdbNode = null;
         OvsdbBridgeAugmentation bridgeAugmentation = extractBridgeAugmentation(bridgeNode);
-        if(bridgeAugmentation != null){
+        if (bridgeAugmentation != null) {
             InstanceIdentifier<Node> ovsdbNodeIid =
                     (InstanceIdentifier<Node>) bridgeAugmentation.getManagedBy().getValue();
-            Node node = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
-            if (node != null){
-                nodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
-                LOG.debug("readOvsdbNode: Ovsdb node {} found that manages bridge {}",
-                        nodeAugmentation != null?nodeAugmentation:"not",bridgeAugmentation);
-            }else {
-                LOG.debug ("readOvsdbNode: Ovsdb node that manages bridge {} not found. ",bridgeAugmentation);
-            }
-
+            ovsdbNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
         }else{
             LOG.debug("readOvsdbNode: Provided node is not a bridge node : {}",bridgeNode);
         }
-        return nodeAugmentation;
+        return ovsdbNode;
     }
 
     public String getOvsdbNodeUUID(Node node) {
@@ -170,10 +180,10 @@ public class SouthboundImpl implements Southbound {
         return value;
     }
 
-    public boolean addBridge(Node ovsdbNode, String bridgeName, String target) throws InvalidParameterException {
+    public boolean addBridge(Node ovsdbNode, String bridgeName, List<String> controllersStr) {
         boolean result = false;
 
-        LOG.info("addBridge: node: {}, bridgeName: {}, target: {}", ovsdbNode, bridgeName, target);
+        LOG.info("addBridge: node: {}, bridgeName: {}, controller(s): {}", ovsdbNode, bridgeName, controllersStr);
         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
         if (connectionInfo != null) {
             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
@@ -182,12 +192,15 @@ public class SouthboundImpl implements Southbound {
             NodeId bridgeNodeId = MdsalHelper.createManagedNodeId(bridgeIid);
             bridgeNodeBuilder.setNodeId(bridgeNodeId);
             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
-            ovsdbBridgeAugmentationBuilder.setControllerEntry(createControllerEntries(target));
+            ovsdbBridgeAugmentationBuilder.setControllerEntry(createControllerEntries(controllersStr));
             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
             ovsdbBridgeAugmentationBuilder.setProtocolEntry(createMdsalProtocols());
             ovsdbBridgeAugmentationBuilder.setFailMode(
                     MdsalHelper.OVSDB_FAIL_MODE_MAP.inverse().get("secure"));
             setManagedByForBridge(ovsdbBridgeAugmentationBuilder, ovsdbNode.getKey());
+            if (isOvsdbNodeDpdk(ovsdbNode)) {
+                ovsdbBridgeAugmentationBuilder.setDatapathType(DatapathTypeNetdev.class);
+            }
             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
 
             result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
@@ -223,8 +236,12 @@ public class SouthboundImpl implements Southbound {
     }
 
     public Node readBridgeNode(Node node, String name) {
+        Node ovsdbNode = node;
+        if (extractNodeAugmentation(ovsdbNode) == null) {
+            ovsdbNode = readOvsdbNode(node);
+        }
         Node bridgeNode = null;
-        ConnectionInfo connectionInfo = getConnectionInfo(node);
+        ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
         if (connectionInfo != null) {
             InstanceIdentifier<Node> bridgeIid =
             MdsalHelper.createInstanceIdentifier(node.getKey(), name);
@@ -236,10 +253,8 @@ public class SouthboundImpl implements Southbound {
     public Node getBridgeNode(Node node, String bridgeName) {
         Node bridgeNode = null;
         OvsdbBridgeAugmentation bridge = extractBridgeAugmentation(node);
-        if (bridge != null) {
-            if (bridge.getBridgeName().getValue().equals(bridgeName)) {
+        if (bridge != null && bridge.getBridgeName().getValue().equals(bridgeName)) {
                 bridgeNode = node;
-            }
         } else {
             bridgeNode = readBridgeNode(node, bridgeName);
         }
@@ -262,10 +277,10 @@ public class SouthboundImpl implements Southbound {
         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
     }
 
-    private void setControllerForBridge(Node ovsdbNode, String bridgeName, String targetString) {
+    private void setControllersForBridge(Node ovsdbNode, String bridgeName, List<String> controllersString) {
         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
         if (connectionInfo != null) {
-            for (ControllerEntry controllerEntry: createControllerEntries(targetString)) {
+            for (ControllerEntry controllerEntry : createControllerEntries(controllersString)) {
                 InstanceIdentifier<ControllerEntry> iid =
                         MdsalHelper.createInstanceIdentifier(ovsdbNode.getKey(), bridgeName)
                                 .augmentation(OvsdbBridgeAugmentation.class)
@@ -277,20 +292,24 @@ public class SouthboundImpl implements Southbound {
         }
     }
 
-    private List<ControllerEntry> createControllerEntries(String targetString) {
-        List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
-        ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
-        controllerEntryBuilder.setTarget(new Uri(targetString));
-        controllerEntries.add(controllerEntryBuilder.build());
+    private List<ControllerEntry> createControllerEntries(List<String> controllersStr) {
+        List<ControllerEntry> controllerEntries = new ArrayList<>();
+        if (controllersStr != null) {
+            for (String controllerStr : controllersStr) {
+                ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
+                controllerEntryBuilder.setTarget(new Uri(controllerStr));
+                controllerEntries.add(controllerEntryBuilder.build());
+            }
+        }
         return controllerEntries;
     }
 
     private List<ProtocolEntry> createMdsalProtocols() {
-        List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
+        List<ProtocolEntry> protocolList = new ArrayList<>();
         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
                 MdsalHelper.OVSDB_PROTOCOL_MAP.inverse();
         protocolList.add(new ProtocolEntryBuilder().
-                setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
+                setProtocol(mapper.get("OpenFlow13")).build());
         return protocolList;
     }
 
@@ -322,17 +341,14 @@ public class SouthboundImpl implements Southbound {
 
     public OvsdbBridgeAugmentation getBridge(Node node, String name) {
         OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
-        if (bridge != null) {
-            if (!bridge.getBridgeName().getValue().equals(name)) {
-                bridge = null;
-            }
+        if ((bridge != null) && (!bridge.getBridgeName().getValue().equals(name))) {
+            bridge = null;
         }
         return bridge;
     }
 
     public OvsdbBridgeAugmentation getBridge(Node node) {
-        OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
-        return bridge;
+        return node.getAugmentation(OvsdbBridgeAugmentation.class);
     }
 
     public String getBridgeName(Node node) {
@@ -345,15 +361,18 @@ public class SouthboundImpl implements Southbound {
     }
 
     public String extractBridgeName(Node node) {
-        return (node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue());
+        return node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue();
     }
 
     public OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
+        if (node == null) {
+            return null;
+        }
         return node.getAugmentation(OvsdbBridgeAugmentation.class);
     }
 
     public List<Node> getAllBridgesOnOvsdbNode(Node node) {
-        List<Node> nodes = new ArrayList<Node>();
+        List<Node> nodes = new ArrayList<>();
         List<ManagedNodeEntry> managedNodes = node.getAugmentation(OvsdbNodeAugmentation.class).getManagedNodeEntry();
         for (ManagedNodeEntry managedNode : managedNodes) {
             InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
@@ -365,6 +384,41 @@ public class SouthboundImpl implements Southbound {
         return nodes;
     }
 
+    public boolean isBridgeOnOvsdbNode(Node ovsdbNode, String bridgeName) {
+        boolean found = false;
+        OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
+        if (ovsdbNodeAugmentation != null) {
+            List<ManagedNodeEntry> managedNodes = ovsdbNodeAugmentation.getManagedNodeEntry();
+            if (managedNodes != null) {
+                for (ManagedNodeEntry managedNode : managedNodes) {
+                    InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
+                    if (bridgeIid.toString().contains(bridgeName)) {
+                        found = true;
+                        break;
+                    }
+                }
+            }
+        }
+        return found;
+    }
+
+    public boolean isOvsdbNodeDpdk(Node ovsdbNode) {
+        boolean found = false;
+        OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
+        if (ovsdbNodeAugmentation != null) {
+            List<InterfaceTypeEntry> ifTypes = ovsdbNodeAugmentation.getInterfaceTypeEntry();
+            if (ifTypes != null) {
+                for (InterfaceTypeEntry ifType : ifTypes) {
+                    if (ifType.getInterfaceType().equals(InterfaceTypeDpdk.class)) {
+                        found = true;
+                        break;
+                    }
+                }
+            }
+        }
+        return found;
+    }
+
     public OvsdbNodeAugmentation extractNodeAugmentation(Node node) {
         return node.getAugmentation(OvsdbNodeAugmentation.class);
     }
@@ -372,8 +426,8 @@ public class SouthboundImpl implements Southbound {
     /**
      * Method read ports from bridge node. Method will check if the provided node
      * has the ports details, if not, it will read from Operational data store.
-     * @param node
-     * @return
+     * @param node Target bridge to getch termination points from.
+     * @return List of termination points on the given bridge
      */
     public List<OvsdbTerminationPointAugmentation> getTerminationPointsOfBridge(Node node) {
         List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(node);
@@ -383,7 +437,7 @@ public class SouthboundImpl implements Southbound {
         return tpAugmentations;
     }
 
-    public OvsdbTerminationPointAugmentation getTerminationPointsOfBridge(Node node, String portName) {
+    public OvsdbTerminationPointAugmentation getTerminationPointOfBridge(Node node, String portName) {
         OvsdbTerminationPointAugmentation tpAugmentation = extractTerminationPointAugmentation(node,portName);
         if(tpAugmentation == null){
             List<OvsdbTerminationPointAugmentation> tpAugmentations = readTerminationPointAugmentations(node);
@@ -399,17 +453,11 @@ public class SouthboundImpl implements Southbound {
     }
 
     public OvsdbTerminationPointAugmentation extractTerminationPointAugmentation(Node bridgeNode, String portName) {
-        OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
-        if (ovsdbBridgeAugmentation != null) {
-            List<TerminationPoint> terminationPoints = bridgeNode.getTerminationPoint();
-            if(terminationPoints != null){
-                for(TerminationPoint terminationPoint : terminationPoints) {
-                    OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
-                            terminationPoint.getAugmentation( OvsdbTerminationPointAugmentation.class);
-                    if (ovsdbTerminationPointAugmentation != null
-                            && ovsdbTerminationPointAugmentation.getName().equals(portName)) {
-                        return ovsdbTerminationPointAugmentation;
-                    }
+        if (bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class) != null) {
+            List<OvsdbTerminationPointAugmentation> tpAugmentations = extractTerminationPointAugmentations(bridgeNode);
+            for (OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation : tpAugmentations) {
+                if (ovsdbTerminationPointAugmentation.getName().equals(portName)) {
+                    return ovsdbTerminationPointAugmentation;
                 }
             }
         }
@@ -417,7 +465,7 @@ public class SouthboundImpl implements Southbound {
     }
 
     public List<TerminationPoint> extractTerminationPoints(Node node) {
-        List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();
+        List<TerminationPoint> terminationPoints = new ArrayList<>();
         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
         if (ovsdbBridgeAugmentation != null) {
             terminationPoints.addAll(node.getTerminationPoint());
@@ -426,11 +474,15 @@ public class SouthboundImpl implements Southbound {
     }
 
     public List<OvsdbTerminationPointAugmentation> extractTerminationPointAugmentations( Node node ) {
-        List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<OvsdbTerminationPointAugmentation>();
+        List<OvsdbTerminationPointAugmentation> tpAugmentations = new ArrayList<>();
         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
         if(terminationPoints != null && !terminationPoints.isEmpty()){
             for(TerminationPoint tp : terminationPoints){
-                tpAugmentations.add(tp.getAugmentation(OvsdbTerminationPointAugmentation.class));
+                OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
+                        tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
+                if (ovsdbTerminationPointAugmentation != null) {
+                    tpAugmentations.add(ovsdbTerminationPointAugmentation);
+                }
             }
         }
         return tpAugmentations;
@@ -442,7 +494,7 @@ public class SouthboundImpl implements Southbound {
         if(operNode != null){
             return extractTerminationPointAugmentations(operNode);
         }
-        return new ArrayList<OvsdbTerminationPointAugmentation>();
+        return new ArrayList<>();
     }
 
     public String getInterfaceExternalIdsValue(
@@ -493,7 +545,7 @@ public class SouthboundImpl implements Southbound {
             tpAugmentationBuilder.setInterfaceType(MdsalHelper.OVSDB_INTERFACE_TYPE_MAP.get(type));
         }
 
-        List<Options> optionsList = new ArrayList<Options>();
+        List<Options> optionsList = new ArrayList<>();
         for (Map.Entry<String, String> entry : options.entrySet()) {
             OptionsBuilder optionsBuilder = new OptionsBuilder();
             optionsBuilder.setKey(new OptionsKey(entry.getKey()));
@@ -526,7 +578,7 @@ public class SouthboundImpl implements Southbound {
     }
 
     public Boolean addPatchTerminationPoint(Node node, String bridgeName, String portName, String peerPortName) {
-        Map<String, String> option = new HashMap<String, String>();
+        Map<String, String> option = new HashMap<>();
         option.put("peer", peerPortName);
         return addTerminationPoint(node, bridgeName, portName, PATCH_PORT_TYPE, option);
     }
@@ -548,6 +600,10 @@ public class SouthboundImpl implements Southbound {
             return null;
         case OPENVSWITCH:
             OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
+            if (ovsdbNode == null) {
+                Node nodeFromReadOvsdbNode = readOvsdbNode(node);
+                ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
+            }
             if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
                 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
                     if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
@@ -593,8 +649,9 @@ public class SouthboundImpl implements Southbound {
 
             case OPENVSWITCH:
                 OvsdbNodeAugmentation ovsdbNode = extractNodeAugmentation(node);
-                if (ovsdbNode == null){
-                    ovsdbNode = readOvsdbNode(node);
+                if (ovsdbNode == null) {
+                    Node nodeFromReadOvsdbNode = readOvsdbNode(node);
+                    ovsdbNode = extractNodeAugmentation(nodeFromReadOvsdbNode);
                 }
                 if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
                     for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
@@ -655,8 +712,7 @@ public class SouthboundImpl implements Southbound {
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(MdsalHelper.OVSDB_TOPOLOGY_ID));
 
-        Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
-        return topology;
+        return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
     }
 
     public Long getOFPort(OvsdbTerminationPointAugmentation port) {
@@ -683,4 +739,14 @@ public class SouthboundImpl implements Southbound {
         }
         return ofPort;
     }
+    public OvsdbBridgeAugmentation getBridgeFromConfig(Node node, String bridge) {
+        OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
+        InstanceIdentifier<Node> bridgeIid =
+                MdsalHelper.createInstanceIdentifier(node.getKey(), bridge);
+        Node bridgeNode = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, bridgeIid);
+        if (bridgeNode != null) {
+            ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
+        }
+        return ovsdbBridgeAugmentation;
+    }
 }