NETVIRT-1520: AlivenessMonitor cache issue
[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 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
16 import org.opendaylight.netvirt.vpnmanager.iplearn.model.MacEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class IpMonitorStopTask implements Callable<List<ListenableFuture<Void>>> {
22     private static final Logger LOG = LoggerFactory.getLogger(IpMonitorStopTask.class);
23     private MacEntry macEntry;
24     private final AlivenessMonitorUtils alivenessMonitorUtils;
25     private boolean isRemoveMipAdjAndLearntIp;
26     private final VpnUtil vpnUtil;
27
28     public IpMonitorStopTask(MacEntry macEntry, boolean removeMipAdjAndLearntIp, VpnUtil vpnUtil,
29                               AlivenessMonitorUtils alivenessMonitorUtils) {
30         this.macEntry = macEntry;
31         this.alivenessMonitorUtils = alivenessMonitorUtils;
32         this.isRemoveMipAdjAndLearntIp = removeMipAdjAndLearntIp;
33         this.vpnUtil = vpnUtil;
34     }
35
36     @Override
37     public List<ListenableFuture<Void>> call() {
38         final List<ListenableFuture<Void>> futures = new ArrayList<>();
39         java.util.Optional<Long> monitorIdOptional = alivenessMonitorUtils.getMonitorIdFromInterface(macEntry);
40         if (monitorIdOptional.isPresent()) {
41             alivenessMonitorUtils.stopIpMonitoring(monitorIdOptional.get());
42         } else {
43             LOG.warn("MonitorId not available for IP {} interface {}. IpMonitoring not stopped",
44                     macEntry.getIpAddress(), macEntry.getInterfaceName());
45         }
46
47         String learntIp = macEntry.getIpAddress().getHostAddress();
48         if (this.isRemoveMipAdjAndLearntIp) {
49             String vpnName =  macEntry.getVpnName();
50             LearntVpnVipToPort vpnVipToPort = vpnUtil.getLearntVpnVipToPort(vpnName, learntIp);
51             if (vpnVipToPort != null && !Objects.equals(vpnVipToPort.getCreationTime(), macEntry.getCreatedTime())) {
52                 LOG.warn("The MIP {} over vpn {} has been learnt again and processed. "
53                         + "Ignoring this remove event.", learntIp, vpnName);
54                 return futures;
55             }
56             vpnUtil.removeMipAdjAndLearntIp(vpnName, macEntry.getInterfaceName(), learntIp);
57         } else {
58             // Delete only MIP adjacency
59             vpnUtil.removeMipAdjacency(macEntry.getInterfaceName(), learntIp);
60         }
61         return futures;
62     }
63
64
65 }