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