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