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 com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
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.infrautils.jobcoordinator.JobCoordinator;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesOp;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInterfaceOpData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class VpnInterfaceOpListener extends AsyncDataTreeChangeListenerBase<VpnInterfaceOpDataEntry,
38 VpnInterfaceOpListener> {
39 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceOpListener.class);
40 private final DataBroker dataBroker;
41 private final VpnInterfaceManager vpnInterfaceManager;
42 private final VpnFootprintService vpnFootprintService;
43 private final JobCoordinator jobCoordinator;
44 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
46 /*public VpnInterfaceOpListener(final DataBroker dataBroker) {
47 super(VpnInterface.class);
48 this.dataBroker = dataBroker;
52 public VpnInterfaceOpListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
53 final VpnFootprintService vpnFootprintService, final JobCoordinator jobCoordinator) {
54 super(VpnInterfaceOpDataEntry.class, VpnInterfaceOpListener.class);
55 this.dataBroker = dataBroker;
56 this.vpnInterfaceManager = vpnInterfaceManager;
57 this.vpnFootprintService = vpnFootprintService;
58 this.jobCoordinator = jobCoordinator;
63 LOG.info("{} start", getClass().getSimpleName());
64 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
68 protected InstanceIdentifier<VpnInterfaceOpDataEntry> getWildCardPath() {
69 InstanceIdentifier<VpnInterfaceOpDataEntry> id = InstanceIdentifier.create(VpnInterfaceOpData.class
70 ).child(VpnInterfaceOpDataEntry.class);
75 protected VpnInterfaceOpListener getDataTreeChangeListener() {
76 return VpnInterfaceOpListener.this;
81 protected void remove(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
82 final VpnInterfaceOpDataEntry del) {
83 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class,
84 VpnInterfaceOpDataEntryKey.class);
85 final String interfaceName = key.getName();
86 jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName,
88 WriteTransaction writeOperTxn = dataBroker.newWriteOnlyTransaction();
89 postProcessVpnInterfaceRemoval(identifier, del, writeOperTxn);
90 List<ListenableFuture<Void>> futures = new ArrayList<>();
91 futures.add(writeOperTxn.submit());
92 LOG.info("remove: Removed vpn operational data for interface {} on dpn {} vpn {}", del.getName(),
93 del.getDpnId(), del.getVpnInstanceName());
98 private void postProcessVpnInterfaceRemoval(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
99 VpnInterfaceOpDataEntry del, WriteTransaction writeOperTxn) {
100 final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class,
101 VpnInterfaceOpDataEntryKey.class);
102 String interfaceName = key.getName();
103 String vpnName = del.getVpnInstanceName();
105 LOG.info("postProcessVpnInterfaceRemoval: interface name {} vpnName {} dpn {}", interfaceName, vpnName,
107 //decrement the vpn interface count in Vpn Instance Op Data
108 Optional<VpnInstance> vpnInstance = VpnUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
109 VpnOperDsUtils.getVpnInstanceToVpnIdIdentifier(vpnName));
111 if (vpnInstance.isPresent()) {
113 rd = vpnInstance.get().getVrfId();
115 VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(dataBroker, rd);
117 AdjacenciesOp adjs = del.getAugmentation(AdjacenciesOp.class);
118 List<Adjacency> adjList = adjs != null ? adjs.getAdjacency() : null;
120 if (vpnInstOp != null && adjList != null && adjList.size() > 0) {
122 * When a VPN Interface is removed by FibManager (aka VrfEntryListener and its cohorts),
123 * one adjacency for that VPN Interface will be hanging around along with that
124 * VPN Interface. That adjacency could be primary (or) non-primary.
125 * If its a primary adjacency, then a prefix-to-interface entry will be available for the
126 * same. If its a non-primary adjacency, then a prefix-to-interface entry will not be
127 * available for the same, instead we will have vpn-to-extraroutes filled in for them.
129 * Here we try to remove prefix-to-interface entry for pending adjacency in the deleted
130 * vpnInterface. More importantly, we also update the vpnInstanceOpData by removing this
131 * vpnInterface from it.
133 List<Prefixes> prefixToInterface = new ArrayList<>();
134 for (Adjacency adjacency : adjs.getAdjacency()) {
135 List<Prefixes> prefixToInterfaceLocal = new ArrayList<>();
136 Optional<Prefixes> prefix = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
137 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
138 VpnUtil.getIpPrefix(adjacency.getIpAddress())));
139 if (prefix.isPresent()) {
140 prefixToInterfaceLocal.add(prefix.get());
142 if (prefixToInterfaceLocal.isEmpty()) {
143 for (String nh : adjacency.getNextHopIpList()) {
144 prefix = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
145 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
146 VpnUtil.getIpPrefix(nh)));
147 if (prefix.isPresent()) {
148 prefixToInterfaceLocal.add(prefix.get());
152 if (!prefixToInterfaceLocal.isEmpty()) {
153 prefixToInterface.addAll(prefixToInterfaceLocal);
157 * In VPN Migration scenarios, there is a race condition where we use the new DPNID
158 * for the migrated VM instead of old DPNID because when we read prefix-to-interface to cleanup
159 * old DPNID, we actually get the new DPNID.
161 * More dangerously, we tend to alter the new prefix-to-interface which should be retained intac
162 * for the migration to succeed in L3VPN. As a workaround, here we are going to use the dpnId in
163 * the deleted vpnInterface itself instead of tinkering with the prefix-to-interface. Further we
164 * will tinker prefix-to-interface only when are damn sure if its value matches our
165 * deleted vpnInterface.
168 for (Prefixes pref : prefixToInterface) {
169 if (isMatchedPrefixToInterface(pref, del)) {
170 if (writeOperTxn != null) {
171 writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL,
172 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(), pref.getIpAddress()));
174 VpnUtil.delete(dataBroker, LogicalDatastoreType.OPERATIONAL,
175 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(), pref.getIpAddress()),
176 VpnUtil.DEFAULT_CALLBACK);
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 boolean isMatchedPrefixToInterface(Prefixes prefix, VpnInterfaceOpDataEntry vpnInterface) {
197 if (prefix != null && vpnInterface != null) {
198 if (prefix.getDpnId() != null && vpnInterface.getDpnId() != null) {
199 if (prefix.getVpnInterfaceName() != null && vpnInterface.getName() != null) {
200 return prefix.getDpnId().equals(vpnInterface.getDpnId())
201 && prefix.getVpnInterfaceName().equalsIgnoreCase(vpnInterface.getName());
208 private void notifyTaskIfRequired(String intfName) {
209 Runnable notifyTask = vpnInterfaceManager.isNotifyTaskQueued(intfName);
210 if (notifyTask == null) {
211 LOG.debug("notifyTaskIfRequired: No tasks queued to wait for deletion of vpnInterface {}", intfName);
214 executorService.execute(notifyTask);
218 protected void update(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
219 final VpnInterfaceOpDataEntry original, final VpnInterfaceOpDataEntry update) {
220 LOG.info("update: interface {} vpn {}. Ignoring", original.getName(), original.getVpnInstanceName());
224 protected void add(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, VpnInterfaceOpDataEntry add) {
225 LOG.info("add: interface {} vpn {}. Ignoring", add.getName(), add.getVpnInstanceName());