Bug 5264 : TEP delete does not clean up correctly
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.itm.confighelpers;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.Optional;
33 import com.google.common.util.concurrent.ListenableFuture;
34
35 public class ItmInternalTunnelDeleteWorker {
36    private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
37
38     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,IMdsalApiManager mdsalManager,
39                                                              List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
40     {
41         List<ListenableFuture<Void>> futures = new ArrayList<>();
42         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
43         try {
44             if (dpnTepsList == null || dpnTepsList.size() == 0) {
45                 logger.debug("no vtep to delete");
46                 return null ;
47             }
48
49             if (meshedDpnList == null || meshedDpnList.size() == 0) {
50                 logger.debug("No Meshed Vteps");
51                 return null ;
52             }
53             for (DPNTEPsInfo srcDpn : dpnTepsList) {
54                 logger.trace("Processing srcDpn " + srcDpn);
55                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
56                     logger.trace("Processing srcTep " + srcTep);
57                     String srcTZone = srcTep.getTransportZone();
58
59                     // run through all other DPNS other than srcDpn
60                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
61                         if (!(srcDpn.getDPNID().equals(dstDpn.getDPNID()))) {
62                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
63                                 logger.trace("Processing dstTep " + dstTep);
64                                 if (dstTep.getTransportZone().equals(srcTZone)) {
65                                     if( checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), dataBroker)) {
66                                     // remove all trunk interfaces
67                                     logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
68                                     removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
69                                     }
70                                 }
71                             }
72                         }
73                     }
74
75                     // removing vtep / dpn from Tunnels OpDs.
76                     InstanceIdentifier<TunnelEndPoints> tepPath =
77                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
78                                                     .child(TunnelEndPoints.class, srcTep.getKey()).build();
79
80                     logger.trace("Tep Removal from DPNTEPSINFO CONFIG DS " + srcTep);
81                     t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
82                     InstanceIdentifier<DPNTEPsInfo> dpnPath =
83                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
84                                                     .build();
85                     Optional<DPNTEPsInfo> dpnOptional =
86                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
87                     if (dpnOptional.isPresent()) {
88                         DPNTEPsInfo dpnRead = dpnOptional.get();
89                         // remove dpn if no vteps exist on dpn
90                         if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
91                             logger.debug( "Removing Terminating Service Table Flow ") ;
92                            ItmUtils.setUpOrRemoveTerminatingServiceTable(dpnRead.getDPNID(), mdsalManager,false);
93                             logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + dpnRead);
94                             t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
95                             InstanceIdentifier<DpnEndpoints> tnlContainerPath =
96                                             InstanceIdentifier.builder(DpnEndpoints.class).build();
97                             Optional<DpnEndpoints> containerOptional =
98                                             ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
99                                                             tnlContainerPath, dataBroker);
100                             // remove container if no DPNs are present
101                             if (containerOptional.isPresent()) {
102                                DpnEndpoints deps = containerOptional.get();
103                                 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
104                                     logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
105                                     t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
106                                 }
107                             }
108                         }
109                     }
110                 }
111             }
112             futures.add( t.submit() );
113         } catch (Exception e1) {
114             logger.error("exception while deleting tep", e1);
115         }
116         return futures ;
117     }
118
119     private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
120                                              TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
121                                              WriteTransaction t, List<ListenableFuture<Void>> futures) {
122         String trunkfwdIfName =
123                         ItmUtils.getTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(), srcTep.getIpAddress()
124                                         .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
125                                         .getValue());
126         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
127         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
128         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier ) ;
129         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
130
131         // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
132         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
133                 TunnelList.class)
134                     .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));   
135         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
136         // Release the Ids for the forward trunk interface Name
137         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,srcTep.getInterfaceName(), srcTep.getIpAddress()
138                 .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
139                 .getValue() );
140
141         String trunkRevIfName =
142                         ItmUtils.getTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
143                                         .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
144                                         .getValue());
145         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
146         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
147         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier ) ;
148         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
149
150      // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
151         path = InstanceIdentifier.create(
152                 TunnelList.class)
153                     .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId));   
154         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
155         
156      // Release the Ids for the reverse trunk interface Name
157         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
158                 .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
159                 .getValue());
160     }
161     private static boolean checkIfTrunkExists( BigInteger srcDpnId, BigInteger dstDpnId, DataBroker dataBroker) {
162         boolean existsFlag = false ;
163         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
164                 TunnelList.class)
165                     .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));   
166         Optional<InternalTunnel> internalTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
167         if( internalTunnels.isPresent())
168             existsFlag = true ;
169            return existsFlag ;
170     }
171 }