b790d8c5c3c3d4711e684a09cc3e22284719fb7f
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmTepRemoveWorker.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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.genius.infra.Datastore;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
20 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
21 import org.opendaylight.genius.itm.cache.DPNTEPsInfoCache;
22 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.DcGatewayIpList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.dc.gateway.ip.list.DcGatewayIp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ItmTepRemoveWorker implements Callable<List<? extends ListenableFuture<?>>> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(ItmTepRemoveWorker.class);
36
37     private final List<DPNTEPsInfo> delDpnList ;
38     private final IMdsalApiManager mdsalManager;
39     private final List<HwVtep> cfgdHwVteps;
40     private final TransportZone originalTZone;
41     private final ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker;
42     private final DPNTEPsInfoCache dpnTEPsInfoCache;
43     private final ManagedNewTransactionRunner txRunner;
44     private final ItmConfig itmConfig;
45
46     private Collection<DPNTEPsInfo> meshedDpnList ;
47
48     public ItmTepRemoveWorker(List<DPNTEPsInfo> delDpnList, List<HwVtep> delHwList, TransportZone originalTZone,
49                               IMdsalApiManager mdsalManager,
50                               ItmInternalTunnelDeleteWorker itmInternalTunnelDeleteWorker,
51                               DPNTEPsInfoCache dpnTEPsInfoCache, ManagedNewTransactionRunner txRunner,
52                               ItmConfig itmConfig) {
53         this.delDpnList = delDpnList;
54         this.mdsalManager = mdsalManager;
55         this.cfgdHwVteps = delHwList;
56         this.originalTZone = originalTZone;
57         this.itmInternalTunnelDeleteWorker = itmInternalTunnelDeleteWorker;
58         this.dpnTEPsInfoCache = dpnTEPsInfoCache;
59         this.txRunner = txRunner;
60         this.itmConfig = itmConfig;
61         LOG.trace("ItmTepRemoveWorker initialized with  DpnList {}", delDpnList);
62         LOG.trace("ItmTepRemoveWorker initialized with  cfgdHwTeps {}", delHwList);
63     }
64
65     @Override
66     public List<ListenableFuture<Void>> call() {
67         List<ListenableFuture<Void>> futures = new ArrayList<>() ;
68         this.meshedDpnList = dpnTEPsInfoCache.getAllPresent();
69         futures.addAll(itmInternalTunnelDeleteWorker.deleteTunnels(mdsalManager, delDpnList, meshedDpnList));
70         LOG.debug("Invoking Internal Tunnel delete method with DpnList to be deleted {} ; Meshed DpnList {} ",
71                 delDpnList, meshedDpnList);
72         // IF EXTERNAL TUNNELS NEEDS TO BE DELETED, DO IT HERE, IT COULD BE TO DC GATEWAY OR TOR SWITCH
73         futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
74             tx -> {
75                 Optional<DcGatewayIpList> optional = tx.read(InstanceIdentifier.builder(DcGatewayIpList.class)
76                         .build()).get();
77                 if (optional.isPresent()) {
78                     List<DcGatewayIp> dcGatewayIpList = optional.get().getDcGatewayIp();
79                     if (dcGatewayIpList != null && !dcGatewayIpList.isEmpty()) {
80                         processExternalTunnelTepDelete(dcGatewayIpList, tx);
81                     }
82                 }
83             }
84         ));
85         futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
86             tx -> ItmExternalTunnelDeleteWorker.deleteHwVtepsTunnels(delDpnList, cfgdHwVteps, this.originalTZone, tx,
87                     itmConfig)));
88         return futures;
89     }
90
91     @Override
92     public String toString() {
93         return "ItmTepRemoveWorker  { Delete Dpn List : " + delDpnList + " }" ;
94     }
95
96     private void processExternalTunnelTepDelete(Collection<DcGatewayIp> dcGatewayIpList,
97                                                 TypedReadWriteTransaction<Datastore.Configuration> tx) {
98         List<DPNTEPsInfo>  dpnDeleteList = new ArrayList<>();
99         for (DPNTEPsInfo dpnTEPInfo : delDpnList) {
100             List<TunnelEndPoints> tunnelEndPointsList = dpnTEPInfo.nonnullTunnelEndPoints();
101             if (tunnelEndPointsList.size() == 1) {
102                 dpnDeleteList.add(dpnTEPInfo);
103             } else {
104                 LOG.error("DPNTEPInfo not available in data store for dpnId {}. Unable to delete external tunnel "
105                         + "for dpn ", dpnTEPInfo.getDPNID());
106             }
107         }
108         dcGatewayIpList.forEach(dcGatewayIp -> ItmExternalTunnelDeleteWorker.deleteTunnels(dpnDeleteList,
109             meshedDpnList, dcGatewayIp.getIpAddress(), dcGatewayIp.getTunnnelType(), tx));
110     }
111 }