Bump version odlparent->6.0.0,mdsal->5.0.3
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
index bb514bd6725355a4dc67768ec4b07de0f9223ec8..273f3696a18e7eefcb407fca40621132c5633daf 100644 (file)
@@ -12,10 +12,10 @@ import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.math.BigInteger;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -28,6 +28,7 @@ import org.opendaylight.genius.infra.TypedReadWriteTransaction;
 import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.itm.cache.DpnTepStateCache;
+import org.opendaylight.genius.itm.cache.OfEndPointCache;
 import org.opendaylight.genius.itm.cache.OvsBridgeEntryCache;
 import org.opendaylight.genius.itm.cache.OvsBridgeRefEntryCache;
 import org.opendaylight.genius.itm.cache.TunnelStateCache;
@@ -47,6 +48,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeLogicalGroup;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.meta.rev171210.bridge.tunnel.info.OvsBridgeEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.meta.rev171210.bridge.tunnel.info.OvsBridgeEntryKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.meta.rev171210.bridge.tunnel.info.ovs.bridge.entry.OvsBridgeTunnelEntry;
@@ -67,6 +69,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -84,6 +87,8 @@ public class ItmInternalTunnelDeleteWorker {
     private final OvsBridgeRefEntryCache ovsBridgeRefEntryCache;
     private final TunnelStateCache tunnelStateCache;
     private final DirectTunnelUtils directTunnelUtils;
+    private final OfEndPointCache ofEndPointCache;
+    private final ItmConfig itmConfig;
 
     public ItmInternalTunnelDeleteWorker(DataBroker dataBroker, JobCoordinator jobCoordinator,
                                          TunnelMonitoringConfig tunnelMonitoringConfig,
@@ -91,7 +96,9 @@ public class ItmInternalTunnelDeleteWorker {
                                          OvsBridgeEntryCache ovsBridgeEntryCache,
                                          OvsBridgeRefEntryCache ovsBridgeRefEntryCache,
                                          TunnelStateCache tunnelStateCache,
-                                         DirectTunnelUtils directTunnelUtils) {
+                                         DirectTunnelUtils directTunnelUtils,
+                                         OfEndPointCache ofEndPointCache,
+                                         ItmConfig itmConfig) {
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.jobCoordinator = jobCoordinator;
@@ -102,6 +109,8 @@ public class ItmInternalTunnelDeleteWorker {
         this.ovsBridgeRefEntryCache = ovsBridgeRefEntryCache;
         this.tunnelStateCache = tunnelStateCache;
         this.directTunnelUtils = directTunnelUtils;
+        this.ofEndPointCache = ofEndPointCache;
+        this.itmConfig = itmConfig;
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -127,9 +136,9 @@ public class ItmInternalTunnelDeleteWorker {
                     continue;
                 }
                 LOG.debug("Entries in meshEndPointCache {} for DPN Id{} ", meshedEndPtCache.size(), srcDpn.getDPNID());
-                for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
+                for (TunnelEndPoints srcTep : srcDpn.nonnullTunnelEndPoints()) {
                     LOG.trace("Processing srcTep {}", srcTep);
-                    List<TzMembership> srcTZones = srcTep.getTzMembership();
+                    List<TzMembership> srcTZones = srcTep.nonnullTzMembership();
                     boolean tepDeleteFlag = false;
                     // First, take care of tunnel removal, so run through all other DPNS other than srcDpn
                     // In the tep received from Delete DCN, the membership list will always be 1
@@ -137,9 +146,9 @@ public class ItmInternalTunnelDeleteWorker {
                     // Hence if a tunnel is shared across TZs, compare the original membership list between end points
                     // to decide if tunnel to be deleted.
                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
-                        if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
-                            for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
-                                if (!ItmUtils.getIntersection(dstTep.getTzMembership(), srcTZones).isEmpty()) {
+                        if (!Objects.equals(srcDpn.getDPNID(), dstDpn.getDPNID())) {
+                            for (TunnelEndPoints dstTep : dstDpn.nonnullTunnelEndPoints()) {
+                                if (!ItmUtils.getIntersection(dstTep.nonnullTzMembership(), srcTZones).isEmpty()) {
                                     List<TzMembership> originalTzMembership =
                                             ItmUtils.getOriginalTzMembership(srcTep, srcDpn.getDPNID(), meshedDpnList);
                                     if (ItmUtils.getIntersection(dstTep.getTzMembership(),
@@ -156,7 +165,7 @@ public class ItmInternalTunnelDeleteWorker {
 
                                         } else {
                                             if (checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(),
-                                                    srcTep.getTunnelType(), dataBroker)) {
+                                                srcTep.getTunnelType(), dataBroker)) {
                                                 // remove all trunk interfaces
                                                 LOG.trace("Invoking removeTrunkInterface between source TEP {} , "
                                                         + "Destination TEP {} ", srcTep, dstTep);
@@ -169,12 +178,13 @@ public class ItmInternalTunnelDeleteWorker {
                             }
                         }
                     }
+
                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
                         // Second, take care of Tep TZ membership and identify if tep can be removed
-                        if (srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
+                        if (Objects.equals(srcDpn.getDPNID(), dstDpn.getDPNID())) {
                             // Same DPN, so remove the TZ membership
-                            for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
-                                if (dstTep.getIpAddress().equals(srcTep.getIpAddress())) {
+                            for (TunnelEndPoints dstTep : dstDpn.nonnullTunnelEndPoints()) {
+                                if (Objects.equals(dstTep.getIpAddress(), srcTep.getIpAddress())) {
                                     // Remove the deleted TZ membership from the TEP
                                     LOG.debug("Removing TZ list {} from Existing TZ list {} ", srcTZones,
                                             dstTep.getTzMembership());
@@ -250,10 +260,10 @@ public class ItmInternalTunnelDeleteWorker {
     }
 
     private void removeTrunkInterface(TypedWriteTransaction<Configuration> tx, TunnelEndPoints srcTep,
-        TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId) {
+            TunnelEndPoints dstTep, Uint64 srcDpnId, Uint64 dstDpnId) {
         String trunkfwdIfName = ItmUtils.getTrunkInterfaceName(srcTep.getInterfaceName(),
-                new String(srcTep.getIpAddress().getValue()),
-                new String(dstTep.getIpAddress().getValue()),
+                srcTep.getIpAddress().stringValue(),
+                dstTep.getIpAddress().stringValue(),
                 srcTep.getTunnelType().getName());
         LOG.trace("Removing forward Trunk Interface {}" , trunkfwdIfName);
         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
@@ -268,14 +278,14 @@ public class ItmInternalTunnelDeleteWorker {
         ItmUtils.ITM_CACHE.removeInternalTunnel(trunkfwdIfName);
         // Release the Ids for the forward trunk interface Name
         ItmUtils.releaseIdForTrunkInterfaceName(srcTep.getInterfaceName(),
-                new String(srcTep.getIpAddress().getValue()),
-                new String(dstTep.getIpAddress().getValue()),
+                srcTep.getIpAddress().stringValue(),
+                dstTep.getIpAddress().stringValue(),
                 srcTep.getTunnelType().getName());
         removeLogicalGroupTunnel(srcDpnId, dstDpnId);
 
         String trunkRevIfName = ItmUtils.getTrunkInterfaceName(dstTep.getInterfaceName(),
-                new String(dstTep.getIpAddress().getValue()),
-                new String(srcTep.getIpAddress().getValue()),
+                dstTep.getIpAddress().stringValue(),
+                srcTep.getIpAddress().stringValue(),
                 srcTep.getTunnelType().getName());
         LOG.trace("Removing Reverse Trunk Interface {}", trunkRevIfName);
         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
@@ -290,20 +300,20 @@ public class ItmInternalTunnelDeleteWorker {
 
         // Release the Ids for the reverse trunk interface Name
         ItmUtils.releaseIdForTrunkInterfaceName(dstTep.getInterfaceName(),
-                new String(dstTep.getIpAddress().getValue()),
-                new String(srcTep.getIpAddress().getValue()),
+                dstTep.getIpAddress().stringValue(),
+                srcTep.getIpAddress().stringValue(),
                 dstTep.getTunnelType().getName());
         removeLogicalGroupTunnel(dstDpnId, srcDpnId);
     }
 
-    private static boolean checkIfTrunkExists(BigInteger srcDpnId, BigInteger dstDpnId,
+    private static boolean checkIfTrunkExists(Uint64 srcDpnId, Uint64 dstDpnId,
                                               Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, tunType));
         return ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker).isPresent();
     }
 
-    private void removeLogicalGroupTunnel(BigInteger srcDpnId, BigInteger dstDpnId) {
+    private void removeLogicalGroupTunnel(Uint64 srcDpnId, Uint64 dstDpnId) {
         boolean tunnelAggregationEnabled = ItmTunnelAggregationHelper.isTunnelAggregationEnabled();
         if (!tunnelAggregationEnabled) {
             return;
@@ -317,11 +327,11 @@ public class ItmInternalTunnelDeleteWorker {
     private static class ItmTunnelAggregationDeleteWorker implements Callable<List<ListenableFuture<Void>>> {
 
         private final String logicTunnelName;
-        private final BigInteger srcDpnId;
-        private final BigInteger dstDpnId;
+        private final Uint64 srcDpnId;
+        private final Uint64 dstDpnId;
         private final ManagedNewTransactionRunner txRunner;
 
-        ItmTunnelAggregationDeleteWorker(String groupName, BigInteger srcDpnId, BigInteger dstDpnId, DataBroker db) {
+        ItmTunnelAggregationDeleteWorker(String groupName, Uint64 srcDpnId, Uint64 dstDpnId, DataBroker db) {
             this.logicTunnelName = groupName;
             this.srcDpnId = srcDpnId;
             this.dstDpnId = dstDpnId;
@@ -336,12 +346,15 @@ public class ItmInternalTunnelDeleteWorker {
             boolean emptyTunnelGroup = true;
             boolean foundLogicGroupIface = false;
             for (InternalTunnel tunl : tunnels) {
-                if (tunl.getSourceDPN().equals(srcDpnId) && tunl.getDestinationDPN().equals(dstDpnId)) {
-                    if (tunl.getTransportType().isAssignableFrom(TunnelTypeVxlan.class)
+                if (Objects.equals(tunl.getSourceDPN(), srcDpnId) && Objects.equals(tunl.getDestinationDPN(),
+                        dstDpnId)) {
+                    if (tunl.getTransportType() != null && tunl.getTransportType().isAssignableFrom(
+                            TunnelTypeVxlan.class)
                             && tunl.getTunnelInterfaceNames() != null && !tunl.getTunnelInterfaceNames().isEmpty()) {
                         emptyTunnelGroup = false;
                         break;
-                    } else if (tunl.getTransportType().isAssignableFrom(TunnelTypeLogicalGroup.class)) {
+                    } else if (tunl.getTransportType() != null && tunl.getTransportType().isAssignableFrom(
+                            TunnelTypeLogicalGroup.class)) {
                         foundLogicGroupIface = true;
                     }
                 }
@@ -367,7 +380,7 @@ public class ItmInternalTunnelDeleteWorker {
     }
 
     private void removeTunnelInterfaceFromOvsdb(TypedReadWriteTransaction<Configuration> tx, TunnelEndPoints srcTep,
-        TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId) {
+        TunnelEndPoints dstTep, Uint64 srcDpnId, Uint64 dstDpnId) {
         String trunkfwdIfName = ItmUtils.getTrunkInterfaceName(srcTep.getInterfaceName(),
                 srcTep.getIpAddress().getIpv4Address().getValue(),
                 dstTep.getIpAddress().getIpv4Address().getValue(),
@@ -400,8 +413,8 @@ public class ItmInternalTunnelDeleteWorker {
         }
     }
 
-    private boolean checkIfTepInterfaceExists(BigInteger srcDpnId, BigInteger dstDpnId) {
-        DpnTepInterfaceInfo dpnTepInterfaceInfo = dpnTepStateCache.getDpnTepInterface(srcDpnId,dstDpnId);
+    private boolean checkIfTepInterfaceExists(Uint64 srcDpnId, Uint64 dstDpnId) {
+        DpnTepInterfaceInfo dpnTepInterfaceInfo = dpnTepStateCache.getDpnTepInterface(srcDpnId, dstDpnId);
         if (dpnTepInterfaceInfo != null) {
             return dpnTepInterfaceInfo.getTunnelName() != null;
         }
@@ -409,7 +422,7 @@ public class ItmInternalTunnelDeleteWorker {
     }
 
     private void removeConfiguration(TypedReadWriteTransaction<Configuration> tx, Interface interfaceOld,
-        ParentRefs parentRefs) throws ExecutionException, InterruptedException, OperationFailedException {
+            ParentRefs parentRefs) throws ExecutionException, InterruptedException, OperationFailedException {
         IfTunnel ifTunnel = interfaceOld.augmentation(IfTunnel.class);
         if (ifTunnel != null) {
             // Check if the same transaction can be used across Config and operational shards
@@ -422,7 +435,7 @@ public class ItmInternalTunnelDeleteWorker {
             throws ExecutionException, InterruptedException, OperationFailedException {
 
         LOG.info("removing tunnel configuration for {}", interfaceName);
-        BigInteger dpId = null;
+        Uint64 dpId = null;
         if (parentRefs != null) {
             dpId = parentRefs.getDatapathNodeIdentifier();
         }
@@ -431,6 +444,40 @@ public class ItmInternalTunnelDeleteWorker {
             return;
         }
 
+        OvsdbBridgeRef ovsdbBridgeRef = getOvsdbBridgeRef(dpId);
+        Optional<OvsBridgeEntry> ovsBridgeEntryOptional = ovsBridgeEntryCache.get(dpId);
+
+        // delete bridge to tunnel interface mappings
+        OvsBridgeEntryKey bridgeEntryKey = new OvsBridgeEntryKey(dpId);
+        InstanceIdentifier<OvsBridgeEntry> bridgeEntryIid =
+                DirectTunnelUtils.getOvsBridgeEntryIdentifier(bridgeEntryKey);
+
+
+        if (ovsBridgeEntryOptional.isPresent()) {
+            List<OvsBridgeTunnelEntry> bridgeTunnelEntries = ovsBridgeEntryOptional.get().nonnullOvsBridgeTunnelEntry();
+
+            if (ovsdbBridgeRef != null) {
+                if (!itmConfig.isUseOfTunnels()) {
+                    removeTerminationEndPoint(ovsdbBridgeRef.getValue(), interfaceName);
+                } else if (bridgeTunnelEntries.size() <= 1) {
+                    removeTerminationEndPoint(ovsdbBridgeRef.getValue(), ofEndPointCache.get(dpId));
+                    ofEndPointCache.remove(dpId);
+                }
+            }
+
+            deleteBridgeInterfaceEntry(bridgeEntryKey, bridgeTunnelEntries, bridgeEntryIid, interfaceName);
+            // IfIndex needs to be removed only during State Clean up not Config
+        }
+
+        directTunnelUtils.deleteTunnelStateEntry(interfaceName);
+        // delete tunnel ingress flow
+        removeTunnelIngressFlow(tx, interfaceName, dpId);
+        directTunnelUtils.removeTunnelEgressFlow(tx, dpId, interfaceName);
+        cleanUpInterfaceWithUnknownState(interfaceName, parentRefs, ifTunnel);
+        directTunnelUtils.removeLportTagInterfaceMap(interfaceName);
+    }
+
+    private OvsdbBridgeRef getOvsdbBridgeRef(Uint64 dpId) throws ReadFailedException {
         Optional<OvsBridgeRefEntry> ovsBridgeRefEntryOptional = ovsBridgeRefEntryCache.get(dpId);
         Optional<OvsBridgeEntry> ovsBridgeEntryOptional;
         OvsdbBridgeRef ovsdbBridgeRef = null;
@@ -442,28 +489,7 @@ public class ItmInternalTunnelDeleteWorker {
                 ovsdbBridgeRef = ovsBridgeEntryOptional.get().getOvsBridgeReference();
             }
         }
-
-        if (ovsdbBridgeRef != null) {
-            removeTerminationEndPoint(ovsdbBridgeRef.getValue(), interfaceName);
-        }
-
-        // delete tunnel ingress flow
-        removeTunnelIngressFlow(tx, interfaceName, dpId);
-
-        // delete bridge to tunnel interface mappings
-        OvsBridgeEntryKey bridgeEntryKey = new OvsBridgeEntryKey(dpId);
-        InstanceIdentifier<OvsBridgeEntry> bridgeEntryIid =
-                DirectTunnelUtils.getOvsBridgeEntryIdentifier(bridgeEntryKey);
-
-        ovsBridgeEntryOptional = ovsBridgeEntryCache.get(dpId);
-        if (ovsBridgeEntryOptional.isPresent()) {
-            List<OvsBridgeTunnelEntry> bridgeTunnelEntries = ovsBridgeEntryOptional.get().getOvsBridgeTunnelEntry();
-            deleteBridgeInterfaceEntry(bridgeEntryKey, bridgeTunnelEntries, bridgeEntryIid, interfaceName);
-            // IfIndex needs to be removed only during State Clean up not Config
-            // TunnelMetaUtils.removeLportTagInterfaceMap(idManager, defaultOperationalShardTransaction, interfaceName);
-            cleanUpInterfaceWithUnknownState(interfaceName, parentRefs, ifTunnel);
-            directTunnelUtils.removeLportTagInterfaceMap(interfaceName);
-        }
+        return ovsdbBridgeRef;
     }
 
     private void removeTerminationEndPoint(InstanceIdentifier<?> bridgeIid, String interfaceName) {
@@ -474,7 +500,7 @@ public class ItmInternalTunnelDeleteWorker {
     }
 
     private void removeTunnelIngressFlow(TypedReadWriteTransaction<Configuration> tx, String interfaceName,
-        BigInteger dpId) throws ExecutionException, InterruptedException {
+            Uint64 dpId) throws ExecutionException, InterruptedException {
         directTunnelUtils.removeTunnelIngressFlow(tx, dpId, interfaceName);
     }