Merge "Add missing <name> fields for pom.xml files"
[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 java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
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.genius.itm.impl.ItmUtils;
19 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeLldp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembership;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnelKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ItmInternalTunnelDeleteWorker {
38     private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class);
39
40     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,
41             IMdsalApiManager mdsalManager, List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList) {
42         logger.trace("TEPs to be deleted {} ", dpnTepsList);
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
44         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
45         try {
46             if (dpnTepsList == null || dpnTepsList.size() == 0) {
47                 logger.debug("no vtep to delete");
48                 return futures;
49             }
50
51             if (meshedDpnList == null || meshedDpnList.size() == 0) {
52                 logger.debug("No Meshed Vteps");
53                 return futures;
54             }
55             for (DPNTEPsInfo srcDpn : dpnTepsList) {
56                 logger.trace("Processing srcDpn " + srcDpn);
57
58                 List<TunnelEndPoints> meshedEndPtCache = ItmUtils.getTEPsForDpn(srcDpn.getDPNID(), meshedDpnList);
59                 if (meshedEndPtCache == null) {
60                     logger.debug("No Tunnel End Point configured for this DPN {}", srcDpn.getDPNID());
61                     continue;
62                 }
63                 logger.debug( "Entries in meshEndPointCache {} for DPN Id{} ", meshedEndPtCache.size(), srcDpn.getDPNID() );
64                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
65                     logger.trace("Processing srcTep " + srcTep);
66                     List<TzMembership> srcTZones = srcTep.getTzMembership();
67                     boolean tepDeleteFlag = false;
68                     // First, take care of tunnel removal, so run through all other DPNS other than srcDpn
69                     // In the tep received from Delete DCN, the membership list will always be 1 as the DCN is at transport zone level
70                     // Hence if a tunnel is shared across TZs, compare the original membership list between end points to decide if tunnel to be deleted.
71                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
72                         if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
73                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
74                                 if (!ItmUtils.getIntersection(dstTep.getTzMembership(), srcTZones).isEmpty()) {
75                                     List<TzMembership> originalTzMembership = ItmUtils.getOriginalTzMembership(srcTep, srcDpn.getDPNID(), meshedDpnList);
76                                     if (ItmUtils.getIntersection(dstTep.getTzMembership(), originalTzMembership).size() == 1) {
77                                         if (checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), srcTep.getTunnelType(), dataBroker)) {
78                                             // remove all trunk interfaces
79                                             logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} ", srcTep, dstTep);
80                                             removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
81                                         }
82                                     }
83                                 }
84                             }
85                         }
86                     }
87                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
88                         // Second, take care of Tep TZ membership and identify if tep can be removed
89                         if (srcDpn.getDPNID().equals(dstDpn.getDPNID())){
90                             // Same DPN, so remove the TZ membership
91                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
92                                 if (dstTep.getIpAddress().equals(srcTep.getIpAddress())) {
93                                     // Remove the deleted TZ membership from the TEP
94                                     logger.debug("Removing TZ list {} from Existing TZ list {} ", srcTZones, dstTep.getTzMembership());
95                                     List<TzMembership> updatedList = ItmUtils.removeTransportZoneMembership(dstTep, srcTZones);
96                                     if (updatedList.isEmpty()) {
97                                         logger.debug(" This TEP can be deleted " + srcTep);
98                                         tepDeleteFlag = true;
99                                     }else {
100                                         TunnelEndPointsBuilder modifiedTepBld = new TunnelEndPointsBuilder(dstTep);
101                                         modifiedTepBld.setTzMembership(updatedList);
102                                         TunnelEndPoints modifiedTep = modifiedTepBld.build() ;
103                                         InstanceIdentifier<TunnelEndPoints> tepPath =
104                                                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, dstDpn.getKey())
105                                                         .child(TunnelEndPoints.class, dstTep.getKey()).build();
106
107                                         logger.debug(" Store the modified Tep in DS {} ", modifiedTep);
108                                         t.put(LogicalDatastoreType.CONFIGURATION, tepPath, modifiedTep);
109                                     }
110                                 }
111                             }
112                         }
113                     }
114                     if (tepDeleteFlag) {
115                         // Third, removing vtep / dpn from Tunnels OpDs.
116                         InstanceIdentifier<TunnelEndPoints> tepPath =
117                                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
118                                         .child(TunnelEndPoints.class, srcTep.getKey()).build();
119
120                         logger.trace("Tep Removal of TEP {} from DPNTEPSINFO CONFIG DS with Key {} " + srcTep, srcTep.getKey());
121                         t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
122                         // remove the tep from the cache
123                         meshedEndPtCache.remove(srcTep);
124                         Class<? extends TunnelMonitoringTypeBase> monitorProtocol = ItmUtils.determineMonitorProtocol(dataBroker);
125                         InstanceIdentifier<DPNTEPsInfo> dpnPath =
126                                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
127                                         .build();
128
129                         if (meshedEndPtCache.isEmpty()) {
130                             // remove dpn if no vteps exist on dpn
131                             if (monitorProtocol.isAssignableFrom(TunnelMonitoringTypeLldp.class)) {
132                                 logger.debug("Removing Terminating Service Table Flow ");
133                                 ItmUtils.setUpOrRemoveTerminatingServiceTable(srcDpn.getDPNID(), mdsalManager, false);
134                             }
135                             logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + srcDpn.getDPNID());
136                             t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
137                             InstanceIdentifier<DpnEndpoints> tnlContainerPath =
138                                     InstanceIdentifier.builder(DpnEndpoints.class).build();
139                             Optional<DpnEndpoints> containerOptional =
140                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
141                                             tnlContainerPath, dataBroker);
142                             // remove container if no DPNs are present
143                             if (containerOptional.isPresent()) {
144                                 DpnEndpoints deps = containerOptional.get();
145                                 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
146                                     logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
147                                     t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
148                                 }
149                             }
150                         }
151                     }
152                 }
153             }
154             futures.add(t.submit());
155         } catch (Exception e1) {
156             logger.error("exception while deleting tep", e1);
157         }
158         return futures;
159     }
160
161     private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
162             TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
163             WriteTransaction t, List<ListenableFuture<Void>> futures) {
164         String trunkfwdIfName = ItmUtils.getTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(),
165                 new String(srcTep.getIpAddress().getValue()), new String(dstTep.getIpAddress().getValue()),
166                 srcTep.getTunnelType().getName());
167         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
168         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
169         logger.debug(" Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier);
170         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
171         ItmUtils.itmCache.removeInterface(trunkfwdIfName);
172         // also update itm-state ds -- Delete the forward tunnel-interface from
173         // the tunnel list
174         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
175                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, srcTep.getTunnelType()));
176         t.delete(LogicalDatastoreType.CONFIGURATION, path);
177         ItmUtils.itmCache.removeInternalTunnel(trunkfwdIfName);
178         // Release the Ids for the forward trunk interface Name
179         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(),
180                 new String(srcTep.getIpAddress().getValue()), new String(dstTep.getIpAddress().getValue()),
181                 srcTep.getTunnelType().getName());
182
183         String trunkRevIfName = ItmUtils.getTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(),
184                 new String(dstTep.getIpAddress().getValue()), new String(srcTep.getIpAddress().getValue()),
185                 srcTep.getTunnelType().getName());
186         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
187         trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
188         logger.debug(" Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier);
189         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
190
191         // also update itm-state ds -- Delete the reverse tunnel-interface from
192         // the tunnel list
193         path = InstanceIdentifier.create(TunnelList.class).child(InternalTunnel.class,
194                 new InternalTunnelKey(srcDpnId, dstDpnId, dstTep.getTunnelType()));
195         t.delete(LogicalDatastoreType.CONFIGURATION, path);
196
197         // Release the Ids for the reverse trunk interface Name
198         ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(),
199                 new String(dstTep.getIpAddress().getValue()), new String(srcTep.getIpAddress().getValue()),
200                 dstTep.getTunnelType().getName());
201     }
202
203     private static boolean checkIfTrunkExists(BigInteger srcDpnId, BigInteger dstDpnId,
204             Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
205         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
206                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, tunType));
207         return ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker).isPresent();
208     }
209 }