Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmExternalTunnelDeleteWorker.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
14 import com.google.common.base.Optional;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
20 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.TransportZones;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZone;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZoneKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.Subnets;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.DeviceVteps;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.Vteps;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.util.concurrent.ListenableFuture;
43
44 public class ItmExternalTunnelDeleteWorker {
45     private static final Logger logger = LoggerFactory.getLogger(ItmExternalTunnelDeleteWorker.class ) ;
46
47     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService, List<DPNTEPsInfo> dpnTepsList,IpAddress extIp, Class<? extends TunnelTypeBase> tunType ) {
48         logger.trace( " Delete Tunnels towards DC Gateway with Ip  {}", extIp ) ;
49         List<ListenableFuture<Void>> futures = new ArrayList<>();
50         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
51
52             if (dpnTepsList == null || dpnTepsList.size() == 0) {
53                 logger.debug("no vtep to delete");
54                 return null ;
55             }
56             for( DPNTEPsInfo teps : dpnTepsList) {
57                 TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0) ;
58                     String interfaceName = firstEndPt.getInterfaceName() ;
59                     String trunkInterfaceName = ItmUtils.getTrunkInterfaceName( idManagerService, interfaceName,
60                                                                                 firstEndPt.getIpAddress().getIpv4Address().getValue(),
61                                                                                 extIp.getIpv4Address().getValue(),
62                                                                                 tunType.getName());
63                     InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
64                     t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
65                     ItmUtils.itmCache.removeInterface(trunkInterfaceName);
66
67                     InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
68                             ExternalTunnelList.class)
69                                 .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(extIp.toString(),
70                                                                                     teps.getDPNID().toString(),
71                                                                                     tunType));
72                     t.delete(LogicalDatastoreType.CONFIGURATION, path);
73                     logger.debug( "Deleting tunnel towards DC gateway, Tunnel interface name {} ",trunkInterfaceName );
74                     ItmUtils.itmCache.removeExternalTunnel(trunkInterfaceName);
75                     // Release the Ids for the trunk interface Name
76                     ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,interfaceName,firstEndPt.getIpAddress().getIpv4Address().getValue(), extIp.getIpv4Address().getValue(),tunType.getName());
77                 }
78             futures.add(t.submit()) ;
79         return futures ;
80     }
81
82     public static List<ListenableFuture<Void>> deleteHwVtepsTunnels(DataBroker dataBroker, IdManagerService idManagerService, List<DPNTEPsInfo> delDpnList ,List<HwVtep> cfgdHwVteps) {
83         List<ListenableFuture<Void>> futures = new ArrayList<>();
84         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
85
86         if (delDpnList != null || cfgdHwVteps != null)
87             tunnelsDeletion(delDpnList, cfgdHwVteps, idManagerService, futures, t, dataBroker);
88             futures.add(t.submit());
89         return futures;
90     }
91
92     private static void tunnelsDeletion(List<DPNTEPsInfo> cfgdDpnList, List<HwVtep> cfgdhwVteps, IdManagerService idManagerService, List<ListenableFuture<Void>> futures, WriteTransaction t, DataBroker dataBroker) {
93         if (cfgdDpnList != null && !cfgdDpnList.isEmpty()) {
94         for (DPNTEPsInfo dpn : cfgdDpnList) {
95             if (dpn.getTunnelEndPoints() != null && !dpn.getTunnelEndPoints().isEmpty())
96                 for (TunnelEndPoints srcTep : dpn.getTunnelEndPoints()) {
97                         InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class)
98                                         .child(TransportZone.class, new TransportZoneKey((srcTep.getTransportZone())))
99                                         .build();
100                     Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
101                     if (tZoneOptional.isPresent()) {
102                         TransportZone tZone = tZoneOptional.get();
103                         //do we need to check tunnel type?
104                         if (tZone.getSubnets() != null && !tZone.getSubnets().isEmpty()) {
105                             for (Subnets sub : tZone.getSubnets()) {
106                                 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
107                                     for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
108                                         String cssID = dpn.getDPNID().toString();
109                                         //CSS-TOR-CSS
110                                             deleteTrunksCSSTOR(dataBroker, idManagerService, dpn.getDPNID(), srcTep.getInterfaceName(), srcTep.getIpAddress(),
111                                                             hwVtepDS.getTopologyId(), hwVtepDS.getNodeId(), hwVtepDS.getIpAddress(), tZone.getTunnelType(),
112                                                             t, futures);
113                                         }
114                                     }
115                                 }
116                             }
117                             if (cfgdhwVteps != null && !cfgdhwVteps.isEmpty()) {
118                                 for (HwVtep hwVtep : cfgdhwVteps) {
119                                     deleteTrunksCSSTOR(dataBroker, idManagerService, dpn.getDPNID(), srcTep.getInterfaceName(), srcTep.getIpAddress(),
120                                                     hwVtep.getTopo_id(), hwVtep.getNode_id(), hwVtep.getHwIp(),
121                                                     TunnelTypeVxlan.class, t, futures);
122                                 }
123                             }
124                         }
125                     }
126             }
127         }
128             if (cfgdhwVteps != null && !cfgdhwVteps.isEmpty()) {
129                 for (HwVtep hwTep : cfgdhwVteps) {
130                     logger.trace("processing hwTep from list {}", hwTep);
131                     for (HwVtep hwTepRemote : cfgdhwVteps) {
132                         if (!hwTep.getHwIp().equals(hwTepRemote.getHwIp())) {
133                             deleteTrunksTORTOR(dataBroker, idManagerService, hwTep.getTopo_id(), hwTep.getNode_id(),
134                                     hwTep.getHwIp(), hwTepRemote.getTopo_id(), hwTepRemote.getNode_id(),
135                                     hwTepRemote.getHwIp(), TunnelTypeVxlan.class, t, futures);
136                         }
137                     }
138                     InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class)
139                             .child(TransportZone.class, new TransportZoneKey((hwTep.getTransportZone()))).build();
140                     Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
141                     if (tZoneOptional.isPresent()) {
142                         TransportZone tZone = tZoneOptional.get();
143                         logger.trace("subnets under tz {} are {}", tZone.getZoneName(), tZone.getSubnets());
144                         if (tZone.getSubnets() != null && !tZone.getSubnets().isEmpty()) {
145                             for (Subnets sub : tZone.getSubnets()) {
146                                 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
147                                     for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
148                                         logger.trace("hwtepDS exists {}", hwVtepDS);
149                                         if (hwVtepDS.getIpAddress().equals(hwTep.getHwIp()))
150                                             continue;//dont delete tunnels with self
151                                         logger.trace("deleting tor-tor {} and {}", hwTep, hwVtepDS);
152                                         deleteTrunksTORTOR(dataBroker, idManagerService, hwTep.getTopo_id(), hwTep.getNode_id(),
153                                                 hwTep.getHwIp(), hwVtepDS.getTopologyId(), hwVtepDS.getNodeId(),
154                                                 hwVtepDS.getIpAddress(), tZone.getTunnelType(),
155                                                 t, futures);
156
157                                     }
158                                 }
159                                 if (sub.getVteps() != null && !sub.getVteps().isEmpty()) {
160                                     for (Vteps vtep : sub.getVteps()) {
161                                         logger.trace("deleting tor-css-tor {} and {}", hwTep, vtep);
162                                         String parentIf = ItmUtils.getInterfaceName(vtep.getDpnId(), vtep.getPortname(), sub.getVlanId());
163                                         deleteTrunksCSSTOR(dataBroker, idManagerService, vtep.getDpnId(), parentIf, vtep.getIpAddress(),
164                                                 hwTep.getTopo_id(), hwTep.getNode_id(), hwTep.getHwIp(),
165                                                 tZone.getTunnelType(), t, futures);
166                                     }
167                                 }
168                             }
169                         }
170                     }
171                 }
172         }
173     }
174
175
176
177
178
179                 //do we need to check tunnel type?
180
181                                 //do i need to check node-id?
182                                 //for mlag case and non-m-lag case, isnt it enough to just check ipaddress?
183                                 //TOR-TOR
184
185                                 //TOR-CSS
186
187
188
189     private static void deleteTrunksCSSTOR(DataBroker dataBroker, IdManagerService idManagerService, BigInteger dpnid,
190                     String interfaceName, IpAddress cssIpAddress, String topologyId, String nodeId, IpAddress hWIpAddress,
191                                            Class<? extends TunnelTypeBase> tunType, WriteTransaction t,
192                                            List<ListenableFuture<Void>> futures) {
193         //CSS-TOR
194         if (trunkExists(dpnid.toString(), nodeId, tunType, dataBroker)) {
195             logger.trace("deleting tunnel from {} to {} ", dpnid.toString(), nodeId);
196             String parentIf = interfaceName;
197             String fwdTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService,parentIf,cssIpAddress.getIpv4Address().getValue(),
198                             hWIpAddress.getIpv4Address().getValue(), tunType.getName());
199             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(fwdTrunkIf);
200             t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
201
202             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
203                             ExternalTunnelList.class)
204                             .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId, dpnid.toString(), tunType));
205             t.delete(LogicalDatastoreType.CONFIGURATION, path);
206         }
207         else {
208             logger.trace(" trunk from {} to {} already deleted",dpnid.toString(), nodeId);
209         }
210         //TOR-CSS
211         if (trunkExists( nodeId, dpnid.toString(), tunType, dataBroker)) {
212             logger.trace("deleting tunnel from {} to {} ",nodeId, dpnid.toString());
213
214             String parentIf = ItmUtils.getHwParentIf(topologyId,nodeId);
215             String revTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService,parentIf, hWIpAddress.getIpv4Address().getValue(),
216                             cssIpAddress.getIpv4Address().getValue(), tunType.getName());
217             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(revTrunkIf);
218             t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
219
220             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
221                             ExternalTunnelList.class)
222                             .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dpnid.toString(),nodeId, tunType));
223             t.delete(LogicalDatastoreType.CONFIGURATION, path);
224         }
225         else {
226             logger.trace(" trunk from {} to {} already deleted",  nodeId, dpnid.toString());
227         }
228     }
229
230     private static void deleteTrunksTORTOR(DataBroker dataBroker, IdManagerService idManagerService,
231                     String topologyId1, String nodeId1, IpAddress hWIpAddress1, String topologyId2, String nodeId2, IpAddress hWIpAddress2,
232                                            Class<? extends TunnelTypeBase> tunType, WriteTransaction t, List<ListenableFuture<Void>> futures) {
233         //TOR1-TOR2
234         if (trunkExists(nodeId1, nodeId2, tunType, dataBroker)) {
235             logger.trace("deleting tunnel from {} to {} ", nodeId1, nodeId2);
236             String parentIf = ItmUtils.getHwParentIf(topologyId1,nodeId1);
237             String fwdTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService, parentIf,
238                                                                 hWIpAddress1.getIpv4Address().getValue(),
239                                                                 hWIpAddress2.getIpv4Address().getValue(),
240                                                                 tunType.getName());
241             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(fwdTrunkIf);
242             t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
243
244             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
245                             ExternalTunnelList.class)
246                             .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId2, nodeId1, tunType));
247             t.delete(LogicalDatastoreType.CONFIGURATION, path);
248         }
249         else {
250             logger.trace(" trunk from {} to {} already deleted",nodeId1, nodeId2);
251         }
252         //TOR2-TOR1
253         if (trunkExists( nodeId2, nodeId1, tunType, dataBroker)) {
254             logger.trace("deleting tunnel from {} to {} ",nodeId2, nodeId1);
255
256             String parentIf = ItmUtils.getHwParentIf(topologyId2,nodeId2);
257             String revTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService,parentIf, hWIpAddress2.getIpv4Address().getValue(),
258                                                                 hWIpAddress1.getIpv4Address().getValue(), tunType.getName());
259             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(revTrunkIf);
260             t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
261
262             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
263                             ExternalTunnelList.class)
264                             .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId1,nodeId2, tunType));
265             t.delete(LogicalDatastoreType.CONFIGURATION, path);
266         }
267         else {
268             logger.trace(" trunk from {} to {} already deleted",nodeId2, nodeId1);
269         }
270     }
271
272     private static boolean trunkExists( String srcDpnOrNode,  String dstDpnOrNode,
273                                         Class<? extends TunnelTypeBase> tunType,DataBroker dataBroker) {
274         boolean existsFlag = false ;
275         InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
276                         ExternalTunnelList.class)
277                         .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dstDpnOrNode, srcDpnOrNode, tunType));
278         Optional<ExternalTunnel> exTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
279         if( exTunnels.isPresent()) {
280             existsFlag = true ;
281     }
282         return existsFlag ;
283     }
284
285
286 }