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.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.ListenableFuture;
33
34 public class ItmInternalTunnelDeleteWorker {
35    private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
36
37     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,
38                                                              List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
39     {
40         List<ListenableFuture<Void>> futures = new ArrayList<>();
41         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
42         try {
43             if (dpnTepsList == null || dpnTepsList.size() == 0) {
44                 logger.debug("no vtep to delete");
45                 return null ;
46             }
47
48             if (meshedDpnList == null || meshedDpnList.size() == 0) {
49                 logger.debug("No Meshed Vteps");
50                 return null ;
51             }
52             for (DPNTEPsInfo srcDpn : dpnTepsList) {
53                 logger.trace("Processing srcDpn " + srcDpn);
54                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
55                     logger.trace("Processing srcTep " + srcTep);
56                     String srcTZone = srcTep.getTransportZone();
57
58                     // run through all other DPNS other than srcDpn
59                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
60                         if (!(srcDpn.getDPNID().equals(dstDpn.getDPNID()))) {
61                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
62                                 logger.trace("Processing dstTep " + dstTep);
63                                 if (dstTep.getTransportZone().equals(srcTZone)) {
64                                     if( checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), dataBroker)) {
65                                     // remove all trunk interfaces
66                                     logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
67                                     removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
68                                     }
69                                 }
70                             }
71                         }
72                     }
73
74                     // removing vtep / dpn from Tunnels OpDs.
75                     InstanceIdentifier<TunnelEndPoints> tepPath =
76                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
77                                                     .child(TunnelEndPoints.class, srcTep.getKey()).build();
78
79                     logger.trace("Tep Removal from DPNTEPSINFO CONFIG DS " + srcTep);
80                     t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
81                     InstanceIdentifier<DPNTEPsInfo> dpnPath =
82                                     InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
83                                                     .build();
84                     Optional<DPNTEPsInfo> dpnOptional =
85                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
86                     if (dpnOptional.isPresent()) {
87                         DPNTEPsInfo dpnRead = dpnOptional.get();
88                         // remove dpn if no vteps exist on dpn
89                         if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
90                             logger.debug( "Removing Terminating Service Table Flow ") ;
91                            // setUpOrRemoveTerminatingServiceTable(dpnRead.getDPNID(), false);
92                             logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + dpnRead);
93                             t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
94                             InstanceIdentifier<DpnEndpoints> tnlContainerPath =
95                                             InstanceIdentifier.builder(DpnEndpoints.class).build();
96                             Optional<DpnEndpoints> containerOptional =
97                                             ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
98                                                             tnlContainerPath, dataBroker);
99                             // remove container if no DPNs are present
100                             if (containerOptional.isPresent()) {
101                                DpnEndpoints deps = containerOptional.get();
102                                 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
103                                     logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
104                                     t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
105                                 }
106                             }
107                         }
108                     }
109                 }
110             }
111             futures.add( t.submit() );
112         } catch (Exception e1) {
113             logger.error("exception while deleting tep", e1);
114         }
115         return futures ;
116     }
117
118     private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
119                                              TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
120                                              WriteTransaction t, List<ListenableFuture<Void>> futures) {
121         String trunkfwdIfName =
122                         ItmUtils.getTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(), srcTep.getIpAddress()
123                                         .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
124                                         .getValue());
125         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
126         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
127         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier ) ;
128         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
129
130         // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
131         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
132                 TunnelList.class)
133                     .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));   
134         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
135         // Release the Id for the forward trunk
136         ItmUtils.releaseId(idManagerService, trunkfwdIfName);
137
138         String trunkRevIfName =
139                         ItmUtils.getTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
140                                         .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
141                                         .getValue());
142         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
143         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
144         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier ) ;
145         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
146
147      // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
148         path = InstanceIdentifier.create(
149                 TunnelList.class)
150                     .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId));   
151         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
152         
153         // Release the Id for the Reverse trunk
154         ItmUtils.releaseId(idManagerService, trunkRevIfName);
155     }
156     private static boolean checkIfTrunkExists( BigInteger srcDpnId, BigInteger dstDpnId, DataBroker dataBroker) {
157         boolean existsFlag = false ;
158         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
159                 TunnelList.class)
160                     .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));   
161         Optional<InternalTunnel> internalTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
162         if( internalTunnels.isPresent())
163             existsFlag = true ;
164            return existsFlag ;
165     }
166 }