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