d73a11a3afa01dd2cf1e0e4f39d3e7d59a6ffff3
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
1 /*
2  * Copyright (c) 2016, 2017 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 java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
22 import org.opendaylight.genius.itm.impl.ItmUtils;
23 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeLldp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeLogicalGroup;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembership;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ItmInternalTunnelDeleteWorker {
44     private static final Logger LOG = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
45
46     @SuppressWarnings("checkstyle:IllegalCatch")
47     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,
48                                                              IMdsalApiManager mdsalManager,
49                                                              List<DPNTEPsInfo> dpnTepsList,
50                                                              List<DPNTEPsInfo> meshedDpnList) {
51         LOG.trace("TEPs to be deleted {} " , dpnTepsList);
52         List<ListenableFuture<Void>> futures = new ArrayList<>();
53         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
54         try {
55             if (dpnTepsList == null || dpnTepsList.size() == 0) {
56                 LOG.debug("no vtep to delete");
57                 return futures ;
58             }
59
60             if (meshedDpnList == null || meshedDpnList.size() == 0) {
61                 LOG.debug("No Meshed Vteps");
62                 return futures ;
63             }
64             for (DPNTEPsInfo srcDpn : dpnTepsList) {
65                 LOG.trace("Processing srcDpn " + srcDpn);
66
67                 List<TunnelEndPoints> meshedEndPtCache = ItmUtils.getTEPsForDpn(srcDpn.getDPNID(), meshedDpnList);
68                 if (meshedEndPtCache == null) {
69                     LOG.debug("No Tunnel End Point configured for this DPN {}", srcDpn.getDPNID());
70                     continue ;
71                 }
72                 LOG.debug("Entries in meshEndPointCache {} for DPN Id{} ",
73                         meshedEndPtCache.size(), srcDpn.getDPNID());
74                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
75                     LOG.trace("Processing srcTep " + srcTep);
76                     List<TzMembership> srcTZones = srcTep.getTzMembership();
77                     boolean tepDeleteFlag = false;
78                     // First, take care of tunnel removal, so run through all other DPNS other than srcDpn
79                     // In the tep received from Delete DCN, the membership list will always be 1
80                     // as the DCN is at transport zone level
81                     // Hence if a tunnel is shared across TZs, compare the original membership list between end points
82                     // to decide if tunnel to be deleted.
83                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
84                         if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
85                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
86                                 if (!ItmUtils.getIntersection(dstTep.getTzMembership(), srcTZones).isEmpty()) {
87                                     List<TzMembership> originalTzMembership =
88                                             ItmUtils.getOriginalTzMembership(srcTep, srcDpn.getDPNID(), meshedDpnList);
89                                     if (ItmUtils.getIntersection(dstTep.getTzMembership(), originalTzMembership).size()
90                                             == 1) {
91                                         if (checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(),
92                                                 srcTep.getTunnelType(), dataBroker)) {
93                                             // remove all trunk interfaces
94                                             LOG.trace("Invoking removeTrunkInterface between source TEP {} , "
95                                                     + "Destination TEP {} ", srcTep, dstTep);
96                                             removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn
97                                                     .getDPNID(), dstDpn.getDPNID(), writeTransaction, futures);
98                                         }
99                                     }
100                                 }
101                             }
102                         }
103                     }
104                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
105                         // Second, take care of Tep TZ membership and identify if tep can be removed
106                         if (srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
107                             // Same DPN, so remove the TZ membership
108                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
109                                 if (dstTep.getIpAddress().equals(srcTep.getIpAddress())) {
110                                     // Remove the deleted TZ membership from the TEP
111                                     LOG.debug("Removing TZ list {} from Existing TZ list {} ",
112                                             srcTZones, dstTep.getTzMembership());
113                                     List<TzMembership> updatedList =
114                                             ItmUtils.removeTransportZoneMembership(dstTep, srcTZones);
115                                     if (updatedList.isEmpty()) {
116                                         LOG.debug(" This TEP can be deleted " + srcTep);
117                                         tepDeleteFlag = true;
118                                     } else {
119                                         TunnelEndPointsBuilder modifiedTepBld = new TunnelEndPointsBuilder(dstTep);
120                                         modifiedTepBld.setTzMembership(updatedList);
121                                         TunnelEndPoints modifiedTep = modifiedTepBld.build() ;
122                                         InstanceIdentifier<TunnelEndPoints> tepPath = InstanceIdentifier
123                                                 .builder(DpnEndpoints.class)
124                                                 .child(DPNTEPsInfo.class, dstDpn.getKey())
125                                                 .child(TunnelEndPoints.class, dstTep.getKey()).build();
126
127                                         LOG.debug(" Store the modified Tep in DS {} ", modifiedTep);
128                                         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, tepPath, modifiedTep);
129                                     }
130                                 }
131                             }
132                         }
133                     }
134                     if (tepDeleteFlag) {
135                         // Third, removing vtep / dpn from Tunnels OpDs.
136                         InstanceIdentifier<TunnelEndPoints> tepPath =
137                                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
138                                         .child(TunnelEndPoints.class, srcTep.getKey()).build();
139
140                         LOG.trace("Tep Removal of TEP {} from DPNTEPSINFO CONFIG DS with Key {} "
141                                 + srcTep, srcTep.getKey());
142                         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
143                         // remove the tep from the cache
144                         meshedEndPtCache.remove(srcTep);
145                         Class<? extends TunnelMonitoringTypeBase> monitorProtocol =
146                                 ItmUtils.determineMonitorProtocol(dataBroker);
147                         InstanceIdentifier<DPNTEPsInfo> dpnPath =
148                                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
149                                         .build();
150
151                         if (meshedEndPtCache.isEmpty()) {
152                             // remove dpn if no vteps exist on dpn
153                             if (monitorProtocol.isAssignableFrom(TunnelMonitoringTypeLldp.class)) {
154                                 LOG.debug("Removing Terminating Service Table Flow ");
155                                 ItmUtils.setUpOrRemoveTerminatingServiceTable(srcDpn.getDPNID(), mdsalManager, false);
156                             }
157                             LOG.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + srcDpn.getDPNID());
158                             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
159                             InstanceIdentifier<DpnEndpoints> tnlContainerPath =
160                                     InstanceIdentifier.builder(DpnEndpoints.class).build();
161                             Optional<DpnEndpoints> containerOptional =
162                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
163                                             tnlContainerPath, dataBroker);
164                             // remove container if no DPNs are present
165                             if (containerOptional.isPresent()) {
166                                 DpnEndpoints deps = containerOptional.get();
167                                 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
168                                     LOG.trace("Container Removal from DPNTEPSINFO CONFIG DS");
169                                     writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
170                                 }
171                             }
172                         }
173                     }
174                 }
175             }
176             futures.add(writeTransaction.submit());
177         } catch (Exception e1) {
178             LOG.error("exception while deleting tep", e1);
179         }
180         return futures ;
181     }
182
183     private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
184                                              TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId,
185                                              BigInteger dstDpnId, WriteTransaction transaction,
186                                              List<ListenableFuture<Void>> futures) {
187         String trunkfwdIfName =
188                 ItmUtils.getTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(),
189                         new String(srcTep.getIpAddress().getValue()),
190                         new String(dstTep.getIpAddress().getValue()),
191                         srcTep.getTunnelType().getName());
192         LOG.trace("Removing forward Trunk Interface " + trunkfwdIfName);
193         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
194         LOG.debug(" Removing Trunk Interface Name - {} , Id - {} from Config DS ",
195                 trunkfwdIfName, trunkIdentifier) ;
196         transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
197         ItmUtils.itmCache.removeInterface(trunkfwdIfName);
198         // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
199         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
200                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, srcTep.getTunnelType()));
201         transaction.delete(LogicalDatastoreType.CONFIGURATION,path) ;
202         ItmUtils.itmCache.removeInternalTunnel(trunkfwdIfName);
203         // Release the Ids for the forward trunk interface Name
204         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,srcTep.getInterfaceName(),
205                 new String(srcTep.getIpAddress().getValue()),
206                 new String(dstTep.getIpAddress().getValue()),
207                 srcTep.getTunnelType().getName());
208         removeLogicalGroupTunnel(srcDpnId, dstDpnId, dataBroker);
209
210         String trunkRevIfName = ItmUtils.getTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(),
211                         new String(dstTep.getIpAddress().getValue()),
212                         new String(srcTep.getIpAddress().getValue()),
213                         srcTep.getTunnelType().getName());
214         LOG.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
215         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
216         LOG.debug(" Removing Trunk Interface Name - {} , Id - {} from Config DS ",
217                 trunkRevIfName, trunkIdentifier) ;
218         transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
219
220         // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
221         path = InstanceIdentifier.create(TunnelList.class)
222                 .child(InternalTunnel.class, new InternalTunnelKey(srcDpnId, dstDpnId, dstTep.getTunnelType()));
223         transaction.delete(LogicalDatastoreType.CONFIGURATION,path) ;
224
225         // Release the Ids for the reverse trunk interface Name
226         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(),
227                 new String(dstTep.getIpAddress().getValue()),
228                 new String(srcTep.getIpAddress().getValue()),
229                 dstTep.getTunnelType().getName());
230         removeLogicalGroupTunnel(dstDpnId, srcDpnId, dataBroker);
231
232     }
233
234     private static boolean checkIfTrunkExists(BigInteger srcDpnId, BigInteger dstDpnId,
235                                               Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
236         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
237                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, tunType));
238         return ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker).isPresent();
239     }
240
241     private static void removeLogicalGroupTunnel(BigInteger srcDpnId, BigInteger dstDpnId,
242                                                  DataBroker dataBroker) {
243         boolean tunnelAggregationEnabled = ItmTunnelAggregationHelper.isTunnelAggregationEnabled();
244         if (!tunnelAggregationEnabled) {
245             return;
246         }
247         String logicTunnelName = ItmUtils.getLogicalTunnelGroupName(srcDpnId, dstDpnId);
248         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
249         ItmTunnelAggregationDeleteWorker addWorker =
250                 new ItmTunnelAggregationDeleteWorker(logicTunnelName, srcDpnId, dstDpnId, dataBroker);
251         coordinator.enqueueJob(logicTunnelName, addWorker);
252     }
253
254     private static class ItmTunnelAggregationDeleteWorker implements Callable<List<ListenableFuture<Void>>> {
255
256         private final String logicTunnelName;
257         private final BigInteger srcDpnId;
258         private final BigInteger dstDpnId;
259         private final DataBroker dataBroker;
260
261         ItmTunnelAggregationDeleteWorker(String groupName, BigInteger srcDpnId, BigInteger dstDpnId, DataBroker db) {
262             this.logicTunnelName = groupName;
263             this.srcDpnId = srcDpnId;
264             this.dstDpnId = dstDpnId;
265             this.dataBroker = db;
266         }
267
268         @Override
269         public List<ListenableFuture<Void>> call() throws Exception {
270             List<ListenableFuture<Void>> futures = new ArrayList<>();
271             Collection<InternalTunnel> tunnels = ItmUtils.itmCache.getAllInternalTunnel();
272             if (tunnels == null) {
273                 return futures;
274             }
275             //The logical tunnel interface be removed only when the last tunnel interface on each OVS is deleted
276             boolean emptyTunnelGroup = true;
277             boolean foundLogicGroupIface = false;
278             for (InternalTunnel tunl : tunnels) {
279                 if (tunl.getSourceDPN().equals(srcDpnId) && tunl.getDestinationDPN().equals(dstDpnId)) {
280                     if (tunl.getTransportType().isAssignableFrom(TunnelTypeVxlan.class)
281                             && tunl.getTunnelInterfaceNames() != null && !tunl.getTunnelInterfaceNames().isEmpty()) {
282                         emptyTunnelGroup = false;
283                         break;
284                     } else if (tunl.getTransportType().isAssignableFrom(TunnelTypeLogicalGroup.class)) {
285                         foundLogicGroupIface = true;
286                     }
287                 }
288             }
289             if (emptyTunnelGroup && foundLogicGroupIface) {
290                 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
291                 LOG.debug("MULTIPLE_VxLAN_TUNNELS: remove the logical tunnel group {} because a last tunnel"
292                     + " interface on srcDpnId {} dstDpnId {} is removed", logicTunnelName, srcDpnId, dstDpnId);
293                 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(logicTunnelName);
294                 tx.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
295                 ItmUtils.itmCache.removeInterface(logicTunnelName);
296                 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
297                         .child(InternalTunnel.class,
298                                 new InternalTunnelKey(dstDpnId, srcDpnId, TunnelTypeLogicalGroup.class));
299                 tx.delete(LogicalDatastoreType.CONFIGURATION, path);
300                 ItmUtils.itmCache.removeInternalTunnel(logicTunnelName);
301                 futures.add(tx.submit());
302             } else if (!emptyTunnelGroup) {
303                 LOG.debug("MULTIPLE_VxLAN_TUNNELS: not last tunnel in logical tunnel group {}", logicTunnelName);
304             }
305             return futures;
306         }
307     }
308 }