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