2 * Copyright (c) 2016 - 2017 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 java.util.Collections.emptyList;
11 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
12 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Iterators;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23 import java.math.BigInteger;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.Objects;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentLinkedQueue;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.locks.ReentrantLock;
35 import java.util.function.Consumer;
36 import java.util.function.Predicate;
37 import java.util.stream.Collectors;
38 import javax.annotation.PostConstruct;
39 import javax.annotation.PreDestroy;
40 import javax.inject.Inject;
41 import javax.inject.Singleton;
42 import org.eclipse.jdt.annotation.Nullable;
43 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
44 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
45 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
46 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
47 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
48 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
49 import org.opendaylight.genius.infra.Datastore.Configuration;
50 import org.opendaylight.genius.infra.Datastore.Operational;
51 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
52 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
53 import org.opendaylight.genius.infra.TransactionAdapter;
54 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
55 import org.opendaylight.genius.infra.TypedWriteTransaction;
56 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
57 import org.opendaylight.genius.mdsalutil.NWUtil;
58 import org.opendaylight.genius.mdsalutil.NwConstants;
59 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
60 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
61 import org.opendaylight.genius.utils.JvmGlobalLocks;
62 import org.opendaylight.infrautils.caches.CacheProvider;
63 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
64 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
65 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
66 import org.opendaylight.netvirt.fibmanager.api.FibHelper;
67 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
68 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
69 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
70 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
71 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
72 import org.opendaylight.netvirt.vpnmanager.arp.responder.ArpResponderHandler;
73 import org.opendaylight.netvirt.vpnmanager.populator.input.L3vpnInput;
74 import org.opendaylight.netvirt.vpnmanager.populator.intfc.VpnPopulator;
75 import org.opendaylight.netvirt.vpnmanager.populator.registry.L3vpnRegistry;
76 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
77 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
78 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
79 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
80 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
81 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelList;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.LabelRouteMap;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.RouterInterface;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.RouterInterfaceBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.SubnetRoute;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.VrfEntryBase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.label.route.map.LabelRouteInfo;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.label.route.map.LabelRouteInfoBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.label.route.map.LabelRouteInfoKey;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.Adjacencies;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesOp;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.NeutronRouterDpns;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceOpData;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency.AdjacencyType;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnList;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnListKey;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesList;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesListKey;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryKey;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpntargets.VpnTarget;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.subnets.Subnets;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes.NetworkType;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.vpn.portip.port.data.VpnPortipToPort;
121 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
122 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
123 import org.slf4j.Logger;
124 import org.slf4j.LoggerFactory;
127 public class VpnInterfaceManager extends AsyncDataTreeChangeListenerBase<VpnInterface, VpnInterfaceManager> {
129 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
130 private static final short DJC_MAX_RETRIES = 3;
132 private final DataBroker dataBroker;
133 private final ManagedNewTransactionRunner txRunner;
134 private final IBgpManager bgpManager;
135 private final IFibManager fibManager;
136 private final IMdsalApiManager mdsalManager;
137 private final IdManagerService idManager;
138 private final OdlInterfaceRpcService ifaceMgrRpcService;
139 private final VpnFootprintService vpnFootprintService;
140 private final IInterfaceManager interfaceManager;
141 private final IVpnManager vpnManager;
142 private final ArpResponderHandler arpResponderHandler;
143 private final JobCoordinator jobCoordinator;
144 private final VpnUtil vpnUtil;
146 private final ConcurrentHashMap<String, Runnable> vpnIntfMap = new ConcurrentHashMap<>();
148 private final Map<String, ConcurrentLinkedQueue<UnprocessedVpnInterfaceData>> unprocessedVpnInterfaces =
149 new ConcurrentHashMap<>();
151 private final InstanceIdDataObjectCache<VpnInstanceOpDataEntry> vpnInstanceOpDataEntryCache;
154 public VpnInterfaceManager(final DataBroker dataBroker,
155 final IBgpManager bgpManager,
156 final IdManagerService idManager,
157 final IMdsalApiManager mdsalManager,
158 final IFibManager fibManager,
159 final OdlInterfaceRpcService ifaceMgrRpcService,
160 final VpnFootprintService vpnFootprintService,
161 final IInterfaceManager interfaceManager,
162 final IVpnManager vpnManager,
163 final ArpResponderHandler arpResponderHandler,
164 final JobCoordinator jobCoordinator,
165 final CacheProvider cacheProvider,
166 final VpnUtil vpnUtil) {
167 super(VpnInterface.class, VpnInterfaceManager.class);
169 this.dataBroker = dataBroker;
170 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
171 this.bgpManager = bgpManager;
172 this.idManager = idManager;
173 this.mdsalManager = mdsalManager;
174 this.fibManager = fibManager;
175 this.ifaceMgrRpcService = ifaceMgrRpcService;
176 this.vpnFootprintService = vpnFootprintService;
177 this.interfaceManager = interfaceManager;
178 this.vpnManager = vpnManager;
179 this.arpResponderHandler = arpResponderHandler;
180 this.jobCoordinator = jobCoordinator;
181 this.vpnUtil = vpnUtil;
183 vpnInstanceOpDataEntryCache = new InstanceIdDataObjectCache<>(VpnInstanceOpDataEntry.class, dataBroker,
184 LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(
185 VpnInstanceOpData.class).child(VpnInstanceOpDataEntry.class).build(), cacheProvider);
188 public Runnable isNotifyTaskQueued(String intfName) {
189 return vpnIntfMap.remove(intfName);
193 public void start() {
194 LOG.info("{} start", getClass().getSimpleName());
195 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
200 public void close() {
202 vpnInstanceOpDataEntryCache.close();
206 protected InstanceIdentifier<VpnInterface> getWildCardPath() {
207 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
211 protected VpnInterfaceManager getDataTreeChangeListener() {
212 return VpnInterfaceManager.this;
216 public void add(final InstanceIdentifier<VpnInterface> identifier, final VpnInterface vpnInterface) {
217 LOG.trace("Received VpnInterface add event: vpnInterface={}", vpnInterface);
218 LOG.info("add: intfName {} onto vpnName {}", vpnInterface.getName(),
219 VpnHelper.getVpnInterfaceVpnInstanceNamesString(vpnInterface.getVpnInstanceNames()));
220 addVpnInterface(identifier, vpnInterface, null, null);
223 private boolean canHandleNewVpnInterface(final InstanceIdentifier<VpnInterface> identifier,
224 final VpnInterface vpnInterface, String vpnName) {
225 // FIXME: separate this out somehow?
226 final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnName);
229 if (isVpnInstanceReady(vpnName)) {
232 addToUnprocessedVpnInterfaces(identifier, vpnInterface, vpnName);
239 // TODO Clean up the exception handling
240 @SuppressWarnings("checkstyle:IllegalCatch")
241 private void addVpnInterface(final InstanceIdentifier<VpnInterface> identifier, final VpnInterface vpnInterface,
242 final @Nullable List<Adjacency> oldAdjs, final @Nullable List<Adjacency> newAdjs) {
243 for (VpnInstanceNames vpnInterfaceVpnInstance : vpnInterface.nonnullVpnInstanceNames()) {
244 String vpnName = vpnInterfaceVpnInstance.getVpnName();
245 addVpnInterfaceCall(identifier, vpnInterface, oldAdjs, newAdjs, vpnName);
249 private void addVpnInterfaceCall(final InstanceIdentifier<VpnInterface> identifier, final VpnInterface vpnInterface,
250 final List<Adjacency> oldAdjs, final List<Adjacency> newAdjs, String vpnName) {
251 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class);
252 final String interfaceName = key.getName();
254 if (!canHandleNewVpnInterface(identifier, vpnInterface, vpnName)) {
255 LOG.error("add: VpnInstance {} for vpnInterface {} not ready, holding on ",
256 vpnName, vpnInterface.getName());
259 InstanceIdentifier<VpnInterfaceOpDataEntry> vpnInterfaceOpIdentifier = VpnUtil
260 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
261 List<Adjacency> copyOldAdjs = null;
262 if (oldAdjs != null) {
263 copyOldAdjs = new ArrayList<>();
264 copyOldAdjs.addAll(oldAdjs);
266 List<Adjacency> copyNewAdjs = null;
267 if (newAdjs != null) {
268 copyNewAdjs = new ArrayList<>();
269 copyNewAdjs.addAll(newAdjs);
271 addVpnInterfaceToVpn(vpnInterfaceOpIdentifier, vpnInterface, copyOldAdjs, copyNewAdjs, identifier, vpnName);
274 private void addVpnInterfaceToVpn(final InstanceIdentifier<VpnInterfaceOpDataEntry> vpnInterfaceOpIdentifier,
275 final VpnInterface vpnInterface, final @Nullable List<Adjacency> oldAdjs,
276 final @Nullable List<Adjacency> newAdjs,
277 final InstanceIdentifier<VpnInterface> identifier, String vpnName) {
278 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class);
279 final String interfaceName = key.getName();
280 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
281 if (!vpnUtil.isVpnPendingDelete(primaryRd)) {
282 Interface interfaceState = InterfaceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceName);
283 boolean isBgpVpnInternetVpn = vpnUtil.isBgpVpnInternet(vpnName);
284 if (interfaceState != null) {
286 final BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(interfaceState);
287 final int ifIndex = interfaceState.getIfIndex();
288 jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName, () -> {
289 // TODO Deal with sequencing — the config tx must only submitted if the oper tx goes in
290 // (the inventory tx goes in last)
291 List<ListenableFuture<Void>> futures = new ArrayList<>();
292 //set of prefix used, as entry in prefix-to-interface datastore
293 // is prerequisite for refresh Fib to avoid race condition leading to
294 // missing remote next hop in bucket actions on bgp-vpn delete
295 Set<String> prefixListForRefreshFib = new HashSet<>();
296 ListenableFuture<Void> confFuture =
297 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
298 confTx -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
299 operTx -> futures.add(
300 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, invTx -> {
302 "addVpnInterface: VPN Interface add event - intfName {} vpnName {}"
304 vpnInterface.getName(), vpnName, vpnInterface.getDpnId());
305 processVpnInterfaceUp(dpnId, vpnInterface, primaryRd, ifIndex, false,
306 confTx, operTx, invTx, interfaceState, vpnName,
307 prefixListForRefreshFib);
308 if (oldAdjs != null && !oldAdjs.equals(newAdjs)) {
309 LOG.info("addVpnInterface: Adjacency changed upon VPNInterface {}"
310 + " Update for swapping VPN {} case.", interfaceName, vpnName);
311 if (newAdjs != null) {
312 for (Adjacency adj : newAdjs) {
313 if (oldAdjs.contains(adj)) {
316 if (!isBgpVpnInternetVpn
317 || vpnUtil.isAdjacencyEligibleToVpnInternet(adj)) {
318 addNewAdjToVpnInterface(vpnInterfaceOpIdentifier,
319 primaryRd, adj, dpnId, operTx, confTx, invTx,
320 prefixListForRefreshFib);
325 for (Adjacency adj : oldAdjs) {
326 if (!isBgpVpnInternetVpn
327 || vpnUtil.isAdjacencyEligibleToVpnInternet(adj)) {
328 delAdjFromVpnInterface(vpnInterfaceOpIdentifier, adj, dpnId,
334 Futures.addCallback(confFuture,
335 new VpnInterfaceCallBackHandler(primaryRd, prefixListForRefreshFib),
336 MoreExecutors.directExecutor());
337 futures.add(confFuture);
338 Futures.addCallback(confFuture, new PostVpnInterfaceWorker(interfaceName, true, "Config"),
339 MoreExecutors.directExecutor());
340 LOG.info("addVpnInterface: Addition of interface {} in VPN {} on dpn {}"
341 + " processed successfully", interfaceName, vpnName, dpnId);
344 } catch (NumberFormatException | IllegalStateException e) {
345 LOG.error("addVpnInterface: Unable to retrieve dpnId from interface operational data store for "
346 + "interface {}. Interface addition on vpn {} failed", interfaceName,
350 } else if (Boolean.TRUE.equals(vpnInterface.isRouterInterface())) {
351 jobCoordinator.enqueueJob("VPNINTERFACE-" + vpnInterface.getName(),
353 ListenableFuture<Void> future =
354 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, confTx -> {
355 createFibEntryForRouterInterface(primaryRd, vpnInterface, interfaceName,
357 LOG.info("addVpnInterface: Router interface {} for vpn {} on dpn {}", interfaceName,
358 vpnName, vpnInterface.getDpnId());
360 ListenableFutures.addErrorLogging(future, LOG,
361 "Error creating FIB entry for interface {} on VPN {}", vpnInterface.getName(), vpnName);
362 return Collections.singletonList(future);
365 LOG.info("addVpnInterface: Handling addition of VPN interface {} on vpn {} skipped as interfaceState"
366 + " is not available", interfaceName, vpnName);
369 LOG.error("addVpnInterface: Handling addition of VPN interface {} on vpn {} dpn {} skipped"
370 + " as vpn is pending delete", interfaceName, vpnName,
371 vpnInterface.getDpnId());
375 // "Unconditional wait" and "Wait not in loop" wrt the VpnNotifyTask below - suppressing the FB violation -
376 // see comments below.
377 @SuppressFBWarnings({"UW_UNCOND_WAIT", "WA_NOT_IN_LOOP"})
378 protected void processVpnInterfaceUp(final BigInteger dpId, VpnInterface vpnInterface, final String primaryRd,
379 final int lportTag, boolean isInterfaceUp,
380 TypedWriteTransaction<Configuration> writeConfigTxn,
381 TypedWriteTransaction<Operational> writeOperTxn,
382 TypedReadWriteTransaction<Configuration> writeInvTxn,
383 Interface interfaceState, final String vpnName,
384 Set<String> prefixListForRefreshFib) throws ExecutionException, InterruptedException {
385 final String interfaceName = vpnInterface.getName();
386 Optional<VpnInterfaceOpDataEntry> optOpVpnInterface = vpnUtil.getVpnInterfaceOpDataEntry(interfaceName,
388 VpnInterfaceOpDataEntry opVpnInterface = optOpVpnInterface.isPresent() ? optOpVpnInterface.get() : null;
389 boolean isBgpVpnInternetVpn = vpnUtil.isBgpVpnInternet(vpnName);
390 if (!isInterfaceUp) {
391 LOG.info("processVpnInterfaceUp: Binding vpn service to interface {} onto dpn {} for vpn {}",
392 interfaceName, dpId, vpnName);
393 long vpnId = vpnUtil.getVpnId(vpnName);
394 if (vpnId == VpnConstants.INVALID_ID) {
395 LOG.warn("processVpnInterfaceUp: VpnInstance to VPNId mapping not available for VpnName {}"
396 + " processing vpninterface {} on dpn {}, bailing out now.", vpnName, interfaceName,
401 boolean waitForVpnInterfaceOpRemoval = false;
402 if (opVpnInterface != null) {
403 String opVpnName = opVpnInterface.getVpnInstanceName();
404 String primaryInterfaceIp = null;
405 if (Objects.equals(opVpnName, vpnName)) {
406 // Please check if the primary VRF Entry does not exist for VPNInterface
407 // If so, we have to process ADD, as this might be a DPN Restart with Remove and Add triggered
409 // However, if the primary VRF Entry for this VPNInterface exists, please continue bailing out !
410 List<Adjacency> adjs = vpnUtil.getAdjacenciesForVpnInterfaceFromConfig(interfaceName);
412 LOG.error("processVpnInterfaceUp: VPN Interface {} on dpn {} for vpn {} failed as adjacencies"
413 + " for this vpn interface could not be obtained", interfaceName, dpId,
417 for (Adjacency adj : adjs) {
418 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
419 primaryInterfaceIp = adj.getIpAddress();
423 if (primaryInterfaceIp == null) {
424 LOG.error("processVpnInterfaceUp: VPN Interface {} addition on dpn {} for vpn {} failed"
425 + " as primary adjacency for this vpn interface could not be obtained", interfaceName,
429 // Get the rd of the vpn instance
430 VrfEntry vrf = vpnUtil.getVrfEntry(primaryRd, primaryInterfaceIp);
432 LOG.error("processVpnInterfaceUp: VPN Interface {} on dpn {} for vpn {} already provisioned ,"
433 + " bailing out from here.", interfaceName, dpId, vpnName);
436 waitForVpnInterfaceOpRemoval = true;
438 LOG.error("processVpnInterfaceUp: vpn interface {} to go to configured vpn {} on dpn {},"
439 + " but in operational vpn {}", interfaceName, vpnName, dpId, opVpnName);
442 if (!waitForVpnInterfaceOpRemoval) {
443 // Add the VPNInterface and quit
444 vpnFootprintService.updateVpnToDpnMapping(dpId, vpnName, primaryRd, interfaceName,
445 null/*ipAddressSourceValuePair*/,
447 processVpnInterfaceAdjacencies(dpId, lportTag, vpnName, primaryRd, interfaceName,
448 vpnId, writeConfigTxn, writeOperTxn, writeInvTxn, interfaceState, prefixListForRefreshFib);
449 if (!isBgpVpnInternetVpn) {
450 vpnUtil.bindService(vpnName, interfaceName, false /*isTunnelInterface*/);
452 LOG.info("processVpnInterfaceUp: Plumbed vpn interface {} onto dpn {} for vpn {}", interfaceName,
454 if (interfaceManager.isExternalInterface(interfaceName)) {
455 processExternalVpnInterface(interfaceName, vpnName, dpId, lportTag,
456 NwConstants.ADD_FLOW);
461 // FIB didn't get a chance yet to clean up this VPNInterface
462 // Let us give it a chance here !
463 LOG.info("processVpnInterfaceUp: Trying to add VPN Interface {} on dpn {} for vpn {},"
464 + " but waiting for FIB to clean up! ", interfaceName, dpId, vpnName);
466 Runnable notifyTask = new VpnNotifyTask();
467 synchronized (notifyTask) {
468 // Per FB's "Unconditional wait" violation, the code should really verify that the condition it
469 // intends to wait for is not already satisfied before calling wait. However the VpnNotifyTask is
470 // published here while holding the lock on it so this path will hit the wait before notify can be
472 vpnIntfMap.put(interfaceName, notifyTask);
474 notifyTask.wait(VpnConstants.MAX_WAIT_TIME_IN_MILLISECONDS);
475 } catch (InterruptedException e) {
480 vpnIntfMap.remove(interfaceName);
483 if (opVpnInterface != null) {
484 LOG.warn("processVpnInterfaceUp: VPN Interface {} removal on dpn {} for vpn {}"
485 + " by FIB did not complete on time," + " bailing addition ...", interfaceName,
487 vpnUtil.unsetScheduledToRemoveForVpnInterface(interfaceName);
490 // VPNInterface got removed, proceed with Add
491 LOG.info("processVpnInterfaceUp: Continuing to plumb vpn interface {} onto dpn {} for vpn {}",
492 interfaceName, dpId, vpnName);
493 vpnFootprintService.updateVpnToDpnMapping(dpId, vpnName, primaryRd, interfaceName,
494 null/*ipAddressSourceValuePair*/,
496 processVpnInterfaceAdjacencies(dpId, lportTag, vpnName, primaryRd, interfaceName,
497 vpnId, writeConfigTxn, writeOperTxn, writeInvTxn, interfaceState, prefixListForRefreshFib);
498 if (!isBgpVpnInternetVpn) {
499 vpnUtil.bindService(vpnName, interfaceName, false/*isTunnelInterface*/);
501 LOG.info("processVpnInterfaceUp: Plumbed vpn interface {} onto dpn {} for vpn {} after waiting for"
502 + " FIB to clean up", interfaceName, dpId, vpnName);
503 if (interfaceManager.isExternalInterface(interfaceName)) {
504 processExternalVpnInterface(interfaceName, vpnName, dpId,
505 lportTag, NwConstants.ADD_FLOW);
509 // Interface is retained in the DPN, but its Link Up.
510 // Advertise prefixes again for this interface to BGP
511 InstanceIdentifier<VpnInterface> identifier =
512 VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName());
513 InstanceIdentifier<VpnInterfaceOpDataEntry> vpnInterfaceOpIdentifier =
514 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
515 advertiseAdjacenciesForVpnToBgp(primaryRd, dpId, vpnInterfaceOpIdentifier, vpnName, interfaceName);
516 // Perform similar operation as interface add event for extraroutes.
517 InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
518 Optional<Adjacencies> optAdjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker,
519 LogicalDatastoreType.CONFIGURATION, path);
520 if (!optAdjacencies.isPresent()) {
521 LOG.trace("No config adjacencies present for vpninterface {}", vpnInterface);
524 List<Adjacency> adjacencies = optAdjacencies.get().nonnullAdjacency();
525 for (Adjacency adjacency : adjacencies) {
526 if (adjacency.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
529 // if BGPVPN Internet, filter only IPv6 Adjacencies
530 if (isBgpVpnInternetVpn && !vpnUtil.isAdjacencyEligibleToVpnInternet(adjacency)) {
533 addNewAdjToVpnInterface(vpnInterfaceOpIdentifier, primaryRd, adjacency,
534 dpId, writeOperTxn, writeConfigTxn, writeInvTxn, prefixListForRefreshFib);
536 } catch (ReadFailedException e) {
537 LOG.error("processVpnInterfaceUp: Failed to read data store for interface {} vpn {} rd {} dpn {}",
538 interfaceName, vpnName, primaryRd, dpId);
543 private void processExternalVpnInterface(String interfaceName, String vpnName, BigInteger dpId,
544 int lportTag, int addOrRemove) {
547 // vpn instance of ext-net interface is the network-id
548 extNetworkId = new Uuid(vpnName);
549 } catch (IllegalArgumentException e) {
550 LOG.error("processExternalVpnInterface: VPN instance {} is not Uuid. Processing external vpn interface {}"
551 + " on dpn {} failed", vpnName, interfaceName, dpId);
555 List<Uuid> routerIds = vpnUtil.getExternalNetworkRouterIds(extNetworkId);
556 if (routerIds == null || routerIds.isEmpty()) {
557 LOG.info("processExternalVpnInterface: No router is associated with {}."
558 + " Bailing out of processing external vpn interface {} on dpn {} for vpn {}",
559 extNetworkId.getValue(), interfaceName, dpId, vpnName);
563 LOG.info("processExternalVpnInterface: Router-ids {} associated with exernal vpn-interface {} on dpn {}"
564 + " for vpn {}", routerIds, interfaceName, dpId, vpnName);
565 for (Uuid routerId : routerIds) {
566 String routerName = routerId.getValue();
567 BigInteger primarySwitch = vpnUtil.getPrimarySwitchForRouter(routerName);
568 if (Objects.equals(primarySwitch, dpId)) {
569 Routers router = vpnUtil.getExternalRouter(routerName);
570 if (router != null) {
571 if (addOrRemove == NwConstants.ADD_FLOW) {
572 vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName,
573 VpnUtil.getIpsListFromExternalIps(router.getExternalIps()), router.getExtGwMacAddress(),
574 dpId, interfaceName, lportTag);
576 vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName,
577 VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
578 dpId, interfaceName, lportTag);
581 LOG.error("processExternalVpnInterface: No external-router found for router-id {}. Bailing out of"
582 + " processing external vpn-interface {} on dpn {} for vpn {}", routerName,
583 interfaceName, dpId, vpnName);
589 // TODO Clean up the exception handling
590 @SuppressWarnings("checkstyle:IllegalCatch")
591 private void advertiseAdjacenciesForVpnToBgp(final String rd, BigInteger dpnId,
592 final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
593 String vpnName, String interfaceName) {
595 LOG.error("advertiseAdjacenciesForVpnFromBgp: Unable to recover rd for interface {} on dpn {} in vpn {}",
596 interfaceName, dpnId, vpnName);
599 if (rd.equals(vpnName)) {
600 LOG.info("advertiseAdjacenciesForVpnFromBgp: Ignoring BGP advertisement for interface {} on dpn {}"
601 + " as it is in internal vpn{} with rd {}", interfaceName, dpnId, vpnName, rd);
605 LOG.info("advertiseAdjacenciesForVpnToBgp: Advertising interface {} on dpn {} in vpn {} with rd {} ",
606 interfaceName, dpnId, vpnName, rd);
608 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId);
609 if (nextHopIp == null) {
610 LOG.error("advertiseAdjacenciesForVpnToBgp: NextHop for interface {} on dpn {} is null,"
611 + " returning from advertising route with rd {} vpn {} to bgp", interfaceName, dpnId,
618 InstanceIdentifier<AdjacenciesOp> path = identifier.augmentation(AdjacenciesOp.class);
619 Optional<AdjacenciesOp> adjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker,
620 LogicalDatastoreType.OPERATIONAL, path);
621 if (adjacencies.isPresent()) {
622 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
623 if (nextHops != null && !nextHops.isEmpty()) {
624 LOG.debug("advertiseAdjacenciesForVpnToBgp: NextHops are {} for interface {} on dpn {} for vpn {}"
625 + " rd {}", nextHops, interfaceName, dpnId, vpnName, rd);
626 VpnInstanceOpDataEntry vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(rd);
627 long l3vni = vpnInstanceOpData.getL3vni();
628 VrfEntry.EncapType encapType = VpnUtil.isL3VpnOverVxLan(l3vni)
629 ? VrfEntry.EncapType.Vxlan : VrfEntry.EncapType.Mplsgre;
630 for (Adjacency nextHop : nextHops) {
631 if (nextHop.getAdjacencyType() == AdjacencyType.ExtraRoute) {
634 String gatewayMac = null;
636 if (VpnUtil.isL3VpnOverVxLan(l3vni)) {
637 final VpnPortipToPort gwPort = vpnUtil.getNeutronPortFromVpnPortFixedIp(
638 vpnInstanceOpData.getVpnInstanceName(), nextHop.getIpAddress());
639 gatewayMac = arpResponderHandler.getGatewayMacAddressForInterface(gwPort, interfaceName)
642 label = nextHop.getLabel();
645 LOG.info("VPN ADVERTISE: advertiseAdjacenciesForVpnToBgp: Adding Fib Entry rd {} prefix {}"
646 + " nexthop {} label {}", rd, nextHop.getIpAddress(), nextHopIp, label);
647 bgpManager.advertisePrefix(rd, nextHop.getMacAddress(), nextHop.getIpAddress(), nextHopIp,
648 encapType, (int)label, l3vni, 0 /*l2vni*/,
650 LOG.info("VPN ADVERTISE: advertiseAdjacenciesForVpnToBgp: Added Fib Entry rd {} prefix {}"
651 + " nexthop {} label {} for interface {} on dpn {} for vpn {}", rd,
652 nextHop.getIpAddress(), nextHopIp, label, interfaceName, dpnId, vpnName);
653 } catch (Exception e) {
654 LOG.error("advertiseAdjacenciesForVpnToBgp: Failed to advertise prefix {} in vpn {}"
655 + " with rd {} for interface {} on dpn {}", nextHop.getIpAddress(), vpnName, rd,
656 interfaceName, dpnId, e);
661 } catch (ReadFailedException e) {
662 LOG.error("advertiseAdjacenciesForVpnToBgp: Failed to read data store for interface {} dpn {} nexthop {}"
663 + "vpn {} rd {}", interfaceName, dpnId, nextHopIp, vpnName, rd);
667 // TODO Clean up the exception handling
668 @SuppressWarnings("checkstyle:IllegalCatch")
669 private void withdrawAdjacenciesForVpnFromBgp(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
670 String vpnName, String interfaceName, TypedWriteTransaction<Configuration> writeConfigTxn,
671 TypedWriteTransaction<Operational> writeOperTx) {
673 InstanceIdentifier<AdjacenciesOp> path = identifier.augmentation(AdjacenciesOp.class);
674 String rd = vpnUtil.getVpnRd(interfaceName);
676 LOG.error("withdrawAdjacenciesForVpnFromBgp: Unable to recover rd for interface {} in vpn {}",
677 interfaceName, vpnName);
680 if (rd.equals(vpnName)) {
682 "withdrawAdjacenciesForVpnFromBgp: Ignoring BGP withdrawal for interface {} as it is in "
683 + "internal vpn{} with rd {}", interfaceName, vpnName, rd);
687 LOG.info("withdrawAdjacenciesForVpnFromBgp: For interface {} in vpn {} with rd {}", interfaceName,
689 Optional<AdjacenciesOp> adjacencies = Optional.absent();
691 adjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
693 } catch (ReadFailedException e) {
694 LOG.error("withdrawAdjacenciesForVpnFromBgp: Failed to read data store for interface {} vpn {}",
695 interfaceName, vpnName);
697 if (adjacencies.isPresent()) {
698 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
700 if (nextHops != null && !nextHops.isEmpty()) {
701 LOG.trace("withdrawAdjacenciesForVpnFromBgp: NextHops are {} for interface {} in vpn {} rd {}",
702 nextHops, interfaceName, vpnName, rd);
703 for (Adjacency nextHop : nextHops) {
705 if (nextHop.getAdjacencyType() != AdjacencyType.ExtraRoute) {
706 LOG.info("VPN WITHDRAW: withdrawAdjacenciesForVpnFromBgp: Removing Fib Entry rd {}"
707 + " prefix {} for interface {} in vpn {}", rd, nextHop.getIpAddress(),
708 interfaceName, vpnName);
709 bgpManager.withdrawPrefix(rd, nextHop.getIpAddress());
710 LOG.info("VPN WITHDRAW: withdrawAdjacenciesForVpnFromBgp: Removed Fib Entry rd {}"
711 + " prefix {} for interface {} in vpn {}", rd, nextHop.getIpAddress(),
712 interfaceName, vpnName);
713 } else if (nextHop.getNextHopIpList() != null) {
714 // Perform similar operation as interface delete event for extraroutes.
715 String allocatedRd = nextHop.getVrfId();
716 for (String nh : nextHop.getNextHopIpList()) {
717 deleteExtraRouteFromCurrentAndImportingVpns(
718 vpnName, nextHop.getIpAddress(), nh, allocatedRd, interfaceName, writeConfigTxn,
722 } catch (Exception e) {
723 LOG.error("withdrawAdjacenciesForVpnFromBgp: Failed to withdraw prefix {} in vpn {} with rd {}"
724 + " for interface {} ", nextHop.getIpAddress(), vpnName, rd, interfaceName, e);
731 @SuppressWarnings("checkstyle:IllegalCatch")
732 protected void processVpnInterfaceAdjacencies(BigInteger dpnId, final int lportTag, String vpnName,
733 String primaryRd, String interfaceName, final long vpnId,
734 TypedWriteTransaction<Configuration> writeConfigTxn,
735 TypedWriteTransaction<Operational> writeOperTxn,
736 TypedReadWriteTransaction<Configuration> writeInvTxn,
737 Interface interfaceState, Set<String> prefixListForRefreshFib)
738 throws ExecutionException, InterruptedException {
739 InstanceIdentifier<VpnInterface> identifier = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
741 Optional<VpnInterface> vpnInteface = Optional.absent();
743 vpnInteface = SingleTransactionDataBroker.syncReadOptional(dataBroker,
744 LogicalDatastoreType.CONFIGURATION, identifier);
745 } catch (ReadFailedException e) {
746 LOG.error("processVpnInterfaceAdjacencies: Failed to read data store for interface {} vpn {} rd {}"
747 + "dpn {}", interfaceName, vpnName, primaryRd, dpnId);
749 Uuid intfnetworkUuid = null;
750 NetworkType networkType = null;
751 Long segmentationId = Long.valueOf(-1);
752 Adjacencies adjacencies = null;
753 if (vpnInteface.isPresent()) {
754 intfnetworkUuid = vpnInteface.get().getNetworkId();
755 networkType = vpnInteface.get().getNetworkType();
756 segmentationId = vpnInteface.get().getSegmentationId();
757 adjacencies = vpnInteface.get().augmentation(Adjacencies.class);
758 if (adjacencies == null) {
759 addVpnInterfaceToOperational(vpnName, interfaceName, dpnId, null/*adjacencies*/, lportTag,
760 null/*gwMac*/, writeOperTxn);
764 // Get the rd of the vpn instance
765 String nextHopIp = null;
767 nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId);
768 } catch (Exception e) {
769 LOG.error("processVpnInterfaceAdjacencies: Unable to retrieve endpoint ip address for "
770 + "dpnId {} for vpnInterface {} vpnName {}", dpnId, interfaceName, vpnName);
772 List<String> nhList = new ArrayList<>();
773 if (nextHopIp != null) {
774 nhList.add(nextHopIp);
775 LOG.debug("processVpnInterfaceAdjacencies: NextHop for interface {} on dpn {} in vpn {} is {}",
776 interfaceName, dpnId, vpnName, nhList);
778 Optional<String> gwMac = Optional.absent();
779 String vpnInterfaceSubnetGwMacAddress = null;
780 VpnInstanceOpDataEntry vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(primaryRd);
781 Long l3vni = vpnInstanceOpData.getL3vni();
782 boolean isL3VpnOverVxLan = VpnUtil.isL3VpnOverVxLan(l3vni);
783 VrfEntry.EncapType encapType = isL3VpnOverVxLan ? VrfEntry.EncapType.Vxlan : VrfEntry.EncapType.Mplsgre;
784 VpnPopulator registeredPopulator = L3vpnRegistry.getRegisteredPopulator(encapType);
785 List<Adjacency> nextHops = adjacencies != null ? adjacencies.getAdjacency() : emptyList();
786 List<Adjacency> value = new ArrayList<>();
787 for (Adjacency nextHop : nextHops) {
788 String rd = primaryRd;
789 String nexthopIpValue = nextHop.getIpAddress().split("/")[0];
790 if (vpnInstanceOpData.getBgpvpnType() == VpnInstanceOpDataEntry.BgpvpnType.BGPVPNInternet
791 && NWUtil.isIpv4Address(nexthopIpValue)) {
792 String prefix = nextHop.getIpAddress() == null ? "null" :
793 VpnUtil.getIpPrefix(nextHop.getIpAddress());
794 LOG.debug("processVpnInterfaceAdjacencies: UnsupportedOperation : Not Adding prefix {} to interface {}"
795 + " as InternetVpn has an IPV4 address {}", prefix, interfaceName, vpnName);
798 if (nextHop.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
799 String prefix = VpnUtil.getIpPrefix(nextHop.getIpAddress());
800 Prefixes.PrefixCue prefixCue = nextHop.isPhysNetworkFunc()
801 ? Prefixes.PrefixCue.PhysNetFunc : Prefixes.PrefixCue.None;
802 LOG.debug("processVpnInterfaceAdjacencies: Adding prefix {} to interface {} with nextHops {} on dpn {}"
803 + " for vpn {}", prefix, interfaceName, nhList, dpnId, vpnName);
805 Prefixes prefixes = intfnetworkUuid != null
806 ? VpnUtil.getPrefixToInterface(dpnId, interfaceName, prefix, intfnetworkUuid ,networkType,
807 segmentationId, prefixCue) :
808 VpnUtil.getPrefixToInterface(dpnId, interfaceName, prefix, prefixCue);
809 writeOperTxn.merge(VpnUtil.getPrefixToInterfaceIdentifier(
810 vpnUtil.getVpnId(vpnName), prefix), prefixes, true);
811 final Uuid subnetId = nextHop.getSubnetId();
813 String gatewayIp = nextHop.getSubnetGatewayIp();
814 if (gatewayIp == null) {
815 Optional<String> gatewayIpOptional = vpnUtil.getVpnSubnetGatewayIp(subnetId);
816 if (gatewayIpOptional.isPresent()) {
817 gatewayIp = gatewayIpOptional.get();
821 if (gatewayIp != null) {
822 gwMac = getMacAddressForSubnetIp(vpnName, interfaceName, gatewayIp);
823 if (gwMac.isPresent()) {
824 // A valid mac-address is available for this subnet-gateway-ip
825 // Use this for programming ARP_RESPONDER table here. And save this
826 // info into vpnInterface operational, so it can used in VrfEntryProcessor
827 // to populate L3_GW_MAC_TABLE there.
828 arpResponderHandler.addArpResponderFlow(dpnId, lportTag, interfaceName,
829 gatewayIp, gwMac.get());
830 vpnInterfaceSubnetGwMacAddress = gwMac.get();
832 // A valid mac-address is not available for this subnet-gateway-ip
833 // Use the connected-mac-address to configure ARP_RESPONDER Table.
834 // Save this connected-mac-address as gateway-mac-address for the
835 // VrfEntryProcessor to use this later to populate the L3_GW_MAC_TABLE.
836 gwMac = InterfaceUtils.getMacAddressFromInterfaceState(interfaceState);
837 if (gwMac.isPresent()) {
838 vpnUtil.setupGwMacIfExternalVpn(dpnId, interfaceName, vpnId, writeInvTxn,
839 NwConstants.ADD_FLOW, gwMac.get());
840 arpResponderHandler.addArpResponderFlow(dpnId, lportTag, interfaceName,
841 gatewayIp, gwMac.get());
843 LOG.error("processVpnInterfaceAdjacencies: Gateway MAC for subnet ID {} could not be "
844 + "obtained, cannot create ARP responder flow for interface name {}, vpnName {}, "
846 subnetId, interfaceName, vpnName, gatewayIp);
850 LOG.warn("processVpnInterfaceAdjacencies: Gateway IP for subnet ID {} could not be obtained, "
851 + "cannot create ARP responder flow for interface name {}, vpnName {}",
852 subnetId, interfaceName, vpnName);
853 gwMac = InterfaceUtils.getMacAddressFromInterfaceState(interfaceState);
855 LOG.info("processVpnInterfaceAdjacencies: Added prefix {} to interface {} with nextHops {} on dpn {}"
856 + " for vpn {}", prefix, interfaceName, nhList, dpnId, vpnName);
858 //Extra route adjacency
859 String prefix = VpnUtil.getIpPrefix(nextHop.getIpAddress());
860 String vpnPrefixKey = VpnUtil.getVpnNamePrefixKey(vpnName, prefix);
861 // FIXME: separate this out somehow?
862 final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnPrefixKey);
865 java.util.Optional<String> rdToAllocate = vpnUtil
866 .allocateRdForExtraRouteAndUpdateUsedRdsMap(vpnId, null, prefix, vpnName,
867 nextHop.getNextHopIpList().get(0), dpnId);
868 if (rdToAllocate.isPresent()) {
869 rd = rdToAllocate.get();
870 LOG.info("processVpnInterfaceAdjacencies: The rd {} is allocated for the extraroute {}",
873 LOG.error("processVpnInterfaceAdjacencies: No rds to allocate extraroute {}", prefix);
879 LOG.info("processVpnInterfaceAdjacencies: Added prefix {} and nextHopList {} as extra-route for vpn{}"
880 + " interface {} on dpn {}", nextHop.getIpAddress(), nextHop.getNextHopIpList(), vpnName,
881 interfaceName, dpnId);
883 // Please note that primary adjacency will use a subnet-gateway-mac-address that
884 // can be different from the gateway-mac-address within the VRFEntry as the
885 // gateway-mac-address is a superset.
886 RouteOrigin origin = VpnUtil.getRouteOrigin(nextHop.getAdjacencyType());
887 L3vpnInput input = new L3vpnInput().setNextHop(nextHop).setRd(rd).setVpnName(vpnName)
888 .setInterfaceName(interfaceName).setNextHopIp(nextHopIp).setPrimaryRd(primaryRd)
889 .setSubnetGatewayMacAddress(vpnInterfaceSubnetGwMacAddress).setRouteOrigin(origin);
890 Adjacency operationalAdjacency = null;
892 operationalAdjacency = registeredPopulator.createOperationalAdjacency(input);
893 } catch (NullPointerException e) {
894 LOG.error("processVpnInterfaceAdjacencies: failed to create operational adjacency: input: {}, {}",
895 input, e.getMessage());
898 if (nextHop.getAdjacencyType() != AdjacencyType.PrimaryAdjacency) {
899 vpnManager.addExtraRoute(vpnName, nextHop.getIpAddress(), nextHop.getNextHopIpList().get(0), rd,
900 vpnName, l3vni, origin, interfaceName, operationalAdjacency, encapType, prefixListForRefreshFib,
903 value.add(operationalAdjacency);
906 AdjacenciesOp aug = VpnUtil.getVpnInterfaceOpDataEntryAugmentation(value);
907 addVpnInterfaceToOperational(vpnName, interfaceName, dpnId, aug, lportTag,
908 gwMac.isPresent() ? gwMac.get() : null, writeOperTxn);
910 L3vpnInput input = new L3vpnInput().setNextHopIp(nextHopIp).setL3vni(l3vni).setPrimaryRd(primaryRd)
911 .setGatewayMac(gwMac.orNull()).setInterfaceName(interfaceName)
912 .setVpnName(vpnName).setDpnId(dpnId).setEncapType(encapType);
914 for (Adjacency nextHop : aug.getAdjacency()) {
915 // Adjacencies other than primary Adjacencies are handled in the addExtraRoute call above.
916 if (nextHop.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
917 RouteOrigin origin = VpnUtil.getRouteOrigin(nextHop.getAdjacencyType());
918 input.setNextHop(nextHop).setRd(nextHop.getVrfId()).setRouteOrigin(origin);
919 registeredPopulator.populateFib(input, writeConfigTxn);
924 private void addVpnInterfaceToOperational(String vpnName, String interfaceName, BigInteger dpnId, AdjacenciesOp aug,
925 long lportTag, String gwMac,
926 TypedWriteTransaction<Operational> writeOperTxn) {
927 VpnInterfaceOpDataEntry opInterface =
928 VpnUtil.getVpnInterfaceOpDataEntry(interfaceName, vpnName, aug, dpnId, lportTag, gwMac);
929 InstanceIdentifier<VpnInterfaceOpDataEntry> interfaceId = VpnUtil
930 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
931 writeOperTxn.put(interfaceId, opInterface, CREATE_MISSING_PARENTS);
932 LOG.info("addVpnInterfaceToOperational: Added VPN Interface {} on dpn {} vpn {} to operational datastore",
933 interfaceName, dpnId, vpnName);
936 // TODO Clean up the exception handling
937 @SuppressWarnings("checkstyle:IllegalCatch")
938 public void updateVpnInterfaceOnTepAdd(VpnInterfaceOpDataEntry vpnInterface,
939 StateTunnelList stateTunnelList,
940 TypedWriteTransaction<Configuration> writeConfigTxn,
941 TypedWriteTransaction<Operational> writeOperTxn) {
943 String srcTepIp = stateTunnelList.getSrcInfo().getTepIp().stringValue();
944 BigInteger srcDpnId = new BigInteger(stateTunnelList.getSrcInfo().getTepDeviceId());
945 AdjacenciesOp adjacencies = vpnInterface.augmentation(AdjacenciesOp.class);
946 List<Adjacency> adjList =
947 adjacencies != null && adjacencies.getAdjacency() != null ? adjacencies.getAdjacency() : emptyList();
948 if (adjList.isEmpty()) {
949 LOG.trace("updateVpnInterfaceOnTepAdd: Adjacencies are empty for vpnInterface {} on dpn {}",
950 vpnInterface, srcDpnId);
953 String prefix = null;
955 List<Adjacency> value = new ArrayList<>();
956 boolean isNextHopAddReqd = false;
957 String vpnName = vpnInterface.getVpnInstanceName();
958 long vpnId = vpnUtil.getVpnId(vpnName);
959 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
960 LOG.info("updateVpnInterfaceOnTepAdd: AdjacencyList for interface {} on dpn {} vpn {} is {}",
961 vpnInterface.getName(), vpnInterface.getDpnId(),
962 vpnInterface.getVpnInstanceName(), adjList);
963 for (Adjacency adj : adjList) {
964 String rd = adj.getVrfId();
965 rd = rd != null ? rd : vpnName;
966 prefix = adj.getIpAddress();
967 label = adj.getLabel();
968 List<String> nhList = Collections.singletonList(srcTepIp);
969 List<String> nextHopList = adj.getNextHopIpList();
970 // If TEP is added , update the nexthop of primary adjacency.
971 // Secondary adj nexthop is already pointing to primary adj IP address.
972 if (nextHopList == null || nextHopList.isEmpty()) {
973 isNextHopAddReqd = true;
976 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
977 value.add(new AdjacencyBuilder(adj).setNextHopIpList(nhList).build());
979 Optional<VrfEntry> vrfEntryOptional = FibHelper.getVrfEntry(dataBroker, primaryRd, prefix);
980 if (!vrfEntryOptional.isPresent()) {
983 nhList = FibHelper.getNextHopListFromRoutePaths(vrfEntryOptional.get());
984 if (!nhList.contains(srcTepIp)) {
985 nhList.add(srcTepIp);
986 isNextHopAddReqd = true;
991 if (isNextHopAddReqd) {
992 updateLabelMapper(label, nhList);
993 LOG.info("updateVpnInterfaceOnTepAdd: Updated label mapper : label {} dpn {} prefix {} nexthoplist {}"
994 + " vpn {} vpnid {} rd {} interface {}", label, srcDpnId , prefix, nhList,
995 vpnInterface.getVpnInstanceName(), vpnId, rd, vpnInterface.getName());
996 // Update the VRF entry with nextHop
997 fibManager.updateRoutePathForFibEntry(primaryRd, prefix, srcTepIp,
998 label, true, TransactionAdapter.toWriteTransaction(writeConfigTxn));
1000 //Get the list of VPN's importing this route(prefix) .
1001 // Then update the VRF entry with nhList
1002 List<VpnInstanceOpDataEntry> vpnsToImportRoute = vpnUtil.getVpnsImportingMyRoute(vpnName);
1003 for (VpnInstanceOpDataEntry vpn : vpnsToImportRoute) {
1004 String vpnRd = vpn.getVrfId();
1005 if (vpnRd != null) {
1006 fibManager.updateRoutePathForFibEntry(vpnRd, prefix,
1007 srcTepIp, label, true, TransactionAdapter.toWriteTransaction(writeConfigTxn));
1008 LOG.info("updateVpnInterfaceOnTepAdd: Exported route with rd {} prefix {} nhList {} label {}"
1009 + " interface {} dpn {} from vpn {} to VPN {} vpnRd {}", rd, prefix, nhList, label,
1010 vpnInterface.getName(), srcDpnId, vpnName,
1011 vpn.getVpnInstanceName(), vpnRd);
1014 // Advertise the prefix to BGP only for external vpn
1015 // since there is a nexthop change.
1017 if (!rd.equalsIgnoreCase(vpnName)) {
1018 bgpManager.advertisePrefix(rd, null /*macAddress*/, prefix, nhList,
1019 VrfEntry.EncapType.Mplsgre, (int)label, 0 /*evi*/, 0 /*l2vni*/,
1020 null /*gatewayMacAddress*/);
1022 LOG.info("updateVpnInterfaceOnTepAdd: Advertised rd {} prefix {} nhList {} label {}"
1023 + " for interface {} on dpn {} vpn {}", rd, prefix, nhList, label, vpnInterface.getName(),
1025 } catch (Exception ex) {
1026 LOG.error("updateVpnInterfaceOnTepAdd: Exception when advertising prefix {} nh {} label {}"
1027 + " on rd {} for interface {} on dpn {} vpn {}", prefix, nhList, label, rd,
1028 vpnInterface.getName(), srcDpnId, vpnName, ex);
1032 AdjacenciesOp aug = VpnUtil.getVpnInterfaceOpDataEntryAugmentation(value);
1033 VpnInterfaceOpDataEntry opInterface = new VpnInterfaceOpDataEntryBuilder(vpnInterface)
1034 .withKey(new VpnInterfaceOpDataEntryKey(vpnInterface.getName(), vpnName))
1035 .addAugmentation(AdjacenciesOp.class, aug).build();
1036 InstanceIdentifier<VpnInterfaceOpDataEntry> interfaceId =
1037 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(vpnInterface.getName(), vpnName);
1038 writeOperTxn.put(interfaceId, opInterface, CREATE_MISSING_PARENTS);
1039 LOG.info("updateVpnInterfaceOnTepAdd: interface {} updated successully on tep add on dpn {} vpn {}",
1040 vpnInterface.getName(), srcDpnId, vpnName);
1044 // TODO Clean up the exception handling
1045 @SuppressWarnings("checkstyle:IllegalCatch")
1046 public void updateVpnInterfaceOnTepDelete(VpnInterfaceOpDataEntry vpnInterface,
1047 StateTunnelList stateTunnelList,
1048 TypedWriteTransaction<Configuration> writeConfigTxn,
1049 TypedWriteTransaction<Operational> writeOperTxn) {
1051 AdjacenciesOp adjacencies = vpnInterface.augmentation(AdjacenciesOp.class);
1052 List<Adjacency> adjList = adjacencies != null ? adjacencies.getAdjacency() : new ArrayList<>();
1053 String prefix = null;
1055 boolean isNextHopRemoveReqd = false;
1056 String srcTepIp = stateTunnelList.getSrcInfo().getTepIp().stringValue();
1057 BigInteger srcDpnId = new BigInteger(stateTunnelList.getSrcInfo().getTepDeviceId());
1058 String vpnName = vpnInterface.getVpnInstanceName();
1059 long vpnId = vpnUtil.getVpnId(vpnName);
1060 String primaryRd = vpnUtil.getVpnRd(vpnName);
1061 if (adjList != null) {
1062 List<Adjacency> value = new ArrayList<>();
1063 LOG.info("updateVpnInterfaceOnTepDelete: AdjacencyList for interface {} on dpn {} vpn {} is {}",
1064 vpnInterface.getName(), vpnInterface.getDpnId(),
1065 vpnInterface.getVpnInstanceName(), adjList);
1066 for (Adjacency adj : adjList) {
1067 List<String> nhList = new ArrayList<>();
1068 String rd = adj.getVrfId();
1069 rd = rd != null ? rd : vpnName;
1070 prefix = adj.getIpAddress();
1071 List<String> nextHopList = adj.getNextHopIpList();
1072 label = adj.getLabel();
1073 if (nextHopList != null && !nextHopList.isEmpty()) {
1074 isNextHopRemoveReqd = true;
1076 // If TEP is deleted , remove the nexthop from primary adjacency.
1077 // Secondary adj nexthop will continue to point to primary adj IP address.
1078 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
1079 value.add(new AdjacencyBuilder(adj).setNextHopIpList(nhList).build());
1081 Optional<VrfEntry> vrfEntryOptional = FibHelper.getVrfEntry(dataBroker, primaryRd, prefix);
1082 if (!vrfEntryOptional.isPresent()) {
1085 nhList = FibHelper.getNextHopListFromRoutePaths(vrfEntryOptional.get());
1086 if (nhList.contains(srcTepIp)) {
1087 nhList.remove(srcTepIp);
1088 isNextHopRemoveReqd = true;
1093 if (isNextHopRemoveReqd) {
1094 updateLabelMapper(label, nhList);
1095 LOG.info("updateVpnInterfaceOnTepDelete: Updated label mapper : label {} dpn {} prefix {}"
1096 + " nexthoplist {} vpn {} vpnid {} rd {} interface {}", label, srcDpnId,
1097 prefix, nhList, vpnName,
1098 vpnId, rd, vpnInterface.getName());
1099 // Update the VRF entry with removed nextHop
1100 fibManager.updateRoutePathForFibEntry(primaryRd, prefix, srcTepIp,
1101 label, false, TransactionAdapter.toWriteTransaction(writeConfigTxn));
1103 //Get the list of VPN's importing this route(prefix) .
1104 // Then update the VRF entry with nhList
1105 List<VpnInstanceOpDataEntry> vpnsToImportRoute = vpnUtil.getVpnsImportingMyRoute(vpnName);
1106 for (VpnInstanceOpDataEntry vpn : vpnsToImportRoute) {
1107 String vpnRd = vpn.getVrfId();
1108 if (vpnRd != null) {
1109 fibManager.updateRoutePathForFibEntry(vpnRd, prefix,
1110 srcTepIp, label, false, TransactionAdapter.toWriteTransaction(writeConfigTxn));
1111 LOG.info("updateVpnInterfaceOnTepDelete: Exported route with rd {} prefix {} nhList {}"
1112 + " label {} interface {} dpn {} from vpn {} to VPN {} vpnRd {}", rd, prefix,
1113 nhList, label, vpnInterface.getName(), srcDpnId,
1115 vpn.getVpnInstanceName(), vpnRd);
1119 // Withdraw prefix from BGP only for external vpn.
1121 if (!rd.equalsIgnoreCase(vpnName)) {
1122 bgpManager.withdrawPrefix(rd, prefix);
1124 LOG.info("updateVpnInterfaceOnTepDelete: Withdrawn rd {} prefix {} nhList {} label {}"
1125 + " for interface {} on dpn {} vpn {}", rd, prefix, nhList, label,
1126 vpnInterface.getName(), srcDpnId,
1128 } catch (Exception ex) {
1129 LOG.error("updateVpnInterfaceOnTepDelete: Exception when withdrawing prefix {} nh {} label {}"
1130 + " on rd {} for interface {} on dpn {} vpn {}", prefix, nhList, label, rd,
1131 vpnInterface.getName(), srcDpnId, vpnName, ex);
1135 AdjacenciesOp aug = VpnUtil.getVpnInterfaceOpDataEntryAugmentation(value);
1136 VpnInterfaceOpDataEntry opInterface = new VpnInterfaceOpDataEntryBuilder(vpnInterface)
1137 .withKey(new VpnInterfaceOpDataEntryKey(vpnInterface.getName(), vpnName))
1138 .addAugmentation(AdjacenciesOp.class, aug).build();
1139 InstanceIdentifier<VpnInterfaceOpDataEntry> interfaceId =
1140 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(vpnInterface.getName(), vpnName);
1141 writeOperTxn.put(interfaceId, opInterface, CREATE_MISSING_PARENTS);
1142 LOG.info("updateVpnInterfaceOnTepDelete: interface {} updated successully on tep delete on dpn {} vpn {}",
1143 vpnInterface.getName(), srcDpnId, vpnName);
1147 private List<VpnInstanceOpDataEntry> getVpnsExportingMyRoute(final String vpnName) {
1148 List<VpnInstanceOpDataEntry> vpnsToExportRoute = new ArrayList<>();
1150 String vpnRd = vpnUtil.getVpnRd(vpnName);
1151 final VpnInstanceOpDataEntry vpnInstanceOpDataEntry = vpnUtil.getVpnInstanceOpData(vpnRd);
1152 if (vpnInstanceOpDataEntry == null) {
1153 LOG.debug("getVpnsExportingMyRoute: Could not retrieve vpn instance op data for {}"
1154 + " to check for vpns exporting the routes", vpnName);
1155 return vpnsToExportRoute;
1158 Predicate<VpnInstanceOpDataEntry> excludeVpn = input -> {
1159 if (input.getVpnInstanceName() == null) {
1160 LOG.error("getVpnsExportingMyRoute.excludeVpn: Received vpn instance with rd {} without a name",
1164 return !input.getVpnInstanceName().equals(vpnName);
1167 Predicate<VpnInstanceOpDataEntry> matchRTs = input -> {
1168 Iterable<String> commonRTs =
1169 VpnUtil.intersection(VpnUtil.getRts(vpnInstanceOpDataEntry, VpnTarget.VrfRTType.ImportExtcommunity),
1170 VpnUtil.getRts(input, VpnTarget.VrfRTType.ExportExtcommunity));
1171 return Iterators.size(commonRTs.iterator()) > 0;
1175 vpnUtil.getAllVpnInstanceOpData().stream().filter(excludeVpn).filter(matchRTs).collect(
1176 Collectors.toList());
1177 return vpnsToExportRoute;
1180 // TODO Clean up the exception handling
1181 @SuppressWarnings("checkstyle:IllegalCatch")
1182 void handleVpnsExportingRoutes(String vpnName, String vpnRd) {
1183 List<VpnInstanceOpDataEntry> vpnsToExportRoute = getVpnsExportingMyRoute(vpnName);
1184 for (VpnInstanceOpDataEntry vpn : vpnsToExportRoute) {
1185 List<VrfEntry> vrfEntries = vpnUtil.getAllVrfEntries(vpn.getVrfId());
1186 if (vrfEntries != null) {
1187 ListenableFutures.addErrorLogging(
1188 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, confTx -> {
1189 for (VrfEntry vrfEntry : vrfEntries) {
1191 if (!FibHelper.isControllerManagedNonInterVpnLinkRoute(
1192 RouteOrigin.value(vrfEntry.getOrigin()))) {
1193 LOG.info("handleVpnsExportingRoutes: vrfEntry with rd {} prefix {}"
1194 + " is not a controller managed non intervpn link route. Ignoring.",
1195 vpn.getVrfId(), vrfEntry.getDestPrefix());
1198 String prefix = vrfEntry.getDestPrefix();
1199 String gwMac = vrfEntry.getGatewayMacAddress();
1200 vrfEntry.nonnullRoutePaths().forEach(routePath -> {
1201 String nh = routePath.getNexthopAddress();
1202 int label = routePath.getLabel().intValue();
1203 if (FibHelper.isControllerManagedVpnInterfaceRoute(RouteOrigin.value(
1204 vrfEntry.getOrigin()))) {
1206 "handleVpnsExportingRoutesImporting: Importing fib entry rd {}"
1207 + " prefix {} nexthop {} label {} to vpn {} vpnRd {}",
1208 vpn.getVrfId(), prefix, nh, label, vpnName, vpnRd);
1209 fibManager.addOrUpdateFibEntry(vpnRd, null /*macAddress*/, prefix,
1210 Collections.singletonList(nh), VrfEntry.EncapType.Mplsgre, label,
1211 0 /*l3vni*/, gwMac, vpn.getVrfId(), RouteOrigin.SELF_IMPORTED,
1214 LOG.info("handleVpnsExportingRoutes: Importing subnet route fib entry"
1215 + " rd {} prefix {} nexthop {} label {} to vpn {} vpnRd {}",
1216 vpn.getVrfId(), prefix, nh, label, vpnName, vpnRd);
1217 SubnetRoute route = vrfEntry.augmentation(SubnetRoute.class);
1218 importSubnetRouteForNewVpn(vpnRd, prefix, nh, label, route, vpn.getVrfId(),
1222 } catch (RuntimeException e) {
1223 LOG.error("getNextHopAddressList: Exception occurred while importing route with rd {}"
1224 + " prefix {} routePaths {} to vpn {} vpnRd {}", vpn.getVrfId(),
1225 vrfEntry.getDestPrefix(), vrfEntry.getRoutePaths(), vpnName, vpnRd);
1228 }), LOG, "Error handing VPN exporting routes");
1230 LOG.info("getNextHopAddressList: No vrf entries to import from vpn {} with rd {} to vpn {} with rd {}",
1231 vpn.getVpnInstanceName(), vpn.getVrfId(), vpnName, vpnRd);
1237 public void remove(InstanceIdentifier<VpnInterface> identifier, VpnInterface vpnInterface) {
1238 LOG.trace("Received VpnInterface remove event: vpnInterface={}", vpnInterface);
1239 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class);
1240 final String interfaceName = key.getName();
1241 for (VpnInstanceNames vpnInterfaceVpnInstance : vpnInterface.nonnullVpnInstanceNames()) {
1242 String vpnName = vpnInterfaceVpnInstance.getVpnName();
1243 removeVpnInterfaceCall(identifier, vpnInterface, vpnName, interfaceName);
1247 private void removeVpnInterfaceCall(final InstanceIdentifier<VpnInterface> identifier,
1248 final VpnInterface vpnInterface, final String vpnName,
1249 final String interfaceName) {
1250 if (Boolean.TRUE.equals(vpnInterface.isRouterInterface())) {
1251 jobCoordinator.enqueueJob("VPNINTERFACE-" + vpnInterface.getName(), () -> {
1252 ListenableFuture<Void> future =
1253 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, confTx -> {
1254 deleteFibEntryForRouterInterface(vpnInterface, confTx, vpnName);
1255 LOG.info("remove: Router interface {} for vpn {}", interfaceName, vpnName);
1257 ListenableFutures.addErrorLogging(future, LOG, "Error removing call for interface {} on VPN {}",
1258 vpnInterface.getName(), vpnName);
1259 return Collections.singletonList(future);
1260 }, DJC_MAX_RETRIES);
1262 Interface interfaceState = InterfaceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceName);
1263 removeVpnInterfaceFromVpn(identifier, vpnInterface, vpnName, interfaceName, interfaceState);
1267 @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
1268 private void removeVpnInterfaceFromVpn(final InstanceIdentifier<VpnInterface> identifier,
1269 final VpnInterface vpnInterface, final String vpnName,
1270 final String interfaceName, final Interface interfaceState) {
1271 LOG.info("remove: VPN Interface remove event - intfName {} vpn {} dpn {}" ,vpnInterface.getName(),
1272 vpnName, vpnInterface.getDpnId());
1273 removeInterfaceFromUnprocessedList(identifier, vpnInterface);
1274 jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName,
1276 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
1277 ListenableFuture<Void> configFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1278 writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
1279 writeOperTxn -> futures.add(
1280 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
1281 LOG.info("remove: - intfName {} onto vpnName {} running config-driven",
1282 interfaceName, vpnName);
1285 String gwMacAddress;
1286 InstanceIdentifier<VpnInterfaceOpDataEntry> interfaceId =
1287 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
1288 Optional<VpnInterfaceOpDataEntry> optVpnInterface;
1290 optVpnInterface = SingleTransactionDataBroker.syncReadOptional(dataBroker,
1291 LogicalDatastoreType.OPERATIONAL, interfaceId);
1292 } catch (ReadFailedException e) {
1293 LOG.error("remove: Failed to read data store for interface {} vpn {}",
1294 interfaceName, vpnName);
1297 if (interfaceState != null) {
1299 dpId = InterfaceUtils.getDpIdFromInterface(interfaceState);
1300 } catch (NumberFormatException | IllegalStateException e) {
1301 LOG.error("remove: Unable to retrieve dpnId from interface operational"
1302 + " data store for interface {} on dpn {} for vpn {} Fetching"
1303 + " from vpn interface op data store. ", interfaceName,
1304 vpnInterface.getDpnId(), vpnName, e);
1305 dpId = BigInteger.ZERO;
1307 ifIndex = interfaceState.getIfIndex();
1308 gwMacAddress = interfaceState.getPhysAddress().getValue();
1310 LOG.info("remove: Interface state not available for {}. Trying to fetch data"
1311 + " from vpn interface op.", interfaceName);
1312 if (optVpnInterface.isPresent()) {
1313 VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
1314 dpId = vpnOpInterface.getDpnId();
1315 ifIndex = vpnOpInterface.getLportTag().intValue();
1316 gwMacAddress = vpnOpInterface.getGatewayMacAddress();
1318 LOG.error("remove: Handling removal of VPN interface {} for vpn {} skipped"
1319 + " as interfaceState and vpn interface op is not"
1320 + " available", interfaceName, vpnName);
1324 processVpnInterfaceDown(dpId, interfaceName, ifIndex, gwMacAddress,
1325 optVpnInterface.isPresent() ? optVpnInterface.get() : null, false,
1326 writeConfigTxn, writeOperTxn, writeInvTxn);
1328 "remove: Removal of vpn interface {} on dpn {} for vpn {} processed "
1330 interfaceName, vpnInterface.getDpnId(), vpnName);
1332 futures.add(configFuture);
1333 Futures.addCallback(configFuture, new PostVpnInterfaceWorker(
1334 interfaceName, false, "Config"), MoreExecutors.directExecutor());
1336 }, DJC_MAX_RETRIES);
1339 protected void processVpnInterfaceDown(BigInteger dpId,
1340 String interfaceName,
1343 VpnInterfaceOpDataEntry vpnOpInterface,
1344 boolean isInterfaceStateDown,
1345 TypedWriteTransaction<Configuration> writeConfigTxn,
1346 TypedWriteTransaction<Operational> writeOperTxn,
1347 TypedReadWriteTransaction<Configuration> writeInvTxn)
1348 throws ExecutionException, InterruptedException {
1349 if (vpnOpInterface == null) {
1350 LOG.error("processVpnInterfaceDown: Unable to process delete/down for interface {} on dpn {}"
1351 + " as it is not available in operational data store", interfaceName, dpId);
1354 final String vpnName = vpnOpInterface.getVpnInstanceName();
1355 InstanceIdentifier<VpnInterfaceOpDataEntry> identifier = VpnUtil.getVpnInterfaceOpDataEntryIdentifier(
1356 interfaceName, vpnName);
1357 if (!isInterfaceStateDown) {
1358 final long vpnId = vpnUtil.getVpnId(vpnName);
1359 vpnUtil.scheduleVpnInterfaceForRemoval(interfaceName, dpId, vpnName, null);
1360 final boolean isBgpVpnInternetVpn = vpnUtil.isBgpVpnInternet(vpnName);
1361 removeAdjacenciesFromVpn(dpId, lportTag, interfaceName, vpnName,
1362 vpnId, gwMac, writeConfigTxn, writeOperTxn, writeInvTxn);
1363 if (interfaceManager.isExternalInterface(interfaceName)) {
1364 processExternalVpnInterface(interfaceName, vpnName, dpId, lportTag,
1365 NwConstants.DEL_FLOW);
1367 if (!isBgpVpnInternetVpn) {
1368 vpnUtil.unbindService(interfaceName, isInterfaceStateDown);
1370 LOG.info("processVpnInterfaceDown: Unbound vpn service from interface {} on dpn {} for vpn {}"
1371 + " successful", interfaceName, dpId, vpnName);
1373 // Interface is retained in the DPN, but its Link Down.
1374 // Only withdraw the prefixes for this interface from BGP
1375 withdrawAdjacenciesForVpnFromBgp(identifier, vpnName, interfaceName, writeConfigTxn, writeOperTxn);
1379 private void removeAdjacenciesFromVpn(final BigInteger dpnId, final int lportTag, final String interfaceName,
1380 final String vpnName, final long vpnId, String gwMac,
1381 TypedWriteTransaction<Configuration> writeConfigTxn,
1382 TypedWriteTransaction<Operational> writeOperTxn,
1383 TypedReadWriteTransaction<Configuration> writeInvTxn)
1384 throws ExecutionException, InterruptedException {
1387 InstanceIdentifier<VpnInterfaceOpDataEntry> identifier = VpnUtil
1388 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
1389 InstanceIdentifier<AdjacenciesOp> path = identifier.augmentation(AdjacenciesOp.class);
1390 Optional<AdjacenciesOp> adjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker,
1391 LogicalDatastoreType.OPERATIONAL, path);
1392 boolean isNonPrimaryAdjIp = Boolean.FALSE;
1393 String primaryRd = vpnUtil.getVpnRd(vpnName);
1394 LOG.info("removeAdjacenciesFromVpn: For interface {} on dpn {} RD recovered for vpn {} as rd {}",
1395 interfaceName, dpnId, vpnName, primaryRd);
1396 if (adjacencies.isPresent() && adjacencies.get().getAdjacency() != null
1397 && !adjacencies.get().getAdjacency().isEmpty()) {
1398 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
1399 LOG.info("removeAdjacenciesFromVpn: NextHops for interface {} on dpn {} for vpn {} are {}",
1400 interfaceName, dpnId, vpnName, nextHops);
1401 for (Adjacency nextHop : nextHops) {
1402 if (nextHop.isPhysNetworkFunc()) {
1403 LOG.info("removeAdjacenciesFromVpn: Removing PNF FIB entry rd {} prefix {}",
1404 nextHop.getSubnetId().getValue(), nextHop.getIpAddress());
1405 fibManager.removeFibEntry(nextHop.getSubnetId().getValue(), nextHop.getIpAddress(),
1406 null/*writeCfgTxn*/);
1408 String rd = nextHop.getVrfId();
1409 List<String> nhList;
1410 if (nextHop.getAdjacencyType() != AdjacencyType.PrimaryAdjacency) {
1411 nhList = getNextHopForNonPrimaryAdjacency(nextHop, vpnName, dpnId, interfaceName);
1412 isNonPrimaryAdjIp = Boolean.TRUE;
1414 // This is a primary adjacency
1415 nhList = nextHop.getNextHopIpList() != null ? nextHop.getNextHopIpList()
1417 removeGwMacAndArpResponderFlows(nextHop, vpnId, dpnId, lportTag, gwMac,
1418 interfaceName, writeInvTxn);
1420 if (!nhList.isEmpty()) {
1421 if (Objects.equals(primaryRd, vpnName)) {
1422 //this is an internal vpn - the rd is assigned to the vpn instance name;
1423 //remove from FIB directly
1424 nhList.forEach(removeAdjacencyFromInternalVpn(nextHop, vpnName,
1425 interfaceName, dpnId, writeConfigTxn, writeOperTxn));
1427 removeAdjacencyFromBgpvpn(nextHop, nhList, vpnName, primaryRd, dpnId, rd,
1428 interfaceName, writeConfigTxn, writeOperTxn);
1431 LOG.error("removeAdjacenciesFromVpn: nextHop empty for ip {} rd {} adjacencyType {}"
1432 + " interface {}", nextHop.getIpAddress(), rd,
1433 nextHop.getAdjacencyType().toString(), interfaceName);
1434 bgpManager.withdrawPrefixIfPresent(rd, nextHop.getIpAddress());
1435 fibManager.removeFibEntry(primaryRd, nextHop.getIpAddress(), writeConfigTxn);
1438 String ip = nextHop.getIpAddress().split("/")[0];
1439 LearntVpnVipToPort vpnVipToPort = vpnUtil.getLearntVpnVipToPort(vpnName, ip);
1440 if (vpnVipToPort != null && vpnVipToPort.getPortName().equals(interfaceName)) {
1441 vpnUtil.removeLearntVpnVipToPort(vpnName, ip, null);
1442 LOG.info("removeAdjacenciesFromVpn: VpnInterfaceManager removed LearntVpnVipToPort entry"
1443 + " for Interface {} ip {} on dpn {} for vpn {}",
1444 vpnVipToPort.getPortName(), ip, dpnId, vpnName);
1446 // Remove the MIP-IP from VpnPortIpToPort.
1447 if (isNonPrimaryAdjIp) {
1448 VpnPortipToPort persistedIp = vpnUtil.getVpnPortipToPort(vpnName, ip);
1449 if (persistedIp != null && persistedIp.isLearntIp()
1450 && persistedIp.getPortName().equals(interfaceName)) {
1451 VpnUtil.removeVpnPortFixedIpToPort(dataBroker, vpnName, ip, null);
1453 "removeAdjacenciesFromVpn: Learnt-IP: {} interface {} of vpn {} removed "
1454 + "from VpnPortipToPort",
1455 persistedIp.getPortFixedip(), persistedIp.getPortName(), vpnName);
1458 VpnPortipToPort vpnPortipToPort = vpnUtil.getNeutronPortFromVpnPortFixedIp(vpnName, ip);
1459 if (vpnPortipToPort != null) {
1460 VpnUtil.removeVpnPortFixedIpToPort(dataBroker, vpnName, ip, null);
1461 LOG.info("removeAdjacenciesFromVpn: VpnInterfaceManager removed vpnPortipToPort entry for "
1462 + "Interface {} ip {} on dpn {} for vpn {}",
1463 vpnPortipToPort.getPortName(), ip, dpnId, vpnName);
1467 // this vpn interface has no more adjacency left, so clean up the vpn interface from Operational DS
1468 LOG.info("removeAdjacenciesFromVpn: Vpn Interface {} on vpn {} dpn {} has no adjacencies."
1469 + " Removing it.", interfaceName, vpnName, dpnId);
1470 writeOperTxn.delete(identifier);
1472 } catch (ReadFailedException e) {
1473 LOG.error("removeAdjacenciesFromVpn: Failed to read data store for interface {} dpn {} vpn {}",
1474 interfaceName, dpnId, vpnName);
1478 private Consumer<String> removeAdjacencyFromInternalVpn(Adjacency nextHop, String vpnName,
1479 String interfaceName, BigInteger dpnId,
1480 TypedWriteTransaction<Configuration> writeConfigTxn,
1481 TypedWriteTransaction<Operational> writeOperTx) {
1483 String primaryRd = vpnUtil.getVpnRd(vpnName);
1484 String prefix = nextHop.getIpAddress();
1485 String vpnNamePrefixKey = VpnUtil.getVpnNamePrefixKey(vpnName, prefix);
1486 LOG.info("remove adjacencies for nexthop {} vpnName {} interfaceName {} dpnId {}",
1487 nextHop, vpnName, interfaceName, dpnId);
1488 // FIXME: separate this out somehow?
1489 final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnNamePrefixKey);
1492 if (vpnUtil.removeOrUpdateDSForExtraRoute(vpnName, primaryRd, dpnId.toString(), interfaceName,
1493 prefix, nextHop.getNextHopIpList().get(0), nh, writeOperTx)) {
1494 //If extra-route is present behind at least one VM, then do not remove or update
1495 //fib entry for route-path representing that CSS nexthop, just update vpntoextraroute and
1496 //prefixtointerface DS
1499 fibManager.removeOrUpdateFibEntry(vpnName, nextHop.getIpAddress(), nh,
1504 LOG.info("removeAdjacenciesFromVpn: removed/updated FIB with rd {} prefix {}"
1505 + " nexthop {} for interface {} on dpn {} for internal vpn {}",
1506 vpnName, nextHop.getIpAddress(), nh, interfaceName, dpnId, vpnName);
1510 private void removeAdjacencyFromBgpvpn(Adjacency nextHop, List<String> nhList, String vpnName, String primaryRd,
1511 BigInteger dpnId, String rd, String interfaceName,
1512 TypedWriteTransaction<Configuration> writeConfigTxn,
1513 TypedWriteTransaction<Operational> writeOperTx) {
1514 List<VpnInstanceOpDataEntry> vpnsToImportRoute =
1515 vpnUtil.getVpnsImportingMyRoute(vpnName);
1516 nhList.forEach((nh) -> {
1517 //IRT: remove routes from other vpns importing it
1518 vpnManager.removePrefixFromBGP(vpnName, primaryRd, rd, interfaceName, nextHop.getIpAddress(),
1519 nextHop.getNextHopIpList().get(0), nh, dpnId, writeConfigTxn, writeOperTx);
1520 for (VpnInstanceOpDataEntry vpn : vpnsToImportRoute) {
1521 String vpnRd = vpn.getVrfId();
1522 if (vpnRd != null) {
1523 fibManager.removeOrUpdateFibEntry(vpnRd,
1524 nextHop.getIpAddress(), nh, writeConfigTxn);
1525 LOG.info("removeAdjacenciesFromVpn: Removed Exported route with rd {}"
1526 + " prefix {} nextHop {} from VPN {} parentVpn {}"
1527 + " for interface {} on dpn {}", vpnRd, nextHop.getIpAddress(), nh,
1528 vpn.getVpnInstanceName(), vpnName, interfaceName, dpnId);
1534 private void removeGwMacAndArpResponderFlows(Adjacency nextHop, long vpnId, BigInteger dpnId,
1535 int lportTag, String gwMac, String interfaceName,
1536 TypedReadWriteTransaction<Configuration> writeInvTxn)
1537 throws ExecutionException, InterruptedException {
1538 final Uuid subnetId = nextHop.getSubnetId();
1539 if (nextHop.getSubnetGatewayMacAddress() == null) {
1540 // A valid mac-address was not available for this subnet-gateway-ip
1541 // So a connected-mac-address was used for this subnet and we need
1542 // to remove the flows for the same here from the L3_GW_MAC_TABLE.
1543 vpnUtil.setupGwMacIfExternalVpn(dpnId, interfaceName, vpnId, writeInvTxn, NwConstants.DEL_FLOW, gwMac);
1545 arpResponderHandler.removeArpResponderFlow(dpnId, lportTag, interfaceName, nextHop.getSubnetGatewayIp(),
1549 private List<String> getNextHopForNonPrimaryAdjacency(Adjacency nextHop, String vpnName, BigInteger dpnId,
1550 String interfaceName) {
1551 // This is either an extra-route (or) a learned IP via subnet-route
1552 List<String> nhList = null;
1553 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId);
1554 if (nextHopIp == null || nextHopIp.isEmpty()) {
1555 LOG.error("removeAdjacenciesFromVpn: Unable to obtain nextHopIp for"
1556 + " extra-route/learned-route in rd {} prefix {} interface {} on dpn {}"
1557 + " for vpn {}", nextHop.getVrfId(), nextHop.getIpAddress(), interfaceName, dpnId,
1559 nhList = emptyList();
1561 nhList = Collections.singletonList(nextHopIp);
1566 private Optional<String> getMacAddressForSubnetIp(String vpnName, String ifName, String ipAddress) {
1567 VpnPortipToPort gwPort = vpnUtil.getNeutronPortFromVpnPortFixedIp(vpnName, ipAddress);
1568 //Check if a router gateway interface is available for the subnet gw is so then use Router interface
1569 // else use connected interface
1570 if (gwPort != null && gwPort.isSubnetIp()) {
1571 LOG.info("getGatewayMacAddressForSubnetIp: Retrieved gw Mac as {} for ip {} interface {} vpn {}",
1572 gwPort.getMacAddress(), ipAddress, ifName, vpnName);
1573 return Optional.of(gwPort.getMacAddress());
1575 return Optional.absent();
1579 protected void update(final InstanceIdentifier<VpnInterface> identifier, final VpnInterface original,
1580 final VpnInterface update) {
1581 LOG.trace("Received VpnInterface update event: original={}, update={}", original, update);
1582 LOG.info("update: VPN Interface update event - intfName {} on dpn {} oldVpn {} newVpn {}", update.getName(),
1583 update.getDpnId(), original.getVpnInstanceNames(), update.getVpnInstanceNames());
1584 final String vpnInterfaceName = update.getName();
1585 final BigInteger dpnId = InterfaceUtils.getDpnForInterface(ifaceMgrRpcService, vpnInterfaceName);
1586 LOG.info("VPN Interface update event - intfName {}", vpnInterfaceName);
1587 //handles switching between <internal VPN - external VPN>
1588 jobCoordinator.enqueueJob("VPNINTERFACE-" + vpnInterfaceName, () -> {
1589 List<ListenableFuture<Void>> futures = new ArrayList<>();
1590 if (handleVpnInstanceUpdateForVpnInterface(identifier, original, update, futures)) {
1591 LOG.info("update: handled Instance update for VPNInterface {} on dpn {} from oldVpn(s) {} "
1592 + "to newVpn(s) {}",
1593 original.getName(), dpnId,
1594 VpnHelper.getVpnInterfaceVpnInstanceNamesString(original.getVpnInstanceNames()),
1595 VpnHelper.getVpnInterfaceVpnInstanceNamesString(update.getVpnInstanceNames()));
1598 updateVpnInstanceAdjChange(original, update, vpnInterfaceName, futures);
1603 private boolean handleVpnInstanceUpdateForVpnInterface(InstanceIdentifier<VpnInterface> identifier,
1604 VpnInterface original, VpnInterface update,
1605 List<ListenableFuture<Void>> futures) {
1606 boolean isVpnInstanceUpdate = false;
1607 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class);
1608 final String interfaceName = key.getName();
1609 List<String> oldVpnList = VpnUtil.getVpnListForVpnInterface(original);
1610 List<String> oldVpnListCopy = new ArrayList<>();
1611 oldVpnListCopy.addAll(oldVpnList);
1612 List<String> newVpnList = VpnUtil.getVpnListForVpnInterface(update);
1613 List<String> newVpnListCopy = new ArrayList<>();
1614 newVpnListCopy.addAll(newVpnList);
1616 oldVpnList.removeAll(newVpnList);
1617 newVpnList.removeAll(oldVpnListCopy);
1618 //This block will execute only on if there is a change in the VPN Instance.
1619 if (!oldVpnList.isEmpty() || !newVpnList.isEmpty()) {
1621 * Internet BGP-VPN Instance update with single router:
1622 * ====================================================
1623 * In this case single VPN Interface will be part of maximum 2 VPN Instance only.
1624 * 1st VPN Instance : router VPN or external BGP-VPN.
1625 * 2nd VPN Instance : Internet BGP-VPN(router-gw update/delete) for public network access.
1627 * VPN Instance UPDATE:
1628 * oldVpnList = 0 and newVpnList = 1 (Internet BGP-VPN)
1629 * oldVpnList = 1 and newVpnList = 0 (Internet BGP-VPN)
1631 * External BGP-VPN Instance update with single router:
1632 * ====================================================
1633 * In this case single VPN interface will be part of maximum 1 VPN Instance only.
1635 * Updated VPN Instance will be always either internal router VPN to
1636 * external BGP-VPN or external BGP-VPN to internal router VPN swap.
1638 * VPN Instance UPDATE:
1639 * oldVpnList = 1 and newVpnList = 1 (router VPN to Ext-BGPVPN)
1640 * oldVpnList = 1 and newVpnList = 1 (Ext-BGPVPN to router VPN)
1642 * Dual Router VPN Instance Update:
1643 * ================================
1644 * In this case single VPN interface will be part of maximum 3 VPN Instance only.
1646 * 1st VPN Instance : router VPN or external BGP-VPN-1.
1647 * 2nd VPN Instance : router VPN or external BGP-VPN-2.
1648 * 3rd VPN Instance : Internet BGP-VPN(router-gw update/delete) for public network access.
1650 * Dual Router --> Associated with common external BGP-VPN Instance.
1651 * 1st router and 2nd router are getting associated with single External BGP-VPN
1652 * 1) add 1st router to external bgpvpn --> oldVpnList=1, newVpnList=1;
1653 * 2) add 2nd router to the same external bgpvpn --> oldVpnList=1, newVpnList=0
1654 * In this case, we need to call removeVpnInterfaceCall() followed by addVpnInterfaceCall()
1658 isVpnInstanceUpdate = true;
1659 if (VpnUtil.isDualRouterVpnUpdate(oldVpnListCopy, newVpnListCopy)) {
1660 if ((oldVpnListCopy.size() == 2 || oldVpnListCopy.size() == 3)
1661 && oldVpnList.size() == 1 && newVpnList.isEmpty()) {
1662 //Identify the external BGP-VPN Instance and pass that value as newVpnList
1663 List<String> externalBgpVpnList = new ArrayList<>();
1664 for (String newVpnName : newVpnListCopy) {
1665 String primaryRd = vpnUtil.getPrimaryRd(newVpnName);
1666 VpnInstanceOpDataEntry vpnInstanceOpDataEntry = vpnUtil.getVpnInstanceOpData(primaryRd);
1667 if (vpnInstanceOpDataEntry.getBgpvpnType() == VpnInstanceOpDataEntry
1668 .BgpvpnType.BGPVPNExternal) {
1669 externalBgpVpnList.add(newVpnName);
1673 //This call will execute removeVpnInterfaceCall() followed by addVpnInterfaceCall()
1674 updateVpnInstanceChange(identifier, interfaceName, original, update, oldVpnList,
1675 externalBgpVpnList, oldVpnListCopy, futures);
1677 } else if ((oldVpnListCopy.size() == 2 || oldVpnListCopy.size() == 3)
1678 && oldVpnList.isEmpty() && newVpnList.size() == 1) {
1679 //Identify the router VPN Instance and pass that value as oldVpnList
1680 List<String> routerVpnList = new ArrayList<>();
1681 for (String newVpnName : newVpnListCopy) {
1682 String primaryRd = vpnUtil.getPrimaryRd(newVpnName);
1683 VpnInstanceOpDataEntry vpnInstanceOpDataEntry = vpnUtil.getVpnInstanceOpData(primaryRd);
1684 if (vpnInstanceOpDataEntry.getBgpvpnType() == VpnInstanceOpDataEntry
1686 routerVpnList.add(newVpnName);
1690 //This call will execute removeVpnInterfaceCall() followed by addVpnInterfaceCall()
1691 updateVpnInstanceChange(identifier, interfaceName, original, update, routerVpnList,
1692 newVpnList, oldVpnListCopy, futures);
1695 //Handle remaining use cases.
1696 updateVpnInstanceChange(identifier, interfaceName, original, update, oldVpnList, newVpnList,
1697 oldVpnListCopy, futures);
1700 updateVpnInstanceChange(identifier, interfaceName, original, update, oldVpnList, newVpnList,
1701 oldVpnListCopy, futures);
1704 return isVpnInstanceUpdate;
1707 private void updateVpnInstanceChange(InstanceIdentifier<VpnInterface> identifier, String interfaceName,
1708 VpnInterface original, VpnInterface update, List<String> oldVpnList,
1709 List<String> newVpnList, List<String> oldVpnListCopy,
1710 List<ListenableFuture<Void>> futures) {
1711 final Adjacencies origAdjs = original.augmentation(Adjacencies.class);
1712 final List<Adjacency> oldAdjs = origAdjs != null && origAdjs.getAdjacency() != null
1713 ? origAdjs.getAdjacency() : new ArrayList<>();
1714 final Adjacencies updateAdjs = update.augmentation(Adjacencies.class);
1715 final List<Adjacency> newAdjs = updateAdjs != null && updateAdjs.getAdjacency() != null
1716 ? updateAdjs.getAdjacency() : new ArrayList<>();
1718 boolean isOldVpnRemoveCallExecuted = false;
1719 for (String oldVpnName : oldVpnList) {
1720 LOG.info("updateVpnInstanceChange: VPN Interface update event - intfName {} "
1721 + "remove from vpnName {} ", interfaceName, oldVpnName);
1722 removeVpnInterfaceCall(identifier, original, oldVpnName, interfaceName);
1723 LOG.info("updateVpnInstanceChange: Processed Remove for update on VPNInterface"
1724 + " {} upon VPN update from old vpn {} to newVpn(s) {}", interfaceName, oldVpnName,
1726 isOldVpnRemoveCallExecuted = true;
1728 //Wait for previous interface bindings to be removed
1729 if (isOldVpnRemoveCallExecuted && !newVpnList.isEmpty()) {
1732 } catch (InterruptedException e) {
1736 for (String newVpnName : newVpnList) {
1737 String primaryRd = vpnUtil.getPrimaryRd(newVpnName);
1738 if (!vpnUtil.isVpnPendingDelete(primaryRd)) {
1739 LOG.info("updateVpnInstanceChange: VPN Interface update event - intfName {} "
1740 + "onto vpnName {} ", interfaceName, newVpnName);
1741 addVpnInterfaceCall(identifier, update, oldAdjs, newAdjs, newVpnName);
1742 LOG.info("updateVpnInstanceChange: Processed Add for update on VPNInterface {}"
1743 + "from oldVpn(s) {} to newVpn {} ",
1744 interfaceName, oldVpnListCopy, newVpnName);
1745 /* This block will execute only if V6 subnet is associated with internet BGP-VPN.
1747 * In Dual stack network, first V4 subnet only attached to router and router is associated
1748 * with internet BGP-VPN(router-gw). At this point VPN interface is having only router vpn instance.
1749 * Later V6 subnet is added to router, at this point existing VPN interface will get updated
1750 * with Internet BGP-VPN instance(Note: Internet BGP-VPN Instance update in vpn interface
1751 * is applicable for only on V6 subnet is added to router). newVpnList = Contains only Internet
1752 * BGP-VPN Instance. So we required V6 primary adjacency info needs to be populated onto
1753 * router VPN as well as Internet BGP-VPN.
1755 * addVpnInterfaceCall() --> It will create V6 Adj onto Internet BGP-VPN only.
1756 * updateVpnInstanceAdjChange() --> This method call is needed for second primary V6 Adj
1757 * update in existing router VPN instance.
1759 if (vpnUtil.isBgpVpnInternet(newVpnName)) {
1760 LOG.info("updateVpnInstanceChange: VPN Interface {} with new Adjacency {} in existing "
1761 + "VPN instance {}", interfaceName, newAdjs, original.getVpnInstanceNames());
1762 updateVpnInstanceAdjChange(original, update, interfaceName, futures);
1768 private List<ListenableFuture<Void>> updateVpnInstanceAdjChange(VpnInterface original, VpnInterface update,
1769 String vpnInterfaceName,
1770 List<ListenableFuture<Void>> futures) {
1771 final Adjacencies origAdjs = original.augmentation(Adjacencies.class);
1772 final List<Adjacency> oldAdjs = origAdjs != null && origAdjs.getAdjacency()
1773 != null ? origAdjs.getAdjacency() : new ArrayList<>();
1774 final Adjacencies updateAdjs = update.augmentation(Adjacencies.class);
1775 final List<Adjacency> newAdjs = updateAdjs != null && updateAdjs.getAdjacency()
1776 != null ? updateAdjs.getAdjacency() : new ArrayList<>();
1778 final BigInteger dpnId = InterfaceUtils.getDpnForInterface(ifaceMgrRpcService, vpnInterfaceName);
1779 for (VpnInstanceNames vpnInterfaceVpnInstance : update.nonnullVpnInstanceNames()) {
1780 String newVpnName = vpnInterfaceVpnInstance.getVpnName();
1781 List<Adjacency> copyNewAdjs = new ArrayList<>(newAdjs);
1782 List<Adjacency> copyOldAdjs = new ArrayList<>(oldAdjs);
1783 String primaryRd = vpnUtil.getPrimaryRd(newVpnName);
1784 if (!vpnUtil.isVpnPendingDelete(primaryRd)) {
1785 // TODO Deal with sequencing — the config tx must only submitted if the oper tx goes in
1786 //set of prefix used as entry in prefix-to-interface datastore
1787 // is prerequisite for refresh Fib to avoid race condition leading to missing remote next hop
1788 // in bucket actions on bgp-vpn delete
1789 Set<String> prefixListForRefreshFib = new HashSet<>();
1790 ListenableFuture<Void> configTxFuture = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
1791 confTx -> futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
1793 InstanceIdentifier<VpnInterfaceOpDataEntry> vpnInterfaceOpIdentifier =
1794 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(vpnInterfaceName, newVpnName);
1795 LOG.info("VPN Interface update event-intfName {} onto vpnName {} running config-driven",
1796 update.getName(), newVpnName);
1797 //handle both addition and removal of adjacencies
1798 // currently, new adjacency may be an extra route
1799 boolean isBgpVpnInternetVpn = vpnUtil.isBgpVpnInternet(newVpnName);
1800 if (!oldAdjs.equals(newAdjs)) {
1801 for (Adjacency adj : copyNewAdjs) {
1802 if (copyOldAdjs.contains(adj)) {
1803 copyOldAdjs.remove(adj);
1805 // add new adjacency
1806 if (!isBgpVpnInternetVpn || vpnUtil.isAdjacencyEligibleToVpnInternet(adj)) {
1807 addNewAdjToVpnInterface(vpnInterfaceOpIdentifier, primaryRd, adj,
1808 dpnId, operTx, confTx, confTx, prefixListForRefreshFib);
1810 LOG.info("update: new Adjacency {} with nextHop {} label {} subnet {} "
1811 + " added to vpn interface {} on vpn {} dpnId {}",
1812 adj.getIpAddress(), adj.getNextHopIpList(), adj.getLabel(),
1813 adj.getSubnetId(), update.getName(), newVpnName, dpnId);
1816 for (Adjacency adj : copyOldAdjs) {
1817 if (!isBgpVpnInternetVpn || vpnUtil.isAdjacencyEligibleToVpnInternet(adj)) {
1818 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency
1819 && !adj.isPhysNetworkFunc()) {
1820 delAdjFromVpnInterface(vpnInterfaceOpIdentifier, adj, dpnId, operTx,
1823 String vpnRd = vpnUtil.getVpnRd(newVpnName);
1824 LOG.debug("update: remove prefix {} from the FIB and BGP entry "
1825 + "for the Vpn-Rd {} ", adj.getIpAddress(), vpnRd);
1827 fibManager.removeFibEntry(vpnRd, adj.getIpAddress(), confTx);
1828 if (vpnRd != null && !vpnRd.equalsIgnoreCase(newVpnName)) {
1829 bgpManager.withdrawPrefix(vpnRd, adj.getIpAddress());
1832 delAdjFromVpnInterface(vpnInterfaceOpIdentifier, adj, dpnId,
1836 LOG.info("update: Adjacency {} with nextHop {} label {} subnet {} removed from"
1837 + " vpn interface {} on vpn {}", adj.getIpAddress(), adj.getNextHopIpList(),
1838 adj.getLabel(), adj.getSubnetId(), update.getName(), newVpnName);
1842 Futures.addCallback(configTxFuture, new VpnInterfaceCallBackHandler(primaryRd, prefixListForRefreshFib),
1843 MoreExecutors.directExecutor());
1844 futures.add(configTxFuture);
1845 for (ListenableFuture<Void> future : futures) {
1846 ListenableFutures.addErrorLogging(future, LOG, "update: failed for interface {} on vpn {}",
1847 update.getName(), update.getVpnInstanceNames());
1850 LOG.error("update: Ignoring update of vpnInterface {}, as newVpnInstance {} with primaryRd {}"
1851 + " is already marked for deletion", vpnInterfaceName, newVpnName, primaryRd);
1857 private void updateLabelMapper(Long label, List<String> nextHopIpList) {
1858 final String labelStr = Preconditions.checkNotNull(label, "updateLabelMapper: label cannot be null or empty!")
1860 // FIXME: separate this out somehow?
1861 final ReentrantLock lock = JvmGlobalLocks.getLockForString(labelStr);
1864 InstanceIdentifier<LabelRouteInfo> lriIid = InstanceIdentifier.builder(LabelRouteMap.class)
1865 .child(LabelRouteInfo.class, new LabelRouteInfoKey(label)).build();
1866 Optional<LabelRouteInfo> opResult = SingleTransactionDataBroker.syncReadOptional(dataBroker,
1867 LogicalDatastoreType.OPERATIONAL, lriIid);
1868 if (opResult.isPresent()) {
1869 LabelRouteInfo labelRouteInfo =
1870 new LabelRouteInfoBuilder(opResult.get()).setNextHopIpList(nextHopIpList).build();
1871 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, lriIid,
1872 labelRouteInfo, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
1874 LOG.info("updateLabelMapper: Updated label rotue info for label {} with nextHopList {}", label,
1876 } catch (ReadFailedException e) {
1877 LOG.error("updateLabelMapper: Failed to read data store for label {} nexthopList {}", label,
1879 } catch (TransactionCommitFailedException e) {
1880 LOG.error("updateLabelMapper: Failed to commit to data store for label {} nexthopList {}", label,
1887 public synchronized void importSubnetRouteForNewVpn(String rd, String prefix, String nextHop, int label,
1888 SubnetRoute route, String parentVpnRd, TypedWriteTransaction<Configuration> writeConfigTxn) {
1890 RouteOrigin origin = RouteOrigin.SELF_IMPORTED;
1891 VrfEntry vrfEntry = FibHelper.getVrfEntryBuilder(prefix, label, nextHop, origin, parentVpnRd)
1892 .addAugmentation(SubnetRoute.class, route).build();
1893 List<VrfEntry> vrfEntryList = Collections.singletonList(vrfEntry);
1894 InstanceIdentifierBuilder<VrfTables> idBuilder =
1895 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
1896 InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
1897 VrfTables vrfTableNew = new VrfTablesBuilder().setRouteDistinguisher(rd).setVrfEntry(vrfEntryList).build();
1898 if (writeConfigTxn != null) {
1899 writeConfigTxn.merge(vrfTableId, vrfTableNew, CREATE_MISSING_PARENTS);
1901 vpnUtil.syncUpdate(LogicalDatastoreType.CONFIGURATION, vrfTableId, vrfTableNew);
1903 LOG.info("SUBNETROUTE: importSubnetRouteForNewVpn: Created vrfEntry for rd {} prefix {} nexthop {} label {}"
1904 + " and elantag {}", rd, prefix, nextHop, label, route.getElantag());
1907 protected void addNewAdjToVpnInterface(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, String primaryRd,
1908 Adjacency adj, BigInteger dpnId,
1909 TypedWriteTransaction<Operational> writeOperTxn,
1910 TypedWriteTransaction<Configuration> writeConfigTxn,
1911 TypedReadWriteTransaction<Configuration> writeInvTxn,
1912 Set<String> prefixListForRefreshFib)
1913 throws ExecutionException, InterruptedException {
1914 String interfaceName = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class).getName();
1915 String configVpnName = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class).getVpnInstanceName();
1917 Optional<VpnInterfaceOpDataEntry> optVpnInterface = SingleTransactionDataBroker
1918 .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, identifier);
1919 if (optVpnInterface.isPresent()) {
1920 VpnInterfaceOpDataEntry currVpnIntf = optVpnInterface.get();
1921 String prefix = VpnUtil.getIpPrefix(adj.getIpAddress());
1922 String vpnName = currVpnIntf.getVpnInstanceName();
1923 VpnInstanceOpDataEntry vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(primaryRd);
1924 InstanceIdentifier<AdjacenciesOp> adjPath = identifier.augmentation(AdjacenciesOp.class);
1925 Optional<AdjacenciesOp> optAdjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker,
1926 LogicalDatastoreType.OPERATIONAL, adjPath);
1927 boolean isL3VpnOverVxLan = VpnUtil.isL3VpnOverVxLan(vpnInstanceOpData.getL3vni());
1928 VrfEntry.EncapType encapType = VpnUtil.getEncapType(isL3VpnOverVxLan);
1929 long l3vni = vpnInstanceOpData.getL3vni() == null ? 0L : vpnInstanceOpData.getL3vni();
1930 VpnPopulator populator = L3vpnRegistry.getRegisteredPopulator(encapType);
1931 List<Adjacency> adjacencies = new ArrayList<>();
1932 if (optAdjacencies.isPresent() && optAdjacencies.get().getAdjacency() != null) {
1933 adjacencies.addAll(optAdjacencies.get().getAdjacency());
1935 long vpnId = vpnUtil.getVpnId(vpnName);
1936 L3vpnInput input = new L3vpnInput().setNextHop(adj).setVpnName(vpnName)
1937 .setInterfaceName(currVpnIntf.getName()).setPrimaryRd(primaryRd).setRd(primaryRd);
1938 Adjacency operationalAdjacency = null;
1939 //Handling dual stack neutron port primary adjacency
1940 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency && !adj.isPhysNetworkFunc()) {
1941 LOG.trace("addNewAdjToVpnInterface: Adding prefix {} to existing interface {} for vpn {}", prefix,
1942 currVpnIntf.getName(), vpnName);
1943 Interface interfaceState = InterfaceUtils.getInterfaceStateFromOperDS(dataBroker,
1944 currVpnIntf.getName());
1945 if (interfaceState != null) {
1946 processVpnInterfaceAdjacencies(dpnId, currVpnIntf.getLportTag().intValue(), vpnName, primaryRd,
1947 currVpnIntf.getName(), vpnId, writeConfigTxn, writeOperTxn, writeInvTxn, interfaceState,
1948 prefixListForRefreshFib);
1951 if (adj.getNextHopIpList() != null && !adj.getNextHopIpList().isEmpty()
1952 && adj.getAdjacencyType() != AdjacencyType.PrimaryAdjacency) {
1953 RouteOrigin origin = adj.getAdjacencyType() == AdjacencyType.LearntIp ? RouteOrigin.DYNAMIC
1954 : RouteOrigin.STATIC;
1955 String nh = adj.getNextHopIpList().get(0);
1956 String vpnPrefixKey = VpnUtil.getVpnNamePrefixKey(vpnName, prefix);
1957 // FIXME: separate out to somehow?
1958 final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnPrefixKey);
1961 java.util.Optional<String> rdToAllocate = vpnUtil.allocateRdForExtraRouteAndUpdateUsedRdsMap(
1962 vpnId, null, prefix, vpnName, nh, dpnId);
1963 if (rdToAllocate.isPresent()) {
1964 input.setRd(rdToAllocate.get());
1965 operationalAdjacency = populator.createOperationalAdjacency(input);
1966 int label = operationalAdjacency.getLabel().intValue();
1967 vpnManager.addExtraRoute(vpnName, adj.getIpAddress(), nh, rdToAllocate.get(),
1968 currVpnIntf.getVpnInstanceName(), l3vni, origin,
1969 currVpnIntf.getName(), operationalAdjacency, encapType,
1970 prefixListForRefreshFib, writeConfigTxn);
1971 LOG.info("addNewAdjToVpnInterface: Added extra route ip {} nh {} rd {} vpnname {} label {}"
1972 + " Interface {} on dpn {}", adj.getIpAddress(), nh, rdToAllocate.get(),
1973 vpnName, label, currVpnIntf.getName(), dpnId);
1975 LOG.error("addNewAdjToVpnInterface: No rds to allocate extraroute vpn {} prefix {}",
1979 // iRT/eRT use case Will be handled in a new patchset for L3VPN Over VxLAN.
1980 // Keeping the MPLS check for now.
1981 if (encapType.equals(VrfEntryBase.EncapType.Mplsgre)) {
1982 final Adjacency opAdjacency = new AdjacencyBuilder(operationalAdjacency).build();
1983 List<VpnInstanceOpDataEntry> vpnsToImportRoute =
1984 vpnUtil.getVpnsImportingMyRoute(vpnName);
1985 vpnsToImportRoute.forEach(vpn -> {
1986 if (vpn.getVrfId() != null) {
1987 vpnUtil.allocateRdForExtraRouteAndUpdateUsedRdsMap(vpn.getVpnId(), vpnId, prefix,
1988 vpnUtil.getVpnName(vpn.getVpnId()), nh, dpnId)
1990 rds -> vpnManager.addExtraRoute(
1991 vpnUtil.getVpnName(vpn.getVpnId()), adj.getIpAddress(),
1992 nh, rds, currVpnIntf.getVpnInstanceName(), l3vni,
1993 RouteOrigin.SELF_IMPORTED, currVpnIntf.getName(), opAdjacency,
1994 encapType, prefixListForRefreshFib, writeConfigTxn));
2001 } else if (adj.isPhysNetworkFunc()) { // PNF adjacency.
2002 LOG.trace("addNewAdjToVpnInterface: Adding prefix {} to interface {} for vpn {}", prefix,
2003 currVpnIntf.getName(), vpnName);
2005 InstanceIdentifier<VpnInterface> vpnIfaceConfigidentifier = VpnUtil
2006 .getVpnInterfaceIdentifier(currVpnIntf.getName());
2007 Optional<VpnInterface> vpnIntefaceConfig = SingleTransactionDataBroker.syncReadOptional(dataBroker,
2008 LogicalDatastoreType.CONFIGURATION, vpnIfaceConfigidentifier);
2009 Prefixes pnfPrefix = VpnUtil.getPrefixToInterface(BigInteger.ZERO, currVpnIntf.getName(), prefix,
2010 Prefixes.PrefixCue.PhysNetFunc);
2011 if (vpnIntefaceConfig.isPresent()) {
2012 pnfPrefix = VpnUtil.getPrefixToInterface(BigInteger.ZERO, currVpnIntf.getName(), prefix,
2013 vpnIntefaceConfig.get().getNetworkId(), vpnIntefaceConfig.get().getNetworkType(),
2014 vpnIntefaceConfig.get().getSegmentationId(), Prefixes.PrefixCue.PhysNetFunc);
2017 String parentVpnRd = getParentVpnRdForExternalSubnet(adj);
2020 VpnUtil.getPrefixToInterfaceIdentifier(vpnUtil.getVpnId(adj.getSubnetId().getValue()),
2021 prefix), pnfPrefix, true);
2023 fibManager.addOrUpdateFibEntry(adj.getSubnetId().getValue(), adj.getMacAddress(),
2024 adj.getIpAddress(), emptyList(), null /* EncapType */, 0 /* label */,
2025 0 /*l3vni*/, null /* gw-mac */, parentVpnRd, RouteOrigin.LOCAL, writeConfigTxn);
2027 input.setRd(adj.getVrfId());
2029 if (operationalAdjacency == null) {
2030 operationalAdjacency = populator.createOperationalAdjacency(input);
2032 adjacencies.add(operationalAdjacency);
2033 AdjacenciesOp aug = VpnUtil.getVpnInterfaceOpDataEntryAugmentation(adjacencies);
2034 VpnInterfaceOpDataEntry newVpnIntf =
2035 VpnUtil.getVpnInterfaceOpDataEntry(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(),
2036 aug, dpnId, currVpnIntf.getLportTag(),
2037 currVpnIntf.getGatewayMacAddress());
2039 writeOperTxn.merge(identifier, newVpnIntf, CREATE_MISSING_PARENTS);
2041 } catch (ReadFailedException e) {
2042 LOG.error("addNewAdjToVpnInterface: Failed to read data store for interface {} dpn {} vpn {} rd {} ip "
2043 + "{}", interfaceName, dpnId, configVpnName, primaryRd, adj.getIpAddress());
2048 private String getParentVpnRdForExternalSubnet(Adjacency adj) {
2049 Subnets subnets = vpnUtil.getExternalSubnet(adj.getSubnetId());
2050 return subnets != null ? subnets.getExternalNetworkId().getValue() : null;
2053 protected void delAdjFromVpnInterface(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, Adjacency adj,
2054 BigInteger dpnId, TypedWriteTransaction<Operational> writeOperTxn,
2055 TypedWriteTransaction<Configuration> writeConfigTxn) {
2056 String interfaceName = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class).getName();
2057 String vpnName = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class).getVpnInstanceName();
2059 Optional<VpnInterfaceOpDataEntry> optVpnInterface = SingleTransactionDataBroker.syncReadOptional(
2060 dataBroker, LogicalDatastoreType.OPERATIONAL, identifier);
2061 if (optVpnInterface.isPresent()) {
2062 VpnInterfaceOpDataEntry currVpnIntf = optVpnInterface.get();
2063 InstanceIdentifier<AdjacenciesOp> path = identifier.augmentation(AdjacenciesOp.class);
2064 Optional<AdjacenciesOp> optAdjacencies = SingleTransactionDataBroker.syncReadOptional(dataBroker,
2065 LogicalDatastoreType.OPERATIONAL, path);
2066 if (optAdjacencies.isPresent()) {
2067 List<Adjacency> adjacencies = optAdjacencies.get().getAdjacency();
2069 if (adjacencies != null && !adjacencies.isEmpty()) {
2070 LOG.trace("delAdjFromVpnInterface: Adjacencies are {}", adjacencies);
2071 for (Adjacency adjacency : adjacencies) {
2072 if (Objects.equals(adjacency.getIpAddress(), adj.getIpAddress())) {
2073 String rd = adjacency.getVrfId();
2074 InstanceIdentifier<Adjacency> adjIdentifier = VpnUtil
2075 .getVpnInterfaceOpDataEntryAdjacencyIdentifier(currVpnIntf.getName(),
2076 currVpnIntf.getVpnInstanceName(), adj.getIpAddress());
2077 LOG.debug("delAdjFromVpnInterface: adjIdentifier {}", adjIdentifier);
2078 writeOperTxn.delete(adjIdentifier);
2079 if (adj.getNextHopIpList() != null) {
2080 for (String nh : adj.getNextHopIpList()) {
2081 deleteExtraRouteFromCurrentAndImportingVpns(
2082 currVpnIntf.getVpnInstanceName(), adj.getIpAddress(), nh, rd,
2083 currVpnIntf.getName(), writeConfigTxn, writeOperTxn);
2085 } else if (adj.isPhysNetworkFunc()) {
2086 LOG.info("delAdjFromVpnInterface: deleting PNF adjacency prefix {} subnet {}",
2087 adj.getIpAddress(), adj.getSubnetId());
2088 fibManager.removeFibEntry(adj.getSubnetId().getValue(), adj.getIpAddress(),
2096 LOG.info("delAdjFromVpnInterface: Removed adj {} on dpn {} rd {}", adj.getIpAddress(),
2097 dpnId, adj.getVrfId());
2099 LOG.error("delAdjFromVpnInterface: Cannnot DEL adjacency, since operational interface is "
2100 + "unavailable dpnId {} adjIP {} rd {}", dpnId, adj.getIpAddress(), adj.getVrfId());
2103 } catch (ReadFailedException e) {
2104 LOG.error("delAdjFromVpnInterface: Failed to read data store for ip {} interface {} dpn {} vpn {}",
2105 adj.getIpAddress(), interfaceName, dpnId, vpnName);
2109 private void deleteExtraRouteFromCurrentAndImportingVpns(String vpnName, String destination, String nextHop,
2110 String rd, String intfName, TypedWriteTransaction<Configuration> writeConfigTxn,
2111 TypedWriteTransaction<Operational> writeOperTx) {
2112 vpnManager.delExtraRoute(vpnName, destination, nextHop, rd, vpnName, intfName, writeConfigTxn, writeOperTx);
2113 List<VpnInstanceOpDataEntry> vpnsToImportRoute = vpnUtil.getVpnsImportingMyRoute(vpnName);
2114 for (VpnInstanceOpDataEntry vpn : vpnsToImportRoute) {
2115 String vpnRd = vpn.getVrfId();
2116 if (vpnRd != null) {
2117 vpnManager.delExtraRoute(vpnName, destination, nextHop, vpnRd, vpnName, intfName, writeConfigTxn,
2123 InstanceIdentifier<DpnVpninterfacesList> getRouterDpnId(String routerName, BigInteger dpnId) {
2124 return InstanceIdentifier.builder(NeutronRouterDpns.class)
2125 .child(RouterDpnList.class, new RouterDpnListKey(routerName))
2126 .child(DpnVpninterfacesList.class, new DpnVpninterfacesListKey(dpnId)).build();
2129 InstanceIdentifier<RouterDpnList> getRouterId(String routerName) {
2130 return InstanceIdentifier.builder(NeutronRouterDpns.class)
2131 .child(RouterDpnList.class, new RouterDpnListKey(routerName)).build();
2134 protected void createFibEntryForRouterInterface(String primaryRd, VpnInterface vpnInterface, String interfaceName,
2135 TypedWriteTransaction<Configuration> writeConfigTxn, String vpnName) {
2136 if (vpnInterface == null) {
2139 List<Adjacency> adjs = vpnUtil.getAdjacenciesForVpnInterfaceFromConfig(interfaceName);
2141 LOG.error("createFibEntryForRouterInterface: VPN Interface {} of router addition failed as adjacencies for"
2142 + " this vpn interface could not be obtained. vpn {}", interfaceName, vpnName);
2145 for (Adjacency adj : adjs) {
2146 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
2147 String primaryInterfaceIp = adj.getIpAddress();
2148 String macAddress = adj.getMacAddress();
2149 String prefix = VpnUtil.getIpPrefix(primaryInterfaceIp);
2151 long label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
2152 VpnUtil.getNextHopLabelKey(primaryRd, prefix));
2154 RouterInterface routerInt = new RouterInterfaceBuilder().setUuid(vpnName)
2155 .setIpAddress(primaryInterfaceIp).setMacAddress(macAddress).build();
2156 fibManager.addFibEntryForRouterInterface(primaryRd, prefix,
2157 routerInt, label, writeConfigTxn);
2158 LOG.info("createFibEntryForRouterInterface: Router interface {} for vpn {} rd {} prefix {} label {}"
2159 + " macAddress {} processed successfully;", interfaceName, vpnName, primaryRd, prefix, label,
2162 LOG.error("createFibEntryForRouterInterface: VPN Interface {} of router addition failed as primary"
2163 + " adjacency for this vpn interface could not be obtained. rd {} vpnName {}",
2164 interfaceName, primaryRd, vpnName);
2169 protected void deleteFibEntryForRouterInterface(VpnInterface vpnInterface,
2170 TypedWriteTransaction<Configuration> writeConfigTxn, String vpnName) {
2171 Adjacencies adjs = vpnInterface.augmentation(Adjacencies.class);
2172 String rd = vpnUtil.getVpnRd(vpnName);
2174 List<Adjacency> adjsList = adjs.nonnullAdjacency();
2175 for (Adjacency adj : adjsList) {
2176 if (adj.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
2177 String primaryInterfaceIp = adj.getIpAddress();
2178 String prefix = VpnUtil.getIpPrefix(primaryInterfaceIp);
2179 fibManager.removeFibEntry(rd, prefix, writeConfigTxn);
2180 LOG.info("deleteFibEntryForRouterInterface: FIB for router interface {} deleted for vpn {} rd {}"
2181 + " prefix {}", vpnInterface.getName(), vpnName, rd, prefix);
2185 LOG.error("deleteFibEntryForRouterInterface: Adjacencies for vpninterface {} is null, rd: {}",
2186 vpnInterface.getName(), rd);
2190 private void processSavedInterface(UnprocessedVpnInterfaceData intefaceData, String vpnName) {
2191 final VpnInterfaceKey key = intefaceData.identifier.firstKeyOf(VpnInterface.class);
2192 final String interfaceName = key.getName();
2193 InstanceIdentifier<VpnInterfaceOpDataEntry> vpnInterfaceOpIdentifier = VpnUtil
2194 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
2195 addVpnInterfaceToVpn(vpnInterfaceOpIdentifier, intefaceData.vpnInterface, null, null,
2196 intefaceData.identifier, vpnName);
2199 private void addToUnprocessedVpnInterfaces(InstanceIdentifier<VpnInterface> identifier,
2200 VpnInterface vpnInterface, String vpnName) {
2201 ConcurrentLinkedQueue<UnprocessedVpnInterfaceData> vpnInterfaces = unprocessedVpnInterfaces
2203 if (vpnInterfaces == null) {
2204 vpnInterfaces = new ConcurrentLinkedQueue<>();
2206 vpnInterfaces.add(new UnprocessedVpnInterfaceData(identifier, vpnInterface));
2207 unprocessedVpnInterfaces.put(vpnName, vpnInterfaces);
2208 LOG.info("addToUnprocessedVpnInterfaces: Saved unhandled vpn interface {} in vpn instance {}",
2209 vpnInterface.getName(), vpnName);
2212 public boolean isVpnInstanceReady(String vpnInstanceName) {
2213 String vpnRd = vpnUtil.getVpnRd(vpnInstanceName);
2214 if (vpnRd == null) {
2217 VpnInstanceOpDataEntry vpnInstanceOpDataEntry = vpnUtil.getVpnInstanceOpData(vpnRd);
2219 return vpnInstanceOpDataEntry != null;
2222 public void processSavedInterfaces(String vpnInstanceName, boolean hasVpnInstanceCreatedSuccessfully) {
2223 // FIXME: separate out to somehow?
2224 final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnInstanceName);
2227 ConcurrentLinkedQueue<UnprocessedVpnInterfaceData> vpnInterfaces =
2228 unprocessedVpnInterfaces.get(vpnInstanceName);
2229 if (vpnInterfaces != null) {
2230 while (!vpnInterfaces.isEmpty()) {
2231 UnprocessedVpnInterfaceData savedInterface = vpnInterfaces.poll();
2232 if (hasVpnInstanceCreatedSuccessfully) {
2233 processSavedInterface(savedInterface, vpnInstanceName);
2234 LOG.info("processSavedInterfaces: Handle saved vpn interfaces {} in vpn instance {}",
2235 savedInterface.vpnInterface.getName(), vpnInstanceName);
2237 LOG.error("processSavedInterfaces: Cannot process vpn interface {} in vpn instance {}",
2238 savedInterface.vpnInterface.getName(), vpnInstanceName);
2242 LOG.info("processSavedInterfaces: No interfaces in queue for VPN {}", vpnInstanceName);
2249 private void removeInterfaceFromUnprocessedList(InstanceIdentifier<VpnInterface> identifier,
2250 VpnInterface vpnInterface) {
2251 // FIXME: use VpnInstanceNamesKey perhaps? What about nulls?
2252 final String firstVpnName = VpnHelper.getFirstVpnNameFromVpnInterface(vpnInterface);
2253 final ReentrantLock lock = JvmGlobalLocks.getLockForString(firstVpnName);
2256 ConcurrentLinkedQueue<UnprocessedVpnInterfaceData> vpnInterfaces =
2257 unprocessedVpnInterfaces.get(firstVpnName);
2258 if (vpnInterfaces != null) {
2259 if (vpnInterfaces.remove(new UnprocessedVpnInterfaceData(identifier, vpnInterface))) {
2260 LOG.info("removeInterfaceFromUnprocessedList: Removed vpn interface {} in vpn instance {} from "
2261 + "unprocessed list", vpnInterface.getName(), firstVpnName);
2264 LOG.info("removeInterfaceFromUnprocessedList: No interfaces in queue for VPN {}", firstVpnName);
2271 public void vpnInstanceIsReady(String vpnInstanceName) {
2272 processSavedInterfaces(vpnInstanceName, true);
2275 public void vpnInstanceFailed(String vpnInstanceName) {
2276 processSavedInterfaces(vpnInstanceName, false);
2279 private static class UnprocessedVpnInterfaceData {
2280 InstanceIdentifier<VpnInterface> identifier;
2281 VpnInterface vpnInterface;
2283 UnprocessedVpnInterfaceData(InstanceIdentifier<VpnInterface> identifier, VpnInterface vpnInterface) {
2284 this.identifier = identifier;
2285 this.vpnInterface = vpnInterface;
2289 public int hashCode() {
2290 final int prime = 31;
2292 result = prime * result + (identifier == null ? 0 : identifier.hashCode());
2293 result = prime * result + (vpnInterface == null ? 0 : vpnInterface.hashCode());
2298 public boolean equals(Object obj) {
2305 if (getClass() != obj.getClass()) {
2308 UnprocessedVpnInterfaceData other = (UnprocessedVpnInterfaceData) obj;
2309 if (identifier == null) {
2310 if (other.identifier != null) {
2313 } else if (!identifier.equals(other.identifier)) {
2316 if (vpnInterface == null) {
2317 if (other.vpnInterface != null) {
2320 } else if (!vpnInterface.equals(other.vpnInterface)) {
2327 public void updateVpnInterfacesForUnProcessAdjancencies(String vpnName) {
2328 String primaryRd = vpnUtil.getVpnRd(vpnName);
2329 VpnInstanceOpDataEntry vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(primaryRd);
2330 if (vpnInstanceOpData == null) {
2333 List<VpnToDpnList> vpnToDpnLists = vpnInstanceOpData.getVpnToDpnList();
2334 if (vpnToDpnLists == null || vpnToDpnLists.isEmpty()) {
2337 LOG.debug("Update the VpnInterfaces for Unprocessed Adjancencies for vpnName:{}", vpnName);
2338 vpnToDpnLists.forEach(vpnToDpnList -> {
2339 if (vpnToDpnList.getVpnInterfaces() == null) {
2342 vpnToDpnList.getVpnInterfaces().forEach(vpnInterface -> {
2344 InstanceIdentifier<VpnInterfaceOpDataEntry> existingVpnInterfaceId =
2345 VpnUtil.getVpnInterfaceOpDataEntryIdentifier(vpnInterface.getInterfaceName(), vpnName);
2346 Optional<VpnInterfaceOpDataEntry> vpnInterfaceOptional = SingleTransactionDataBroker
2347 .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, existingVpnInterfaceId);
2348 if (!vpnInterfaceOptional.isPresent()) {
2351 List<Adjacency> configVpnAdjacencies = vpnUtil.getAdjacenciesForVpnInterfaceFromConfig(
2352 vpnInterface.getInterfaceName());
2353 if (configVpnAdjacencies == null) {
2354 LOG.debug("There is no adjacency available for vpnInterface:{}", vpnInterface);
2357 List<Adjacency> operationVpnAdjacencies = vpnInterfaceOptional.get()
2358 .augmentation(AdjacenciesOp.class).nonnullAdjacency();
2359 // Due to insufficient rds, some of the extra route wont get processed when it is added.
2360 // The unprocessed adjacencies will be present in config vpn interface DS but will be missing
2361 // in operational DS. These unprocessed adjacencies will be handled below.
2362 // To obtain unprocessed adjacencies, filtering is done by which the missing adjacencies in
2363 // operational DS are retrieved which is used to call addNewAdjToVpnInterface method.
2364 configVpnAdjacencies.stream()
2365 .filter(adjacency -> operationVpnAdjacencies.stream()
2366 .noneMatch(operationalAdjacency ->
2367 Objects.equals(operationalAdjacency.getIpAddress(), adjacency.getIpAddress())))
2368 .forEach(adjacency -> {
2369 LOG.debug("Processing the vpnInterface{} for the Ajacency:{}", vpnInterface, adjacency);
2370 jobCoordinator.enqueueJob("VPNINTERFACE-" + vpnInterface.getInterfaceName(),
2372 // TODO Deal with sequencing — the config tx must only submitted
2373 // if the oper tx goes in
2374 if (vpnUtil.isAdjacencyEligibleToVpn(adjacency, vpnName)) {
2375 List<ListenableFuture<Void>> futures = new ArrayList<>();
2377 txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, operTx -> {
2378 //set of prefix used, as entry in prefix-to-interface datastore
2379 // is prerequisite for refresh Fib to avoid race condition leading
2380 // to missing remote next hop in bucket actions on bgp-vpn delete
2381 Set<String> prefixListForRefreshFib = new HashSet<>();
2382 ListenableFuture<Void> configTxFuture =
2383 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
2384 confTx -> addNewAdjToVpnInterface(existingVpnInterfaceId,
2385 primaryRd, adjacency, vpnInterfaceOptional.get().getDpnId(),
2386 operTx, confTx, confTx, prefixListForRefreshFib));
2387 Futures.addCallback(configTxFuture,
2388 new VpnInterfaceCallBackHandler(primaryRd, prefixListForRefreshFib),
2389 MoreExecutors.directExecutor());
2390 futures.add(configTxFuture);
2398 } catch (ReadFailedException e) {
2399 LOG.error("updateVpnInterfacesForUnProcessAdjancencies: Failed to read data store for vpn {} rd {}",
2400 vpnName, primaryRd);
2406 private class PostVpnInterfaceWorker implements FutureCallback<Void> {
2407 private final String interfaceName;
2408 private final boolean add;
2409 private final String txnDestination;
2411 PostVpnInterfaceWorker(String interfaceName, boolean add, String transactionDest) {
2412 this.interfaceName = interfaceName;
2414 this.txnDestination = transactionDest;
2418 public void onSuccess(Void voidObj) {
2420 LOG.debug("VpnInterfaceManager: VrfEntries for {} stored into destination {} successfully",
2421 interfaceName, txnDestination);
2423 LOG.debug("VpnInterfaceManager: VrfEntries for {} removed successfully", interfaceName);
2428 public void onFailure(Throwable throwable) {
2430 LOG.error("VpnInterfaceManager: VrfEntries for {} failed to store into destination {}",
2431 interfaceName, txnDestination, throwable);
2433 LOG.error("VpnInterfaceManager: VrfEntries for {} removal failed", interfaceName, throwable);
2434 vpnUtil.unsetScheduledToRemoveForVpnInterface(interfaceName);
2439 private class VpnInterfaceCallBackHandler implements FutureCallback<Void> {
2440 private final String primaryRd;
2441 private final Set<String> prefixListForRefreshFib;
2443 VpnInterfaceCallBackHandler(String primaryRd, Set<String> prefixListForRefreshFib) {
2444 this.primaryRd = primaryRd;
2445 this.prefixListForRefreshFib = prefixListForRefreshFib;
2449 public void onSuccess(Void voidObj) {
2450 prefixListForRefreshFib.forEach(prefix -> {
2451 fibManager.refreshVrfEntry(primaryRd, prefix);
2456 public void onFailure(Throwable throwable) {
2457 LOG.debug("write Tx config operation failed", throwable);