Merge "creation of tunnel ingress flow and lfib table entries moved to interface...
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmInternalTunnelDeleteWorker.java
1 /*
2  * Copyright (c) 2015 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.params.xml.ns.yang.itm.op.rev150701.Tunnels;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.op.rev150701.tunnels.DPNTEPsInfo;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.op.rev150701.tunnels.dpn.teps.info.TunnelEndPoints;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.ListenableFuture;
29
30 public class ItmInternalTunnelDeleteWorker {
31    private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
32
33     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
34     {
35         List<ListenableFuture<Void>> futures = new ArrayList<>();
36         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
37         try {
38             if (dpnTepsList == null || dpnTepsList.size() == 0) {
39                 logger.debug("no vtep to delete");
40                 return null ;
41             }
42
43             if (meshedDpnList == null || meshedDpnList.size() == 0) {
44                 logger.debug("No Meshed Vteps");
45                 return null ;
46             }
47             for (DPNTEPsInfo srcDpn : dpnTepsList) {
48                 logger.trace("Processing srcDpn " + srcDpn);
49                 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
50                     logger.trace("Processing srcTep " + srcTep);
51                     String srcTZone = srcTep.getTransportZone();
52
53                     // run through all other DPNS other than srcDpn
54                     for (DPNTEPsInfo dstDpn : meshedDpnList) {
55                         if (!(srcDpn.getDPNID().equals(dstDpn.getDPNID()))) {
56                             for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
57                                 logger.trace("Processing dstTep " + dstTep);
58                                 if (dstTep.getTransportZone().equals(srcTZone)) {
59                                     // remove all trunk interfaces
60                                     logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
61                                     removeTrunkInterface(dataBroker, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
62                                 }
63                             }
64                         }
65                     }
66
67                     // removing vtep / dpn from Tunnels OpDs.
68                     InstanceIdentifier<TunnelEndPoints> tepPath =
69                                     InstanceIdentifier.builder(Tunnels.class).child(DPNTEPsInfo.class, srcDpn.getKey())
70                                                     .child(TunnelEndPoints.class, srcTep.getKey()).build();
71
72                     logger.trace("Tep Removal from DPNTEPSINFO CONFIG DS " + srcTep);
73                     t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
74                     InstanceIdentifier<DPNTEPsInfo> dpnPath =
75                                     InstanceIdentifier.builder(Tunnels.class).child(DPNTEPsInfo.class, srcDpn.getKey())
76                                                     .build();
77                     Optional<DPNTEPsInfo> dpnOptional =
78                                     ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
79                     if (dpnOptional.isPresent()) {
80                         DPNTEPsInfo dpnRead = dpnOptional.get();
81                         // remove dpn if no vteps exist on dpn
82                         if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
83                             logger.debug( "Removing Terminating Service Table Flow ") ;
84                            // setUpOrRemoveTerminatingServiceTable(dpnRead.getDPNID(), false);
85                             logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + dpnRead);
86                             t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
87                             InstanceIdentifier<Tunnels> tnlContainerPath =
88                                             InstanceIdentifier.builder(Tunnels.class).build();
89                             Optional<Tunnels> containerOptional =
90                                             ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
91                                                             tnlContainerPath, dataBroker);
92                             // remove container if no DPNs are present
93                             if (containerOptional.isPresent()) {
94                                 Tunnels tnls = containerOptional.get();
95                                 if (tnls.getDPNTEPsInfo() == null || tnls.getDPNTEPsInfo().isEmpty()) {
96                                     logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
97                                     t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
98                                 }
99                             }
100                         }
101                     }
102                 }
103             }
104             futures.add( t.submit() );
105         } catch (Exception e1) {
106             logger.error("exception while deleting tep", e1);
107         }
108         return futures ;
109     }
110
111     private static void removeTrunkInterface(DataBroker dataBroker, TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
112         WriteTransaction t, List<ListenableFuture<Void>> futures) {
113         String trunkfwdIfName =
114                         ItmUtils.getTrunkInterfaceName(srcTep.getInterfaceName(), srcTep.getIpAddress()
115                                         .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
116                                         .getValue());
117         logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
118         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
119         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS {}, {} ", trunkfwdIfName, trunkIdentifier ) ;
120         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
121         String trunkRevIfName =
122                         ItmUtils.getTrunkInterfaceName(dstTep.getInterfaceName(), dstTep.getIpAddress()
123                                         .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
124                                         .getValue());
125         logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
126         trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
127         logger.debug(  " Removing Trunk Interface Name - {} , Id - {} from Config DS {}, {} ", trunkfwdIfName, trunkIdentifier ) ;
128         t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
129     }
130 }