16135b5f1124d821ecf6742d97e27794b0ce29e6
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
1 /*
2  * Copyright (c) 2016 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.genius.itm.confighelpers;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.itm.impl.ItmUtils;
16 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.math.BigInteger;
31 import java.util.ArrayList;
32 import java.util.List;
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,IMdsalApiManager mdsalManager,
38                                                              List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
39     {
40         logger.trace( "TEPs to be deleted {} " , dpnTepsList );
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 futures ;
47             }
48
49             if (meshedDpnList == null || meshedDpnList.size() == 0) {
50                 logger.debug("No Meshed Vteps");
51                 return futures ;
52             }
53             for (DPNTEPsInfo srcDpn : dpnTepsList) {
54                 logger.trace("Processing srcDpn " + srcDpn);
55                 List<TunnelEndPoints> meshedEndPtCache = new ArrayList<TunnelEndPoints>(ItmUtils.getTEPsForDpn(srcDpn.getDPNID(), meshedDpnList)) ;
56                 if(meshedEndPtCache == null ) {
57                     logger.debug("No Tunnel End Point configured for this DPN {}", srcDpn.getDPNID());
58                     continue ;
59                 }
60                 logger.debug( "Entries in meshEndPointCache {} ", meshedEndPtCache.size() );
61                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
62                     logger.trace("Processing srcTep " + srcTep);
63                     String srcTZone = srcTep.getTransportZone();
64
65                     // run through all other DPNS other than srcDpn
66                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
67                         if (!(srcDpn.getDPNID().equals(dstDpn.getDPNID()))) {
68                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
69                                 logger.trace("Processing dstTep " + dstTep);
70                                 if (dstTep.getTransportZone().equals(srcTZone)) {
71                                     if( checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), srcTep.getTunnelType(),dataBroker)) {
72                                         // remove all trunk interfaces
73                                         logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
74                                         removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
75                                     }
76                                 }
77                             }
78                         }
79                     }
80
81                     // removing vtep / dpn from Tunnels OpDs.
82                     InstanceIdentifier<TunnelEndPoints> tepPath =
83                             InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
84                                     .child(TunnelEndPoints.class, srcTep.getKey()).build();
85
86                     logger.trace("Tep Removal of TEP {} from DPNTEPSINFO CONFIG DS with Key {} " + srcTep, srcTep.getKey());
87                     t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
88                     // remove the tep from the cache
89                     meshedEndPtCache.remove(srcTep) ;
90
91                     InstanceIdentifier<DPNTEPsInfo> dpnPath =
92                             InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
93                                     .build();
94                     /*
95                     Optional<DPNTEPsInfo> dpnOptional =
96                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
97                     if (dpnOptional.isPresent()) {
98                     */
99                     if( meshedEndPtCache.isEmpty()) {
100                         //DPNTEPsInfo dpnRead = dpnOptional.get();
101                         // remove dpn if no vteps exist on dpn
102                         //  if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
103                         logger.debug( "Removing Terminating Service Table Flow ") ;
104                         ItmUtils.setUpOrRemoveTerminatingServiceTable(srcDpn.getDPNID(), mdsalManager,false);
105                         logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + srcDpn.getDPNID());
106                         t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
107                         InstanceIdentifier<DpnEndpoints> tnlContainerPath =
108                                 InstanceIdentifier.builder(DpnEndpoints.class).build();
109                         Optional<DpnEndpoints> containerOptional =
110                                 ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
111                                         tnlContainerPath, dataBroker);
112                         // remove container if no DPNs are present
113                         if (containerOptional.isPresent()) {
114                             DpnEndpoints deps = containerOptional.get();
115                             if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
116                                 logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
117                                 t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
118                             }
119                         }
120                         //}
121                     }
122                 }
123             }
124             futures.add( t.submit() );
125         } catch (Exception e1) {
126             logger.error("exception while deleting tep", e1);
127         }
128         return futures ;
129     }
130
131     private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
132                                              TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
133                                              WriteTransaction t, List<ListenableFuture<Void>> futures) {
134         String trunkfwdIfName =
135                 ItmUtils.getTrunkInterfaceName( idManagerService, srcTep.getInterfaceName(),
136                         srcTep.getIpAddress().getIpv4Address().getValue(),
137                         dstTep.getIpAddress().getIpv4Address().getValue(),
138                         srcTep.getTunnelType().getName());
139         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
140         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
141         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier ) ;
142         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
143         ItmUtils.itmCache.removeInterface(trunkfwdIfName);
144         // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
145         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
146                 TunnelList.class)
147                 .child(InternalTunnel.class, new InternalTunnelKey( dstDpnId, srcDpnId, srcTep.getTunnelType()));
148         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
149         ItmUtils.itmCache.removeInternalTunnel(trunkfwdIfName);
150         // Release the Ids for the forward trunk interface Name
151         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,srcTep.getInterfaceName(), srcTep.getIpAddress()
152                 .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
153                 .getValue(), srcTep.getTunnelType().getName() );
154
155         String trunkRevIfName =
156                 ItmUtils.getTrunkInterfaceName( idManagerService, dstTep.getInterfaceName(),
157                         dstTep.getIpAddress().getIpv4Address().getValue(),
158                         srcTep.getIpAddress().getIpv4Address().getValue(),
159                         srcTep.getTunnelType().getName());
160         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
161         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
162         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier ) ;
163         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
164
165         // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
166         path = InstanceIdentifier.create(
167                 TunnelList.class)
168                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, srcTep.getTunnelType()));
169         t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
170
171         // Release the Ids for the reverse trunk interface Name
172         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
173                 .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
174                 .getValue(),dstTep.getTunnelType().getName());
175     }
176     private static boolean checkIfTrunkExists(BigInteger srcDpnId, BigInteger dstDpnId, Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
177         boolean existsFlag = false ;
178         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
179                 TunnelList.class)
180                 .child(InternalTunnel.class, new InternalTunnelKey( dstDpnId, srcDpnId, tunType));
181         Optional<InternalTunnel> internalTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
182         if( internalTunnels.isPresent())
183             existsFlag = true ;
184         return existsFlag ;
185     }
186 }