2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.vpnmanager;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import javax.annotation.PostConstruct;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
25 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
26 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesOp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInterfaceOpData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class VpnInterfaceOpListener extends AsyncDataTreeChangeListenerBase<VpnInterfaceOpDataEntry,
41 VpnInterfaceOpListener> {
42 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceOpListener.class);
43 private final DataBroker dataBroker;
44 private final ManagedNewTransactionRunner txRunner;
45 private final VpnInterfaceManager vpnInterfaceManager;
46 private final VpnFootprintService vpnFootprintService;
47 private final JobCoordinator jobCoordinator;
48 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
50 /*public VpnInterfaceOpListener(final DataBroker dataBroker) {
51 super(VpnInterface.class);
52 this.dataBroker = dataBroker;
56 public VpnInterfaceOpListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
57 final VpnFootprintService vpnFootprintService, final JobCoordinator jobCoordinator) {
58 super(VpnInterfaceOpDataEntry.class, VpnInterfaceOpListener.class);
59 this.dataBroker = dataBroker;
60 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
61 this.vpnInterfaceManager = vpnInterfaceManager;
62 this.vpnFootprintService = vpnFootprintService;
63 this.jobCoordinator = jobCoordinator;
68 LOG.info("{} start", getClass().getSimpleName());
69 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
73 protected InstanceIdentifier<VpnInterfaceOpDataEntry> getWildCardPath() {
74 InstanceIdentifier<VpnInterfaceOpDataEntry> id = InstanceIdentifier.create(VpnInterfaceOpData.class
75 ).child(VpnInterfaceOpDataEntry.class);
80 protected VpnInterfaceOpListener getDataTreeChangeListener() {
81 return VpnInterfaceOpListener.this;
86 protected void remove(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
87 final VpnInterfaceOpDataEntry del) {
88 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class,
89 VpnInterfaceOpDataEntryKey.class);
90 final String interfaceName = key.getName();
91 jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName,
92 () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
93 postProcessVpnInterfaceRemoval(identifier, del, tx);
94 LOG.info("remove: Removed vpn operational data for interface {} on dpn {} vpn {}", del.getName(),
95 del.getDpnId(), del.getVpnInstanceName());
99 private void postProcessVpnInterfaceRemoval(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
100 VpnInterfaceOpDataEntry del, WriteTransaction writeOperTxn) {
101 if (writeOperTxn == null) {
102 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
103 postProcessVpnInterfaceRemoval(identifier, del, tx)), LOG,
104 "Error post-processing VPN interface removal");
107 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class,
108 VpnInterfaceOpDataEntryKey.class);
109 String interfaceName = key.getName();
110 String vpnName = del.getVpnInstanceName();
112 LOG.info("postProcessVpnInterfaceRemoval: interface name {} vpnName {} dpn {}", interfaceName, vpnName,
114 //decrement the vpn interface count in Vpn Instance Op Data
115 Optional<VpnInstance> vpnInstance = VpnUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
116 VpnOperDsUtils.getVpnInstanceToVpnIdIdentifier(vpnName));
118 if (vpnInstance.isPresent()) {
119 String rd = vpnInstance.get().getVrfId();
121 VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(dataBroker, rd);
123 AdjacenciesOp adjs = del.getAugmentation(AdjacenciesOp.class);
124 List<Adjacency> adjList = adjs != null ? adjs.getAdjacency() : null;
126 if (vpnInstOp != null && adjList != null && adjList.size() > 0) {
128 * When a VPN Interface is removed by FibManager (aka VrfEntryListener and its cohorts),
129 * one adjacency for that VPN Interface will be hanging around along with that
130 * VPN Interface. That adjacency could be primary (or) non-primary.
131 * If its a primary adjacency, then a prefix-to-interface entry will be available for the
132 * same. If its a non-primary adjacency, then a prefix-to-interface entry will not be
133 * available for the same, instead we will have vpn-to-extraroutes filled in for them.
135 * Here we try to remove prefix-to-interface entry for pending adjacency in the deleted
136 * vpnInterface. More importantly, we also update the vpnInstanceOpData by removing this
137 * vpnInterface from it.
139 List<Prefixes> prefixToInterface = new ArrayList<>();
140 for (Adjacency adjacency : adjs.getAdjacency()) {
141 List<Prefixes> prefixToInterfaceLocal = new ArrayList<>();
142 Optional<Prefixes> prefix = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
143 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
144 VpnUtil.getIpPrefix(adjacency.getIpAddress())));
145 if (prefix.isPresent()) {
146 prefixToInterfaceLocal.add(prefix.get());
148 if (prefixToInterfaceLocal.isEmpty()) {
149 for (String nh : adjacency.getNextHopIpList()) {
150 prefix = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
151 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
152 VpnUtil.getIpPrefix(nh)));
153 if (prefix.isPresent()) {
154 prefixToInterfaceLocal.add(prefix.get());
158 if (!prefixToInterfaceLocal.isEmpty()) {
159 prefixToInterface.addAll(prefixToInterfaceLocal);
163 * In VPN Migration scenarios, there is a race condition where we use the new DPNID
164 * for the migrated VM instead of old DPNID because when we read prefix-to-interface to cleanup
165 * old DPNID, we actually get the new DPNID.
167 * More dangerously, we tend to alter the new prefix-to-interface which should be retained intac
168 * for the migration to succeed in L3VPN. As a workaround, here we are going to use the dpnId in
169 * the deleted vpnInterface itself instead of tinkering with the prefix-to-interface. Further we
170 * will tinker prefix-to-interface only when are damn sure if its value matches our
171 * deleted vpnInterface.
174 for (Prefixes pref : prefixToInterface) {
175 if (VpnUtil.isMatchedPrefixToInterface(pref, del)) {
176 writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL,
177 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(), pref.getIpAddress()));
181 if (del.getDpnId() != null) {
182 vpnFootprintService.updateVpnToDpnMapping(del.getDpnId(), del.getVpnInstanceName(), rd,
183 interfaceName, null /*ipAddressSourceValuePair*/,
184 false /* do delete */);
186 LOG.info("postProcessVpnInterfaceRemoval: Removed vpn operational data and updated vpn footprint"
187 + " for interface {} on dpn {} vpn {}", interfaceName, del.getDpnId(), vpnName);
189 LOG.error("postProcessVpnInterfaceRemoval: rd not retrievable as vpninstancetovpnid for vpn {} is absent,"
190 + " trying rd as {}. interface {} dpn {}", vpnName, vpnName, interfaceName,
193 notifyTaskIfRequired(interfaceName);
196 private void notifyTaskIfRequired(String intfName) {
197 Runnable notifyTask = vpnInterfaceManager.isNotifyTaskQueued(intfName);
198 if (notifyTask == null) {
199 LOG.debug("notifyTaskIfRequired: No tasks queued to wait for deletion of vpnInterface {}", intfName);
202 executorService.execute(notifyTask);
206 protected void update(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
207 final VpnInterfaceOpDataEntry original, final VpnInterfaceOpDataEntry update) {
208 LOG.info("update: interface {} vpn {}. Ignoring", original.getName(), original.getVpnInstanceName());
212 protected void add(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, VpnInterfaceOpDataEntry add) {
213 LOG.info("add: interface {} vpn {}. Ignoring", add.getName(), add.getVpnInstanceName());