2 * Copyright (c) 2015, 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 org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13 import com.google.common.collect.HashBasedTable;
14 import com.google.common.collect.Table;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.Optional;
26 import javax.annotation.PreDestroy;
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
32 import org.opendaylight.infrautils.utils.concurrent.Executors;
33 import org.opendaylight.mdsal.binding.api.DataBroker;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
36 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
37 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.Adjacencies;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterface;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNames;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.Uint64;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 public class InterfaceStateChangeListener extends AbstractAsyncDataTreeChangeListener<Interface> {
56 private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
57 private static final short DJC_MAX_RETRIES = 3;
58 private final DataBroker dataBroker;
59 private final ManagedNewTransactionRunner txRunner;
60 private final VpnInterfaceManager vpnInterfaceManager;
61 private final VpnUtil vpnUtil;
62 private final JobCoordinator jobCoordinator;
63 private final IFibManager fibManager;
65 Table<OperStatus, OperStatus, IntfTransitionState> stateTable = HashBasedTable.create();
67 enum IntfTransitionState {
73 private void initialize() {
74 // Interface State Transition Table
76 // ---------------------------------------------------------------
77 /* Up { STATE_IGNORE, STATE_DOWN, STATE_IGNORE }, */
78 /* Down { STATE_UP, STATE_IGNORE, STATE_IGNORE }, */
79 /* Unknown { STATE_UP, STATE_DOWN, STATE_IGNORE }, */
81 stateTable.put(Interface.OperStatus.Up, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
82 stateTable.put(Interface.OperStatus.Down, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
83 stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
84 stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
88 public InterfaceStateChangeListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
89 final VpnUtil vpnUtil, final JobCoordinator jobCoordinator, final IFibManager fibManager) {
90 super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(InterfacesState.class)
91 .child(Interface.class),
92 Executors.newListeningSingleThreadExecutor("InterfaceStateChangeListener", LOG));
93 this.dataBroker = dataBroker;
94 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
95 this.vpnInterfaceManager = vpnInterfaceManager;
96 this.vpnUtil = vpnUtil;
97 this.jobCoordinator = jobCoordinator;
98 this.fibManager = fibManager;
102 public void start() {
103 LOG.info("{} start", getClass().getSimpleName());
108 public void close() {
110 Executors.shutdownAndAwaitTermination(getExecutorService());
115 // TODO Clean up the exception handling
116 @SuppressWarnings("checkstyle:IllegalCatch")
117 public void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
119 if (L2vlan.class.equals(intrf.getType())) {
120 LOG.info("VPN Interface add event - intfName {} from InterfaceStateChangeListener",
122 jobCoordinator.enqueueJob("VPNINTERFACE-" + intrf.getName(), () -> {
123 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
124 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
125 //map of prefix and vpn name used, as entry in prefix-to-interface datastore
126 // is prerequisite for refresh Fib to avoid race condition leading to missing remote next hop
127 // in bucket actions on bgp-vpn delete
128 Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib = new HashMap<>();
129 ListenableFuture<Void> configFuture
130 = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, writeConfigTxn -> {
131 ListenableFuture<Void> operFuture
132 = txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> {
133 final String interfaceName = intrf.getName();
134 LOG.info("Detected interface add event for interface {}", interfaceName);
135 final VpnInterface vpnIf = vpnUtil.getConfiguredVpnInterface(interfaceName);
137 for (VpnInstanceNames vpnInterfaceVpnInstance :
138 vpnIf.nonnullVpnInstanceNames()) {
139 String vpnName = vpnInterfaceVpnInstance.getVpnName();
140 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
141 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
142 LOG.info("VPN Interface add event - intfName {} onto vpnName {} "
143 + "running oper-driven, VpnInstance not ready, holding"
144 + " on", vpnIf.getName(), vpnName);
145 } else if (vpnUtil.isVpnPendingDelete(primaryRd)) {
146 LOG.error("add: Ignoring addition of vpnInterface {}, as"
147 + " vpnInstance {} with primaryRd {} is already marked for"
148 + " deletion", interfaceName, vpnName, primaryRd);
150 Uint64 intfDpnId = Uint64.ZERO;
152 intfDpnId = InterfaceUtils.getDpIdFromInterface(intrf);
153 } catch (Exception e) {
154 LOG.error("Unable to retrieve dpnId for interface {}. "
155 + "Process vpn interface add failed",intrf.getName(),
159 LOG.error("InterfaceStateChangeListener- Processing ifState"
160 + " {} add event with dpnId {}",
161 intrf.getName(), intfDpnId);
162 final Uint64 dpnId = intfDpnId;
163 final int ifIndex = intrf.getIfIndex();
164 LOG.info("VPN Interface add event - intfName {} onto vpnName {}"
165 + " running oper-driven", vpnIf.getName(), vpnName);
166 Set<String> prefixes = new HashSet<>();
167 vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
168 ifIndex, false, writeConfigTxn, writeOperTxn, writeInvTxn,
169 intrf, vpnName, prefixes);
170 mapOfRdAndPrefixesForRefreshFib.put(primaryRd, prefixes);
176 futures.add(operFuture);
177 operFuture.get(); //Synchronous submit of operTxn
179 Futures.addCallback(configFuture,
180 new VpnInterfaceCallBackHandler(mapOfRdAndPrefixesForRefreshFib),
181 MoreExecutors.directExecutor());
182 futures.add(configFuture);
183 //TODO: Allow immediateFailedFuture from writeCfgTxn to cancel writeInvTxn as well.
184 Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), true,
185 "Operational"), MoreExecutors.directExecutor());
190 } catch (Exception e) {
191 LOG.error("Exception caught in Interface {} Operational State Up event", intrf.getName(), e);
196 // TODO Clean up the exception handling
197 @SuppressWarnings("checkstyle:IllegalCatch")
198 public void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
199 final String ifName = intrf.getName();
200 Uint64 dpId = Uint64.ZERO;
202 if (L2vlan.class.equals(intrf.getType())) {
203 LOG.info("VPN Interface remove event - intfName {} from InterfaceStateChangeListener",
206 dpId = InterfaceUtils.getDpIdFromInterface(intrf);
207 } catch (Exception e) {
208 LOG.error("Unable to retrieve dpnId from interface operational data store for interface"
209 + " {}. Fetching from vpn interface op data store. ", ifName, e);
211 final Uint64 inputDpId = dpId;
212 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
213 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
214 ListenableFuture<Void> configFuture =
215 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
216 writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
217 writeOperTxn -> futures.add(
218 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
219 VpnInterface cfgVpnInterface =
220 vpnUtil.getConfiguredVpnInterface(ifName);
221 if (cfgVpnInterface == null) {
222 LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
225 for (VpnInstanceNames vpnInterfaceVpnInstance :
226 cfgVpnInterface.nonnullVpnInstanceNames()) {
227 String vpnName = vpnInterfaceVpnInstance.getVpnName();
228 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
229 vpnUtil.getVpnInterfaceOpDataEntry(ifName, vpnName);
230 if (!optVpnInterface.isPresent()) {
231 LOG.debug("Interface {} vpn {} is not a vpninterface, or deletion"
232 + " triggered by northbound agent. ignoring.", ifName, vpnName);
235 handleMipAdjRemoval(cfgVpnInterface, vpnName);
236 final VpnInterfaceOpDataEntry vpnInterface = optVpnInterface.get();
237 String gwMac = intrf.getPhysAddress() != null ? intrf.getPhysAddress()
238 .getValue() : vpnInterface.getGatewayMacAddress();
239 Uint64 dpnId = inputDpId;
240 if (dpnId == null || dpnId.equals(Uint64.ZERO)) {
241 dpnId = vpnInterface.getDpnId();
243 final int ifIndex = intrf.getIfIndex();
244 LOG.info("VPN Interface remove event - intfName {} onto vpnName {}"
245 + " running oper-driver", vpnInterface.getName(), vpnName);
246 vpnInterfaceManager.processVpnInterfaceDown(dpnId, ifName, ifIndex, gwMac,
247 vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
250 futures.add(configFuture);
251 Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), false,
252 "Operational"), MoreExecutors.directExecutor());
256 } catch (Exception e) {
257 LOG.error("Exception observed in handling deletion of VPN Interface {}. ", ifName, e);
261 // TODO Clean up the exception handling
262 @SuppressWarnings("checkstyle:IllegalCatch")
264 public void update(InstanceIdentifier<Interface> identifier,
265 Interface original, Interface update) {
266 final String ifName = update.getName();
268 if (update.getIfIndex() == null) {
271 if (L2vlan.class.equals(update.getType())) {
272 LOG.info("VPN Interface update event - intfName {} from InterfaceStateChangeListener",
274 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
275 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
276 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> {
277 //map of prefix and vpn name used, as entry in prefix-to-interface datastore
278 // is prerequisite for refresh Fib to avoid race condition leading to missing remote
279 // next hop in bucket actions on bgp-vpn delete
280 Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib = new HashMap<>();
281 ListenableFuture<Void> configTxFuture =
282 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, writeConfigTxn ->
283 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
285 final VpnInterface vpnIf = vpnUtil.getConfiguredVpnInterface(ifName);
287 final int ifIndex = update.getIfIndex();
290 dpnId = InterfaceUtils.getDpIdFromInterface(update);
291 } catch (Exception e) {
292 LOG.error("remove: Unable to retrieve dpnId for interface {}",
296 IntfTransitionState state = getTransitionState(
297 original.getOperStatus(), update.getOperStatus());
298 if (state.equals(IntfTransitionState.STATE_IGNORE)) {
299 LOG.info("InterfaceStateChangeListener: Interface {} state "
300 + "original {}" + "updated {} not handled", ifName,
301 original.getOperStatus(), update.getOperStatus());
304 LOG.error("InterfaceStateChangeListener- Processing ifState {} "
306 + "with dpnId {} operstate {}",
307 ifName, dpnId, update.getOperStatus());
308 if (state.equals(IntfTransitionState.STATE_UP)
309 && vpnIf.getVpnInstanceNames() != null) {
310 for (VpnInstanceNames vpnInterfaceVpnInstance :
311 vpnIf.getVpnInstanceNames()) {
312 String vpnName = vpnInterfaceVpnInstance.getVpnName();
313 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
314 Set<String> prefixes = new HashSet<>();
315 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
316 LOG.error("VPN Interface update event - intfName {} "
317 + "onto vpnName {} running oper-driven UP, "
318 + "VpnInstance not ready, holding on",
319 vpnIf.getName(), vpnName);
320 } else if (vpnUtil.isVpnPendingDelete(primaryRd)) {
321 LOG.error("update: Ignoring UP event for vpnInterface "
322 + "{}, as vpnInstance {} with primaryRd {} is "
323 + "already marked for deletion ",
324 vpnIf.getName(), vpnName, primaryRd);
326 vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf,
327 primaryRd, ifIndex, true, writeConfigTxn,
328 writeOperTxn, writeInvTxn, update, vpnName, prefixes);
329 mapOfRdAndPrefixesForRefreshFib.put(primaryRd, prefixes);
332 } else if (state.equals(IntfTransitionState.STATE_DOWN)
333 && vpnIf.getVpnInstanceNames() != null) {
334 for (VpnInstanceNames vpnInterfaceVpnInstance :
335 vpnIf.getVpnInstanceNames()) {
336 String vpnName = vpnInterfaceVpnInstance.getVpnName();
337 LOG.info("VPN Interface update event - intfName {} "
338 + " onto vpnName {} running oper-driven DOWN",
339 vpnIf.getName(), vpnName);
340 Optional<VpnInterfaceOpDataEntry> optVpnInterface = vpnUtil
341 .getVpnInterfaceOpDataEntry(vpnIf.getName(), vpnName);
342 if (optVpnInterface.isPresent()) {
343 VpnInterfaceOpDataEntry vpnOpInterface =
344 optVpnInterface.get();
345 handleMipAdjRemoval(vpnIf, vpnName);
346 vpnInterfaceManager.processVpnInterfaceDown(dpnId,
347 vpnIf.getName(), ifIndex, update.getPhysAddress()
348 .getValue(), vpnOpInterface, true,
349 writeConfigTxn, writeOperTxn, writeInvTxn);
351 LOG.error("InterfaceStateChangeListener Update DOWN - "
352 + " vpnInterface {}not available, ignoring event",
359 LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
362 Futures.addCallback(configTxFuture,
363 new VpnInterfaceCallBackHandler(mapOfRdAndPrefixesForRefreshFib),
364 MoreExecutors.directExecutor());
365 futures.add(configTxFuture);
370 } catch (Exception e) {
371 LOG.error("Exception observed in handling updation of VPN Interface {}. ", update.getName(), e);
375 private void handleMipAdjRemoval(VpnInterface cfgVpnInterface, String vpnName) {
376 String interfaceName = cfgVpnInterface.getName();
377 Adjacencies adjacencies = cfgVpnInterface.augmentation(Adjacencies.class);
378 if (adjacencies != null) {
379 List<Adjacency> adjacencyList = adjacencies.getAdjacency();
380 if (!adjacencyList.isEmpty()) {
381 for (Adjacency adj : adjacencyList) {
382 if (adj.getAdjacencyType() != Adjacency.AdjacencyType.PrimaryAdjacency) {
383 String ipAddress = adj.getIpAddress();
384 String prefix = ipAddress.split("/")[0];
385 LearntVpnVipToPort vpnVipToPort = vpnUtil.getLearntVpnVipToPort(vpnName, prefix);
386 if (vpnVipToPort != null && vpnVipToPort.getPortName().equals(interfaceName)) {
387 vpnUtil.removeMipAdjacency(vpnName, interfaceName, ipAddress, null);
389 LOG.debug("IP {} could be extra-route or learnt-ip on different interface"
390 + "than oper-vpn-interface {}", ipAddress, interfaceName);
398 private class PostVpnInterfaceThreadWorker implements FutureCallback<Void> {
399 private final String interfaceName;
400 private final boolean add;
401 private final String txnDestination;
403 PostVpnInterfaceThreadWorker(String interfaceName, boolean add, String transactionDest) {
404 this.interfaceName = interfaceName;
406 this.txnDestination = transactionDest;
410 public void onSuccess(Void voidObj) {
412 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} stored into destination {} successfully",
413 interfaceName, txnDestination);
415 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} removed successfully", interfaceName);
420 public void onFailure(Throwable throwable) {
422 LOG.error("InterfaceStateChangeListener: VrfEntries for {} failed to store into destination {}",
423 interfaceName, txnDestination, throwable);
425 LOG.error("InterfaceStateChangeListener: VrfEntries for {} removal failed", interfaceName, throwable);
426 vpnUtil.unsetScheduledToRemoveForVpnInterface(interfaceName);
431 private IntfTransitionState getTransitionState(Interface.OperStatus original , Interface.OperStatus updated) {
432 IntfTransitionState transitionState = stateTable.get(original, updated);
434 if (transitionState == null) {
435 return IntfTransitionState.STATE_IGNORE;
437 return transitionState;
440 private class VpnInterfaceCallBackHandler implements FutureCallback<Void> {
441 private final Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib;
443 VpnInterfaceCallBackHandler(Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib) {
444 this.mapOfRdAndPrefixesForRefreshFib = mapOfRdAndPrefixesForRefreshFib;
448 public void onSuccess(Void voidObj) {
449 mapOfRdAndPrefixesForRefreshFib.forEach((primaryRd, prefixes) -> {
450 prefixes.forEach(prefix -> {
451 fibManager.refreshVrfEntry(primaryRd, prefix);
457 public void onFailure(Throwable throwable) {
458 LOG.debug("write Tx config operation failedTunnelEndPointChangeListener", throwable);