Merge "JUnit test case for ITM"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / commons / InterfaceMetaUtils.java
index 56285a6d40756c384c1d3f8b59ae96cd3e3cacc9..7cbe678ad4031617e6d7da3df76a52683889bf73 100644 (file)
@@ -14,6 +14,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.interfacemanager.IfmConstants;
 import org.opendaylight.genius.interfacemanager.IfmUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge._interface.info.BridgeEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
 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.OvsdbBridgeRef;
@@ -45,6 +46,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.math.BigInteger;
+import java.util.List;
 
 public class InterfaceMetaUtils {
     private static final Logger LOG = LoggerFactory.getLogger(InterfaceMetaUtils.class);
@@ -65,8 +67,26 @@ public class InterfaceMetaUtils {
         return bridgeRefEntryOptional.get();
     }
 
-    public static BridgeRefEntry getBridgeReferenceForInterface(Interface interfaceInfo,
+    public static OvsdbBridgeRef getBridgeRefEntryFromOperDS(BigInteger dpId,
                                                              DataBroker dataBroker) {
+        BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
+        InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
+                InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
+        BridgeRefEntry bridgeRefEntry = getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
+        if(bridgeRefEntry == null){
+            // bridge ref entry will be null if the bridge is disconnected from controller.
+            // In that case, fetch bridge reference from bridge interface entry config DS
+            BridgeEntry bridgeEntry = getBridgeEntryFromConfigDS(dpId, dataBroker);
+            if(bridgeEntry == null){
+                return null;
+            }
+            return  bridgeEntry.getBridgeReference();
+        }
+        return bridgeRefEntry.getBridgeReference();
+    }
+
+    public static BridgeRefEntry getBridgeReferenceForInterface(Interface interfaceInfo,
+                                                                DataBroker dataBroker) {
         ParentRefs parentRefs = interfaceInfo.getAugmentation(ParentRefs.class);
         BigInteger dpn = parentRefs.getDatapathNodeIdentifier();
         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpn);
@@ -76,7 +96,7 @@ public class InterfaceMetaUtils {
     }
 
     public static boolean bridgeExists(BridgeRefEntry bridgeRefEntry,
-                                                                DataBroker dataBroker) {
+                                       DataBroker dataBroker) {
         if (bridgeRefEntry != null && bridgeRefEntry.getBridgeReference() != null) {
             InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
                     (InstanceIdentifier<OvsdbBridgeAugmentation>) bridgeRefEntry.getBridgeReference().getValue();
@@ -101,7 +121,7 @@ public class InterfaceMetaUtils {
                 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
         LOG.debug("Trying to retrieve bridge entry from config for Id: {}", bridgeEntryInstanceIdentifier);
         return getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
-                        dataBroker);
+                dataBroker);
     }
 
     public static BridgeEntry getBridgeEntryFromConfigDS(InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier,
@@ -117,26 +137,16 @@ public class InterfaceMetaUtils {
     public static InstanceIdentifier<BridgeInterfaceEntry> getBridgeInterfaceEntryIdentifier(BridgeEntryKey bridgeEntryKey,
                                                                                              BridgeInterfaceEntryKey bridgeInterfaceEntryKey) {
         return InstanceIdentifier.builder(BridgeInterfaceInfo.class)
-                        .child(BridgeEntry.class, bridgeEntryKey)
-                        .child(BridgeInterfaceEntry.class, bridgeInterfaceEntryKey).build();
+                .child(BridgeEntry.class, bridgeEntryKey)
+                .child(BridgeInterfaceEntry.class, bridgeInterfaceEntryKey).build();
 
     }
 
-    public static BridgeInterfaceEntry getBridgeInterfaceEntryFromConfigDS(
-            InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryInstanceIdentifier, DataBroker dataBroker) {
-        Optional<BridgeInterfaceEntry> bridgeInterfaceEntryOptional =
-                IfmUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryInstanceIdentifier, dataBroker);
-        if (!bridgeInterfaceEntryOptional.isPresent()) {
-            return null;
-        }
-        return bridgeInterfaceEntryOptional.get();
-    }
-
-
-    public static void createBridgeInterfaceEntryInConfigDS(BridgeEntryKey bridgeEntryKey,
-                                                             BridgeInterfaceEntryKey bridgeInterfaceEntryKey,
-                                                             String childInterface,
-                                                             WriteTransaction t) {
+    public static void createBridgeInterfaceEntryInConfigDS(BigInteger dpId,
+                                                            String childInterface,
+                                                            WriteTransaction t) {
+        BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
+        BridgeInterfaceEntryKey bridgeInterfaceEntryKey = new BridgeInterfaceEntryKey(childInterface);
         InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
                 InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey, bridgeInterfaceEntryKey);
         BridgeInterfaceEntryBuilder entryBuilder = new BridgeInterfaceEntryBuilder().setKey(bridgeInterfaceEntryKey)
@@ -161,6 +171,14 @@ public class InterfaceMetaUtils {
         return intfIdBuilder.build();
     }
 
+    public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
+            String interfaceName, DataBroker dataBroker) {
+        InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceName);
+        InterfaceParentEntry interfaceParentEntry =
+                InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
+        return interfaceParentEntry;
+    }
+
     public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
             InterfaceParentEntryKey interfaceParentEntryKey, DataBroker dataBroker) {
         InstanceIdentifier<InterfaceParentEntry> intfParentIid =
@@ -205,14 +223,21 @@ public class InterfaceMetaUtils {
         t.put(LogicalDatastoreType.OPERATIONAL, id, ifIndexInterface, true);
     }
 
-    public static void removeLportTagInterfaceMap(WriteTransaction t, IdManagerService idManager, DataBroker broker, String infName, Integer ifIndex) {
-        InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
-        Optional<IfIndexInterface> ifIndexesInterface = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
-        if(ifIndexesInterface.isPresent()) {
-           LOG.debug("removing lport tag to interface map for {}",infName);
-           t.delete(LogicalDatastoreType.OPERATIONAL, id);
-        }
+    public static void removeLportTagInterfaceMap(IdManagerService idManager, WriteTransaction t, String infName) {
+        // workaround to get the id to remove from lport tag interface map
+        Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, infName);
         IfmUtil.releaseId(idManager, IfmConstants.IFM_IDPOOL_NAME, infName);
+        LOG.debug("removing lport tag to interface map for {}",infName);
+        InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
+        t.delete(LogicalDatastoreType.OPERATIONAL, id);
+    }
+
+    public static void addBridgeRefToBridgeInterfaceEntry(BigInteger dpId, OvsdbBridgeRef ovsdbBridgeRef, WriteTransaction t){
+        BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
+        InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier = getBridgeEntryIdentifier(bridgeEntryKey);
+
+        BridgeEntryBuilder bridgeEntryBuilder = new BridgeEntryBuilder().setKey(bridgeEntryKey).setBridgeReference(ovsdbBridgeRef);
+        t.merge(LogicalDatastoreType.CONFIGURATION, bridgeEntryInstanceIdentifier, bridgeEntryBuilder.build(), true);
     }
 
     public static void createBridgeRefEntry(BigInteger dpnId, InstanceIdentifier<?> bridgeIid,
@@ -249,12 +274,12 @@ public class InterfaceMetaUtils {
 
     }
 
-    public static void createTunnelToInterfaceMap(String infName, InstanceIdentifier<Node> nodeId,
+    public static void createTunnelToInterfaceMap(String infName,InstanceIdentifier<Node> nodeId,
                                                   WriteTransaction transaction,
                                                   IfTunnel ifTunnel){
         InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier = org.opendaylight.genius.interfacemanager.renderer.hwvtep.utilities.SouthboundUtils.
                 createTunnelsInstanceIdentifier(nodeId,
-                ifTunnel.getTunnelSource(), ifTunnel.getTunnelDestination());
+                        ifTunnel.getTunnelSource(), ifTunnel.getTunnelDestination());
         createTunnelToInterfaceMap(tunnelsInstanceIdentifier.toString(), infName, transaction);
     }
 
@@ -278,4 +303,18 @@ public class InterfaceMetaUtils {
         return null;
     }
 
+    public static void deleteBridgeInterfaceEntry(BridgeEntryKey bridgeEntryKey, List<BridgeInterfaceEntry> bridgeInterfaceEntries,
+                                                  InstanceIdentifier<BridgeEntry> bridgeEntryIid,
+                                                  WriteTransaction transaction, String interfaceName){
+        BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
+                new BridgeInterfaceEntryKey(interfaceName);
+        InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
+                InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
+                        bridgeInterfaceEntryKey);
+        transaction.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
+
+        if (bridgeInterfaceEntries.size() <= 1) {
+            transaction.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
+        }
+    }
 }
\ No newline at end of file