NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / intervpnlink / tasks / InterVpnLinkCleanedCheckerTask.java
1 /*
2  * Copyright (c) 2016 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.intervpnlink.tasks;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
19 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
20 import org.opendaylight.netvirt.vpnmanager.intervpnlink.InterVpnLinkUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This Task is in charge of checking that all stuff related to a given
29  * InterVpnLink has been removed, like the stateful information, leaked
30  * vrfEntries, etc.
31  */
32 public class InterVpnLinkCleanedCheckerTask implements Callable<List<? extends ListenableFuture<?>>> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkCleanedCheckerTask.class);
35     private static final long MAX_WAIT_FOR_REMOVAL = 10000; // 10 seconds
36
37     private final DataBroker dataBroker;
38     private final InterVpnLink interVpnLinkToCheck;
39     private final InterVpnLinkUtil interVpnLinkUtil;
40     private final VpnUtil vpnUtil;
41
42     public InterVpnLinkCleanedCheckerTask(DataBroker dataBroker, InterVpnLink interVpnLink,
43                                           InterVpnLinkUtil interVpnLinkUtil, VpnUtil vpnUtil) {
44         this.dataBroker = dataBroker;
45         this.interVpnLinkToCheck = interVpnLink;
46         this.interVpnLinkUtil = interVpnLinkUtil;
47         this.vpnUtil = vpnUtil;
48     }
49
50     @Override
51     public List<ListenableFuture<Void>> call() throws Exception {
52         LOG.debug("Checking if InterVpnLink {} is fully cleaned. MAX_WAIT_FOR_REMOVAL={}",
53             interVpnLinkToCheck.getName(), MAX_WAIT_FOR_REMOVAL);
54
55         // Check that the State has also been deleted
56         Optional<InterVpnLinkState> optIVpnLinkState =
57             interVpnLinkUtil.getInterVpnLinkState(interVpnLinkToCheck.getName());
58         long t0 = System.currentTimeMillis();
59         long elapsedTime = t0;
60         while (optIVpnLinkState.isPresent() && elapsedTime - t0 < MAX_WAIT_FOR_REMOVAL) {
61             LOG.debug("InterVpnLink {} State has not yet been removed after {}ms.",
62                 interVpnLinkToCheck.getName(), elapsedTime - t0);
63             Thread.sleep(50);
64             optIVpnLinkState = interVpnLinkUtil.getInterVpnLinkState(interVpnLinkToCheck.getName());
65             elapsedTime = System.currentTimeMillis();
66         }
67
68         if (optIVpnLinkState.isPresent()) {
69             throw new TimeoutException("InterVpnLink " + interVpnLinkToCheck.getName()
70                 + " has not been completely removed after " + MAX_WAIT_FOR_REMOVAL + " ms");
71         }
72
73         LOG.debug("InterVpnLink {} State has been removed. Now checking leaked routes", interVpnLinkToCheck.getName());
74         String vpn1Rd = vpnUtil.getVpnRd(interVpnLinkToCheck.getFirstEndpoint().getVpnUuid().getValue());
75         List<VrfEntry> leakedVrfEntries =
76             vpnUtil.getVrfEntriesByOrigin(vpn1Rd, Collections.singletonList(RouteOrigin.INTERVPN));
77
78         while (!leakedVrfEntries.isEmpty() && t0 - elapsedTime < MAX_WAIT_FOR_REMOVAL) {
79             leakedVrfEntries = vpnUtil.getVrfEntriesByOrigin(vpn1Rd, Collections.singletonList(RouteOrigin.INTERVPN));
80             elapsedTime = System.currentTimeMillis();
81         }
82         if (!leakedVrfEntries.isEmpty()) {
83             LOG.info("InterVpnLink {} leaked routes have not been removed after {} ms",
84                 interVpnLinkToCheck.getName(), MAX_WAIT_FOR_REMOVAL);
85             throw new TimeoutException("InterVpnLink " + interVpnLinkToCheck.getName()
86                 + " has not been completely removed after " + MAX_WAIT_FOR_REMOVAL + " ms");
87         }
88
89         LOG.debug("InterVpnLink {} State and leaked routes are now fully removed", interVpnLinkToCheck.getName());
90
91         return new ArrayList<>();
92     }
93
94 }