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