2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.vpnmanager;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import javax.annotation.PreDestroy;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.genius.infra.Datastore.Configuration;
25 import org.opendaylight.genius.infra.Datastore.Operational;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
28 import org.opendaylight.genius.infra.TypedReadTransaction;
29 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
30 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
31 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesOp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInterfaceOpData;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 public class VpnInterfaceOpListener extends AbstractAsyncDataTreeChangeListener<VpnInterfaceOpDataEntry> {
49 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceOpListener.class);
50 private final DataBroker dataBroker;
51 private final ManagedNewTransactionRunner txRunner;
52 private final VpnInterfaceManager vpnInterfaceManager;
53 private final VpnFootprintService vpnFootprintService;
54 private final JobCoordinator jobCoordinator;
55 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
56 private final VpnUtil vpnUtil;
58 /*public VpnInterfaceOpListener(final DataBroker dataBroker) {
59 super(VpnInterface.class);
60 this.dataBroker = dataBroker;
64 public VpnInterfaceOpListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
65 final VpnFootprintService vpnFootprintService, final JobCoordinator jobCoordinator,
66 final VpnUtil vpnUtil) {
67 super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(VpnInterfaceOpData.class)
68 .child(VpnInterfaceOpDataEntry.class),
69 org.opendaylight.infrautils.utils.concurrent.Executors
70 .newListeningSingleThreadExecutor("VpnInterfaceOpListener", LOG));
71 this.dataBroker = dataBroker;
72 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
73 this.vpnInterfaceManager = vpnInterfaceManager;
74 this.vpnFootprintService = vpnFootprintService;
75 this.jobCoordinator = jobCoordinator;
76 this.vpnUtil = vpnUtil;
81 LOG.info("{} start", getClass().getSimpleName());
88 org.opendaylight.infrautils.utils.concurrent.Executors.shutdownAndAwaitTermination(getExecutorService());
92 public void remove(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
93 final VpnInterfaceOpDataEntry del) {
94 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class);
95 final String interfaceName = key.getName();
96 jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName,
97 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
98 postProcessVpnInterfaceRemoval(identifier, del, tx, null);
99 LOG.info("remove: Removed vpn operational data for interface {} on dpn {} vpn {}", del.getName(),
100 del.getDpnId(), del.getVpnInstanceName());
104 private void postProcessVpnInterfaceRemoval(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
105 VpnInterfaceOpDataEntry del, @Nullable TypedReadWriteTransaction<Operational> operTx,
106 @Nullable TypedReadTransaction<Configuration> confTx) throws InterruptedException {
107 if (confTx == null) {
108 txRunner.callWithNewReadOnlyTransactionAndClose(CONFIGURATION,
109 tx -> postProcessVpnInterfaceRemoval(identifier, del, operTx, tx));
112 if (operTx == null) {
113 LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
114 tx -> postProcessVpnInterfaceRemoval(identifier, del, tx, confTx)), LOG,
115 "Error post-processing VPN interface removal");
118 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class);
119 String interfaceName = key.getName();
120 String vpnName = del.getVpnInstanceName();
122 LOG.info("postProcessVpnInterfaceRemoval: interface name {} vpnName {} dpn {}", interfaceName, vpnName,
124 //decrement the vpn interface count in Vpn Instance Op Data
125 Optional<VpnInstance> vpnInstance =
126 confTx.read(VpnOperDsUtils.getVpnInstanceToVpnIdIdentifier(vpnName)).get();
128 if (vpnInstance.isPresent()) {
129 String rd = vpnInstance.get().getVrfId();
131 VpnInstanceOpDataEntry vpnInstOp = vpnUtil.getVpnInstanceOpData(rd);
133 AdjacenciesOp adjs = del.augmentation(AdjacenciesOp.class);
134 List<Adjacency> adjList = adjs != null ? adjs.getAdjacency() : null;
136 if (vpnInstOp != null && adjList != null && adjList.size() > 0) {
138 * When a VPN Interface is removed by FibManager (aka VrfEntryListener and its cohorts),
139 * one adjacency or two adjacency (in case of dual-stack)
140 * for that VPN Interface will be hanging around along with that
141 * VPN Interface. That adjacency could be primary (or) non-primary.
142 * If its a primary adjacency, then a prefix-to-interface entry will be available for the
143 * same. If its a non-primary adjacency, then a prefix-to-interface entry will not be
144 * available for the same, instead we will have vpn-to-extraroutes filled in for them.
146 * Here we try to remove prefix-to-interface entry for pending adjacency in the deleted
147 * vpnInterface. More importantly, we also update the vpnInstanceOpData by removing this
148 * vpnInterface from it.
150 List<Prefixes> prefixToInterface = new ArrayList<>();
151 for (Adjacency adjacency : adjs.getAdjacency()) {
152 List<Prefixes> prefixToInterfaceLocal = new ArrayList<>();
153 Optional<Prefixes> prefix = operTx.read(
154 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
155 VpnUtil.getIpPrefix(adjacency.getIpAddress()))).get();
156 if (prefix.isPresent()) {
157 prefixToInterfaceLocal.add(prefix.get());
159 if (prefixToInterfaceLocal.isEmpty() && adjacency.getNextHopIpList() != null) {
160 for (String nh : adjacency.getNextHopIpList()) {
161 prefix = operTx.read(VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
162 VpnUtil.getIpPrefix(nh))).get();
163 if (prefix.isPresent()) {
164 prefixToInterfaceLocal.add(prefix.get());
168 if (!prefixToInterfaceLocal.isEmpty()) {
169 prefixToInterface.addAll(prefixToInterfaceLocal);
173 * In VPN Migration scenarios, there is a race condition where we use the new DPNID
174 * for the migrated VM instead of old DPNID because when we read prefix-to-interface to cleanup
175 * old DPNID, we actually get the new DPNID.
177 * More dangerously, we tend to alter the new prefix-to-interface which should be retained intac
178 * for the migration to succeed in L3VPN. As a workaround, here we are going to use the dpnId in
179 * the deleted vpnInterface itself instead of tinkering with the prefix-to-interface. Further we
180 * will tinker prefix-to-interface only when are damn sure if its value matches our
181 * deleted vpnInterface.
184 for (Prefixes pref : prefixToInterface) {
185 if (VpnUtil.isMatchedPrefixToInterface(pref, del)) {
187 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(), pref.getIpAddress()));
191 if (del.getDpnId() != null) {
192 vpnFootprintService.updateVpnToDpnMapping(del.getDpnId(), del.getVpnInstanceName(), rd,
193 interfaceName, null /*ipAddressSourceValuePair*/,
194 false /* do delete */);
196 LOG.info("postProcessVpnInterfaceRemoval: Removed vpn operational data and updated vpn footprint"
197 + " for interface {} on dpn {} vpn {}", interfaceName, del.getDpnId(), vpnName);
199 LOG.error("postProcessVpnInterfaceRemoval: rd not retrievable as vpninstancetovpnid for vpn {}"
200 + " is absent, trying rd as {}. interface {} dpn {}", vpnName, vpnName, interfaceName,
203 notifyTaskIfRequired(interfaceName);
204 } catch (InterruptedException | ExecutionException e) {
205 LOG.error("postProcessVpnInterfaceRemoval: Failed to read data store for interface {} vpn {}",
206 interfaceName, vpnName);
210 private void notifyTaskIfRequired(String intfName) {
211 Runnable notifyTask = vpnInterfaceManager.isNotifyTaskQueued(intfName);
212 if (notifyTask == null) {
213 LOG.debug("notifyTaskIfRequired: No tasks queued to wait for deletion of vpnInterface {}", intfName);
216 executorService.execute(notifyTask);
220 public void update(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
221 final VpnInterfaceOpDataEntry original, final VpnInterfaceOpDataEntry update) {
222 LOG.info("update: interface {} vpn {}", original.getName(), original.getVpnInstanceName());
226 public void add(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, VpnInterfaceOpDataEntry add) {
227 LOG.info("add: interface {} vpn {}. Ignoring", add.getName(), add.getVpnInstanceName());