Merge "Add GPE option to ITM config"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmExternalTunnelDeleteWorker.java
1 /*
2  * Copyright (c) 2016, 2017 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
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.TunnelTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembership;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.DeviceVteps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ItmExternalTunnelDeleteWorker {
40     private static final Logger LOG = LoggerFactory.getLogger(ItmExternalTunnelDeleteWorker.class) ;
41
42     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,
43                                                              List<DPNTEPsInfo> dpnTepsList,
44                                                              List<DPNTEPsInfo> meshedDpnList, IpAddress extIp,
45                                                              Class<? extends TunnelTypeBase> tunType) {
46         LOG.trace(" Delete Tunnels towards DC Gateway with Ip  {}", extIp) ;
47         List<ListenableFuture<Void>> futures = new ArrayList<>();
48         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
49
50         if (dpnTepsList == null || dpnTepsList.size() == 0) {
51             LOG.debug("no vtep to delete");
52             return null ;
53         }
54         for (DPNTEPsInfo teps : dpnTepsList) {
55             TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0);
56             // The membership in the listener will always be 1, to get the actual membership read from the DS
57             List<TzMembership> originalTzMembership =
58                     ItmUtils.getOriginalTzMembership(firstEndPt,teps.getDPNID(),meshedDpnList);
59             if (originalTzMembership.size() == 1) {
60                 String interfaceName = firstEndPt.getInterfaceName();
61                 String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName,
62                         new String(firstEndPt.getIpAddress().getValue()),
63                         new String(extIp.getValue()),
64                         tunType.getName());
65                 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
66                 transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
67                 ItmUtils.itmCache.removeInterface(trunkInterfaceName);
68
69                 InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
70                         ExternalTunnelList.class)
71                         .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(extIp.toString(),
72                                 teps.getDPNID().toString(),
73                                 tunType));
74                 transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
75                 LOG.debug("Deleting tunnel towards DC gateway, Tunnel interface name {} ", trunkInterfaceName);
76                 ItmUtils.itmCache.removeExternalTunnel(trunkInterfaceName);
77                 // Release the Ids for the trunk interface Name
78                 ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, interfaceName,
79                     new String(firstEndPt.getIpAddress().getValue()),
80                     new String(extIp.getValue()), tunType.getName());
81             }
82         }
83         futures.add(transaction.submit()) ;
84         return futures ;
85     }
86
87     public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,
88                                                              List<DPNTEPsInfo> dpnTepsList, IpAddress extIp,
89                                                              Class<? extends TunnelTypeBase> tunType) {
90         LOG.trace(" Delete Tunnels towards DC Gateway with Ip  {}", extIp) ;
91         List<ListenableFuture<Void>> futures = new ArrayList<>();
92         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
93
94         if (dpnTepsList == null || dpnTepsList.size() == 0) {
95             LOG.debug("no vtep to delete");
96             return null ;
97         }
98         for (DPNTEPsInfo teps : dpnTepsList) {
99             TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0);
100             String interfaceName = firstEndPt.getInterfaceName();
101             String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName,
102                         new String(firstEndPt.getIpAddress().getValue()),
103                         new String(extIp.getValue()),
104                         tunType.getName());
105             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
106             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
107             ItmUtils.itmCache.removeInterface(trunkInterfaceName);
108
109             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(ExternalTunnelList.class)
110                         .child(ExternalTunnel.class,
111                                 ItmUtils.getExternalTunnelKey(extIp.toString(), teps.getDPNID().toString(), tunType));
112             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, path);
113             LOG.debug("Deleting tunnel towards DC gateway, Tunnel interface name {} ", trunkInterfaceName);
114             ItmUtils.itmCache.removeExternalTunnel(trunkInterfaceName);
115             // Release the Ids for the trunk interface Name
116             ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, interfaceName,
117                     new String(firstEndPt.getIpAddress().getValue()),
118                     new String(extIp.getValue()),
119                     tunType.getName());
120         }
121         futures.add(writeTransaction.submit()) ;
122         return futures ;
123     }
124
125     public static List<ListenableFuture<Void>> deleteHwVtepsTunnels(DataBroker dataBroker,
126             IdManagerService idManagerService, List<DPNTEPsInfo> delDpnList, List<HwVtep> cfgdHwVteps,
127             TransportZone originalTZone) {
128         List<ListenableFuture<Void>> futures = new ArrayList<>();
129         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
130
131         if (delDpnList != null || cfgdHwVteps != null) {
132             tunnelsDeletion(delDpnList, cfgdHwVteps, originalTZone, idManagerService, futures,
133                     writeTransaction, dataBroker);
134         }
135         futures.add(writeTransaction.submit());
136         return futures;
137     }
138
139     private static void tunnelsDeletion(List<DPNTEPsInfo> cfgdDpnList, List<HwVtep> cfgdhwVteps,
140             TransportZone originalTZone, IdManagerService idManagerService, List<ListenableFuture<Void>> futures,
141             WriteTransaction writeTransaction, DataBroker dataBroker) {
142         if (cfgdDpnList != null) {
143             for (DPNTEPsInfo dpn : cfgdDpnList) {
144                 if (dpn.getTunnelEndPoints() != null) {
145                     for (TunnelEndPoints srcTep : dpn.getTunnelEndPoints()) {
146                         for (TzMembership zone: srcTep.getTzMembership()) {
147                             deleteTunnelsInTransportZone(zone.getZoneName(), dpn, srcTep, cfgdhwVteps, dataBroker,
148                                     idManagerService, writeTransaction, futures);
149                         }
150                     }
151                 }
152             }
153         }
154
155         if (cfgdhwVteps != null && !cfgdhwVteps.isEmpty()) {
156             for (HwVtep hwTep : cfgdhwVteps) {
157                 LOG.trace("processing hwTep from list {}", hwTep);
158                 for (HwVtep hwTepRemote : cfgdhwVteps) {
159                     if (!hwTep.getHwIp().equals(hwTepRemote.getHwIp())) {
160                         deleteTrunksTorTor(dataBroker, idManagerService, hwTep.getTopoId(), hwTep.getNodeId(),
161                                 hwTep.getHwIp(), hwTepRemote.getTopoId(), hwTepRemote.getNodeId(),
162                                 hwTepRemote.getHwIp(), TunnelTypeVxlan.class, writeTransaction, futures);
163                     }
164                 }
165                 //do we need to check tunnel type?
166                 LOG.trace("subnets under tz {} are {}", originalTZone.getZoneName(), originalTZone.getSubnets());
167                 if (originalTZone.getSubnets() != null && !originalTZone.getSubnets().isEmpty()) {
168
169                     for (Subnets sub : originalTZone.getSubnets()) {
170                         if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
171                             for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
172                                 LOG.trace("hwtepDS exists {}", hwVtepDS);
173                                 //do i need to check node-id?
174                                 //for mlag case and non-m-lag case, isnt it enough to just check ipaddress?
175                                 if (hwVtepDS.getIpAddress().equals(hwTep.getHwIp())) {
176                                     continue;//dont delete tunnels with self
177                                 }
178                                 //TOR-TOR
179                                 LOG.trace("deleting tor-tor {} and {}", hwTep, hwVtepDS);
180                                 deleteTrunksTorTor(dataBroker, idManagerService, hwTep.getTopoId(), hwTep.getNodeId(),
181                                         hwTep.getHwIp(), hwVtepDS.getTopologyId(), hwVtepDS.getNodeId(),
182                                         hwVtepDS.getIpAddress(), originalTZone.getTunnelType(),
183                                         writeTransaction, futures);
184
185                             }
186                         }
187                         if (sub.getVteps() != null && !sub.getVteps().isEmpty()) {
188                             for (Vteps vtep : sub.getVteps()) {
189                                 //TOR-OVS
190                                 LOG.trace("deleting tor-css-tor {} and {}", hwTep, vtep);
191                                 String parentIf =
192                                         ItmUtils.getInterfaceName(vtep.getDpnId(), vtep.getPortname(), sub.getVlanId());
193                                 deleteTrunksOvsTor(dataBroker, idManagerService, vtep.getDpnId(), parentIf,
194                                         vtep.getIpAddress(), hwTep.getTopoId(), hwTep.getNodeId(), hwTep.getHwIp(),
195                                         originalTZone.getTunnelType(), writeTransaction, futures);
196                             }
197                         }
198                     }
199                 }
200             }
201         }
202     }
203
204     private static void deleteTunnelsInTransportZone(String zoneName, DPNTEPsInfo dpn, TunnelEndPoints srcTep,
205                                                      List<HwVtep> cfgdhwVteps, DataBroker dataBroker,
206                                                      IdManagerService idManagerService,
207                                                      WriteTransaction writeTransaction,
208                                                      List<ListenableFuture<Void>> futures) {
209         InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class)
210                 .child(TransportZone.class, new TransportZoneKey(zoneName))
211                 .build();
212         Optional<TransportZone> transportZoneOptional =
213                 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
214         if (transportZoneOptional.isPresent()) {
215             TransportZone tzone = transportZoneOptional.get();
216             //do we need to check tunnel type?
217             if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
218                 for (Subnets sub : tzone.getSubnets()) {
219                     if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
220                         for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
221                             String cssID = dpn.getDPNID().toString();
222                             //OVS-TOR-OVS
223                             deleteTrunksOvsTor(dataBroker, idManagerService, dpn.getDPNID(), srcTep.getInterfaceName(),
224                                     srcTep.getIpAddress(), hwVtepDS.getTopologyId(), hwVtepDS.getNodeId(),
225                                     hwVtepDS.getIpAddress(), tzone.getTunnelType(), writeTransaction, futures);
226
227                         }
228                     }
229                 }
230             }
231             if (cfgdhwVteps != null && !cfgdhwVteps.isEmpty()) {
232                 for (HwVtep hwVtep : cfgdhwVteps) {
233                     deleteTrunksOvsTor(dataBroker, idManagerService, dpn.getDPNID(), srcTep.getInterfaceName(),
234                             srcTep.getIpAddress(), hwVtep.getTopoId(), hwVtep.getNodeId(), hwVtep.getHwIp(),
235                             TunnelTypeVxlan.class, writeTransaction, futures);
236
237                 }
238             }
239         }
240     }
241
242     private static void deleteTrunksOvsTor(DataBroker dataBroker, IdManagerService idManagerService, BigInteger dpnid,
243                                            String interfaceName, IpAddress cssIpAddress, String topologyId,
244                                            String nodeId, IpAddress hwIpAddress,
245                                            Class<? extends TunnelTypeBase> tunType, WriteTransaction transaction,
246                                            List<ListenableFuture<Void>> futures) {
247         //OVS-TOR
248         if (trunkExists(dpnid.toString(), nodeId, tunType, dataBroker)) {
249             LOG.trace("deleting tunnel from {} to {} ", dpnid.toString(), nodeId);
250             String parentIf = interfaceName;
251             String fwdTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService, parentIf,
252                     new String(cssIpAddress.getValue()), new String(hwIpAddress.getValue()),
253                     tunType.getName());
254             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(fwdTrunkIf);
255             transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
256
257             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(ExternalTunnelList.class)
258                     .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId, dpnid.toString(), tunType));
259             transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
260         } else {
261             LOG.trace(" trunk from {} to {} already deleted",dpnid.toString(), nodeId);
262         }
263         //TOR-OVS
264         if (trunkExists(nodeId, dpnid.toString(), tunType, dataBroker)) {
265             LOG.trace("deleting tunnel from {} to {} ",nodeId, dpnid.toString());
266
267             String parentIf = ItmUtils.getHwParentIf(topologyId,nodeId);
268             String revTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService,parentIf,
269                     new String(hwIpAddress.getValue()), new String(cssIpAddress.getValue()),
270                     tunType.getName());
271             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(revTrunkIf);
272             transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
273
274             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(ExternalTunnelList.class)
275                     .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dpnid.toString(),nodeId, tunType));
276             transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
277         } else {
278             LOG.trace(" trunk from {} to {} already deleted",  nodeId, dpnid.toString());
279         }
280     }
281
282     private static void deleteTrunksTorTor(DataBroker dataBroker, IdManagerService idManagerService,
283                                            String topologyId1, String nodeId1, IpAddress hwIpAddress1,
284                                            String topologyId2, String nodeId2, IpAddress hwIpAddress2,
285                                            Class<? extends TunnelTypeBase> tunType, WriteTransaction transaction,
286                                            List<ListenableFuture<Void>> futures) {
287         //TOR1-TOR2
288         if (trunkExists(nodeId1, nodeId2, tunType, dataBroker)) {
289             LOG.trace("deleting tunnel from {} to {} ", nodeId1, nodeId2);
290             String parentIf = ItmUtils.getHwParentIf(topologyId1,nodeId1);
291             String fwdTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService, parentIf,
292                     new String(hwIpAddress1.getValue()),
293                     new String(hwIpAddress2.getValue()),
294                     tunType.getName());
295             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(fwdTrunkIf);
296             transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
297
298             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(ExternalTunnelList.class)
299                     .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId2, nodeId1, tunType));
300             transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
301         } else {
302             LOG.trace(" trunk from {} to {} already deleted",nodeId1, nodeId2);
303         }
304         //TOR2-TOR1
305         if (trunkExists(nodeId2, nodeId1, tunType, dataBroker)) {
306             LOG.trace("deleting tunnel from {} to {} ",nodeId2, nodeId1);
307
308             String parentIf = ItmUtils.getHwParentIf(topologyId2,nodeId2);
309             String revTrunkIf = ItmUtils.getTrunkInterfaceName(idManagerService,parentIf,
310                     new String(hwIpAddress2.getValue()), new String(hwIpAddress1.getValue()),
311                     tunType.getName());
312             InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(revTrunkIf);
313             transaction.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
314
315             InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
316                     ExternalTunnelList.class)
317                     .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(nodeId1,nodeId2, tunType));
318             transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
319         } else {
320             LOG.trace(" trunk from {} to {} already deleted",nodeId2, nodeId1);
321         }
322     }
323
324     private static boolean trunkExists(String srcDpnOrNode, String dstDpnOrNode,
325                                        Class<? extends TunnelTypeBase> tunType, DataBroker dataBroker) {
326         InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(ExternalTunnelList.class)
327                 .child(ExternalTunnel.class, ItmUtils.getExternalTunnelKey(dstDpnOrNode, srcDpnOrNode, tunType));
328         return ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker).isPresent();
329     }
330 }