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