2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.itm.confighelpers;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Callable;
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.vpnservice.itm.impl.ItmUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
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.tunnel.list.InternalTunnel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.Optional;
33 import com.google.common.util.concurrent.ListenableFuture;
35 public class ItmInternalTunnelDeleteWorker {
36 private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelDeleteWorker.class) ;
38 public static List<ListenableFuture<Void>> deleteTunnels(DataBroker dataBroker, IdManagerService idManagerService,IMdsalApiManager mdsalManager,
39 List<DPNTEPsInfo> dpnTepsList, List<DPNTEPsInfo> meshedDpnList)
41 List<ListenableFuture<Void>> futures = new ArrayList<>();
42 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
44 if (dpnTepsList == null || dpnTepsList.size() == 0) {
45 logger.debug("no vtep to delete");
49 if (meshedDpnList == null || meshedDpnList.size() == 0) {
50 logger.debug("No Meshed Vteps");
53 for (DPNTEPsInfo srcDpn : dpnTepsList) {
54 logger.trace("Processing srcDpn " + srcDpn);
55 for (TunnelEndPoints srcTep : srcDpn.getTunnelEndPoints()) {
56 logger.trace("Processing srcTep " + srcTep);
57 String srcTZone = srcTep.getTransportZone();
59 // run through all other DPNS other than srcDpn
60 for (DPNTEPsInfo dstDpn : meshedDpnList) {
61 if (!(srcDpn.getDPNID().equals(dstDpn.getDPNID()))) {
62 for (TunnelEndPoints dstTep : dstDpn.getTunnelEndPoints()) {
63 logger.trace("Processing dstTep " + dstTep);
64 if (dstTep.getTransportZone().equals(srcTZone)) {
65 if( checkIfTrunkExists(dstDpn.getDPNID(), srcDpn.getDPNID(), dataBroker)) {
66 // remove all trunk interfaces
67 logger.trace("Invoking removeTrunkInterface between source TEP {} , Destination TEP {} " ,srcTep , dstTep);
68 removeTrunkInterface(dataBroker, idManagerService, srcTep, dstTep, srcDpn.getDPNID(), dstDpn.getDPNID(), t, futures);
75 // removing vtep / dpn from Tunnels OpDs.
76 InstanceIdentifier<TunnelEndPoints> tepPath =
77 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
78 .child(TunnelEndPoints.class, srcTep.getKey()).build();
80 logger.trace("Tep Removal from DPNTEPSINFO CONFIG DS " + srcTep);
81 t.delete(LogicalDatastoreType.CONFIGURATION, tepPath);
82 InstanceIdentifier<DPNTEPsInfo> dpnPath =
83 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, srcDpn.getKey())
85 Optional<DPNTEPsInfo> dpnOptional =
86 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, dpnPath, dataBroker);
87 if (dpnOptional.isPresent()) {
88 DPNTEPsInfo dpnRead = dpnOptional.get();
89 // remove dpn if no vteps exist on dpn
90 if (dpnRead.getTunnelEndPoints() == null || dpnRead.getTunnelEndPoints().size() == 0) {
91 logger.debug( "Removing Terminating Service Table Flow ") ;
92 ItmUtils.setUpOrRemoveTerminatingServiceTable(dpnRead.getDPNID(), mdsalManager,false);
93 logger.trace("DPN Removal from DPNTEPSINFO CONFIG DS " + dpnRead);
94 t.delete(LogicalDatastoreType.CONFIGURATION, dpnPath);
95 InstanceIdentifier<DpnEndpoints> tnlContainerPath =
96 InstanceIdentifier.builder(DpnEndpoints.class).build();
97 Optional<DpnEndpoints> containerOptional =
98 ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
99 tnlContainerPath, dataBroker);
100 // remove container if no DPNs are present
101 if (containerOptional.isPresent()) {
102 DpnEndpoints deps = containerOptional.get();
103 if (deps.getDPNTEPsInfo() == null || deps.getDPNTEPsInfo().isEmpty()) {
104 logger.trace("Container Removal from DPNTEPSINFO CONFIG DS");
105 t.delete(LogicalDatastoreType.CONFIGURATION, tnlContainerPath);
112 futures.add( t.submit() );
113 } catch (Exception e1) {
114 logger.error("exception while deleting tep", e1);
119 private static void removeTrunkInterface(DataBroker dataBroker, IdManagerService idManagerService,
120 TunnelEndPoints srcTep, TunnelEndPoints dstTep, BigInteger srcDpnId, BigInteger dstDpnId,
121 WriteTransaction t, List<ListenableFuture<Void>> futures) {
122 String trunkfwdIfName =
123 ItmUtils.getTrunkInterfaceName(idManagerService, srcTep.getInterfaceName(), srcTep.getIpAddress()
124 .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
126 logger.trace("Removing forward Trunk Interface " + trunkfwdIfName);
127 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkfwdIfName);
128 logger.debug( " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkfwdIfName, trunkIdentifier ) ;
129 t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
131 // also update itm-state ds -- Delete the forward tunnel-interface from the tunnel list
132 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
134 .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));
135 t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
136 // Release the Ids for the forward trunk interface Name
137 ItmUtils.releaseIdForTrunkInterfaceName(idManagerService,srcTep.getInterfaceName(), srcTep.getIpAddress()
138 .getIpv4Address().getValue(), dstTep.getIpAddress().getIpv4Address()
141 String trunkRevIfName =
142 ItmUtils.getTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
143 .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
145 logger.trace("Removing Reverse Trunk Interface " + trunkRevIfName);
146 trunkIdentifier = ItmUtils.buildId(trunkRevIfName);
147 logger.debug( " Removing Trunk Interface Name - {} , Id - {} from Config DS ", trunkRevIfName, trunkIdentifier ) ;
148 t.delete(LogicalDatastoreType.CONFIGURATION, trunkIdentifier);
150 // also update itm-state ds -- Delete the reverse tunnel-interface from the tunnel list
151 path = InstanceIdentifier.create(
153 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId));
154 t.delete(LogicalDatastoreType.CONFIGURATION,path) ;
156 // Release the Ids for the reverse trunk interface Name
157 ItmUtils.releaseIdForTrunkInterfaceName(idManagerService, dstTep.getInterfaceName(), dstTep.getIpAddress()
158 .getIpv4Address().getValue(), srcTep.getIpAddress().getIpv4Address()
161 private static boolean checkIfTrunkExists( BigInteger srcDpnId, BigInteger dstDpnId, DataBroker dataBroker) {
162 boolean existsFlag = false ;
163 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
165 .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));
166 Optional<InternalTunnel> internalTunnels = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,path, dataBroker) ;
167 if( internalTunnels.isPresent())