Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
index a6d3d47f418ebed058747341e17da0fe5e402f04..210622f79d190539e3028ff218b7cc3b657459b2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -17,9 +17,15 @@ import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
 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.vpnservice.idmanager.rev150403.IdManagerService;
+import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,22 +36,30 @@ import com.google.common.util.concurrent.ListenableFuture;
 public class ItmInternalTunnelDeleteWorker {
    private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
 
-    public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
+    public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,IMdsalApiManager mdsalManager,
+                                                             List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
     {
+        logger.trace( "TEPs to be deleted {} " , dpnTepsList );
         List<ListenableFuture<Void>> futures = new ArrayList<>();
         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
         try {
             if (dpnTepsList == null || dpnTepsList.size() == 0) {
                 logger.debug("no vtep to delete");
-                return null ;
+                return futures ;
             }
 
             if (meshedDpnList == null || meshedDpnList.size() == 0) {
                 logger.debug("No Meshed Vteps");
-                return null ;
+                return futures ;
             }
             for (DPNTEPsInfo srcDpn : dpnTepsList) {
                 logger.trace("Processing srcDpn " + srcDpn);
+                List<TunnelEndPoints> meshedEndPtCache = new ArrayList<TunnelEndPoints>(ItmUtils.getTEPsForDpn(srcDpn.getDPNID(), meshedDpnList)) ;
+                if(meshedEndPtCache == null ) {
+                    logger.debug("No Tunnel End Point configured for this DPN {}", srcDpn.getDPNID());
+                    continue ;
+                }
+                logger.debug( "Entries in meshEndPointCache {} ", meshedEndPtCache.size() );
                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
                     logger.trace("Processing srcTep " + srcTep);
                     String srcTZone = srcTep.getTransportZone();
@@ -56,9 +70,11 @@ public class ItmInternalTunnelDeleteWorker {
                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
                                 logger.trace("Processing dstTep " + dstTep);
                                 if (dstTep.getTransportZone().equals(srcTZone)) {
+                                    if( checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), srcTep.getTunnelType(),dataBroker)) {
                                     // remove all trunk interfaces
                                     logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
-                                    removeTrunkInterface(dataBroker, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
+                                    removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
+                                    }
                                 }
                             }
                         }
@@ -69,20 +85,26 @@ public class ItmInternalTunnelDeleteWorker {
                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
                                                     .child(TunnelEndPoints.class, srcTep.getKey()).build();
 
-                    logger.trace("Tep Removal from DPNTEPSINFO CONFIG DS " + srcTep);
+                    logger.trace("Tep Removal of TEP {} from DPNTEPSINFO CONFIG DS with Key {} " + srcTep, srcTep.getKey());
                     t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
+                    // remove the tep from the cache
+                    meshedEndPtCache.remove(srcTep) ;
+                    
                     InstanceIdentifier<DPNTEPsInfo> dpnPath =
                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
                                                     .build();
+                    /*
                     Optional<DPNTEPsInfo> dpnOptional =
                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
                     if (dpnOptional.isPresent()) {
-                        DPNTEPsInfo dpnRead = dpnOptional.get();
+                    */
+                    if( meshedEndPtCache.isEmpty()) {
+                        //DPNTEPsInfo dpnRead = dpnOptional.get();
                         // remove dpn if no vteps exist on dpn
-                        if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
+                      //  if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
                             logger.debug( "Removing Terminating Service Table Flow ") ;
-                           // setUpOrRemoveTerminatingServiceTable(dpnRead.getDPNID(), false);
-                            logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + dpnRead);
+                           ItmUtils.setUpOrRemoveTerminatingServiceTable(srcDpn.getDPNID(), mdsalManager,false);
+                            logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + srcDpn.getDPNID());
                             t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
                             InstanceIdentifier<DpnEndpoints> tnlContainerPath =
                                             InstanceIdentifier.builder(DpnEndpoints.class).build();
@@ -91,13 +113,13 @@ public class ItmInternalTunnelDeleteWorker {
                                                             tnlContainerPath, dataBroker);
                             // remove container if no DPNs are present
                             if (containerOptional.isPresent()) {
-                               DpnEndpoints deps = containerOptional.get();
+                               DpnEndpoints deps = containerOptional.get();
                                 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
                                     logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
                                     t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
                                 }
                             }
-                        }
+                        //}
                     }
                 }
             }
@@ -108,23 +130,59 @@ public class ItmInternalTunnelDeleteWorker {
         return futures ;
     }
 
-    private static void removeTrunkInterface(DataBroker dataBroker, TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
-        WriteTransaction t, List<ListenableFuture<Void>> futures) {
+    private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
+                                             TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
+                                             WriteTransaction t, List<ListenableFuture<Void>> futures) {
         String trunkfwdIfName =
-                        ItmUtils.getTrunkInterfaceName(srcTep.getInterfaceName(), srcTep.getIpAddress()
-                                        .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
-                                        .getValue());
+                        ItmUtils.getTrunkInterfaceName( idManagerService, srcTep.getInterfaceName(),
+                                                        srcTep.getIpAddress().getIpv4Address().getValue(),
+                                                        dstTep.getIpAddress().getIpv4Address().getValue(),
+                                                        srcTep.getTunnelType().getName());
         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
-        logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS {}, {} ", trunkfwdIfName, trunkIdentifier ) ;
+        logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier ) ;
         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
+        ItmUtils.itmCache.removeInterface(trunkfwdIfName);
+        // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
+        InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
+                TunnelList.class)
+                    .child(InternalTunnel.class, new InternalTunnelKey( dstDpnId, srcDpnId, srcTep.getTunnelType()));
+        t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
+        ItmUtils.itmCache.removeInternalTunnel(trunkfwdIfName);
+        // Release the Ids for the forward trunk interface Name
+        ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,srcTep.getInterfaceName(), srcTep.getIpAddress()
+                .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
+                .getValue(), srcTep.getTunnelType().getName() );
+
         String trunkRevIfName =
-                        ItmUtils.getTrunkInterfaceName(dstTep.getInterfaceName(), dstTep.getIpAddress()
-                                        .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
-                                        .getValue());
+                        ItmUtils.getTrunkInterfaceName( idManagerService, dstTep.getInterfaceName(),
+                                                        dstTep.getIpAddress().getIpv4Address().getValue(),
+                                                        srcTep.getIpAddress().getIpv4Address().getValue(),
+                                                        srcTep.getTunnelType().getName());
         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
-        trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
-        logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS {}, {} ", trunkfwdIfName, trunkIdentifier ) ;
+        trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
+        logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier ) ;
         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
+
+     // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
+        path = InstanceIdentifier.create(
+                TunnelList.class)
+                    .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, srcTep.getTunnelType()));
+        t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
+        
+     // Release the Ids for the reverse trunk interface Name
+        ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
+                .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
+                .getValue(),dstTep.getTunnelType().getName());
+    }
+    private static boolean checkIfTrunkExists(BigInteger srcDpnId, BigInteger dstDpnId, Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
+        boolean existsFlag = false ;
+        InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
+                TunnelList.class)
+                    .child(InternalTunnel.class, new InternalTunnelKey( dstDpnId, srcDpnId, tunType));
+        Optional<InternalTunnel> internalTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
+        if( internalTunnels.isPresent())
+            existsFlag = true ;
+           return existsFlag ;
     }
 }