Stale MIP FIB/Flow entries present upon deletion of VRRP master
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / iplearn / IpMonitorStopTask.java
1 /*
2  * Copyright © 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.netvirt.vpnmanager.iplearn;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Objects;
14 import java.util.concurrent.Callable;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.genius.infra.Datastore;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
20 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
21 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
22 import org.opendaylight.netvirt.vpnmanager.iplearn.model.MacEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class IpMonitorStopTask implements Callable<List<ListenableFuture<Void>>> {
29     private static final Logger LOG = LoggerFactory.getLogger(IpMonitorStopTask.class);
30     private MacEntry macEntry;
31     private DataBroker dataBroker;
32     private final AlivenessMonitorUtils alivenessMonitorUtils;
33     private boolean isRemoveMipAdjAndLearntIp;
34     private final VpnUtil vpnUtil;
35     private final ManagedNewTransactionRunner txRunner;
36
37     public IpMonitorStopTask(MacEntry macEntry, DataBroker dataBroker, boolean removeMipAdjAndLearntIp, VpnUtil vpnUtil,
38                              AlivenessMonitorUtils alivenessMonitorUtils) {
39         this.macEntry = macEntry;
40         this.dataBroker = dataBroker;
41         this.alivenessMonitorUtils = alivenessMonitorUtils;
42         this.isRemoveMipAdjAndLearntIp = removeMipAdjAndLearntIp;
43         this.vpnUtil = vpnUtil;
44         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
45     }
46
47     @Override
48     public List<ListenableFuture<Void>> call() {
49         final List<ListenableFuture<Void>> futures = new ArrayList<>();
50         java.util.Optional<Uint32> monitorIdOptional = alivenessMonitorUtils.getMonitorIdFromInterface(macEntry);
51         if (monitorIdOptional.isPresent()) {
52             alivenessMonitorUtils.stopIpMonitoring(monitorIdOptional.get());
53         } else {
54             LOG.warn("MonitorId not available for IP {} interface {}. IpMonitoring not stopped",
55                     macEntry.getIpAddress(), macEntry.getInterfaceName());
56         }
57
58         String learntIp = macEntry.getIpAddress().getHostAddress();
59         if (this.isRemoveMipAdjAndLearntIp) {
60             String vpnName =  macEntry.getVpnName();
61             LearntVpnVipToPort vpnVipToPort = vpnUtil.getLearntVpnVipToPort(vpnName, learntIp);
62             if (vpnVipToPort != null && !Objects.equals(vpnVipToPort.getCreationTime(), macEntry.getCreatedTime())) {
63                 LOG.warn("The MIP {} over vpn {} has been learnt again and processed. "
64                         + "Ignoring this remove event.", learntIp, vpnName);
65                 return futures;
66             }
67             vpnUtil.removeLearntVpnVipToPort(macEntry.getVpnName(),
68                     macEntry.getIpAddress().getHostAddress(), null);
69             vpnUtil.removeVpnPortFixedIpToPort(dataBroker, macEntry.getVpnName(),
70                     macEntry.getIpAddress().getHostAddress(), null);
71
72             ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
73                     Datastore.CONFIGURATION, tx -> vpnUtil.removeMipAdjacency(macEntry.getVpnName(),
74                             macEntry.getInterfaceName(), macEntry.getIpAddress().getHostAddress(), tx)),
75                     LOG, "ArpMonitorStopTask: Error writing to datastore for Vpn {} IP  {}",
76                     macEntry.getVpnName(), macEntry.getIpAddress().getHostAddress());
77         } else {
78             // Delete only MIP adjacency
79             vpnUtil.removeMipAdjacency(macEntry.getInterfaceName(), learntIp);
80         }
81         return futures;
82     }
83 }