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