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 com.google.common.base.Optional;
11 import com.google.common.collect.HashBasedTable;
12 import com.google.common.collect.Table;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
28 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class InterfaceStateChangeListener
42 extends AsyncDataTreeChangeListenerBase<Interface, InterfaceStateChangeListener> {
44 private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
45 private static final short DJC_MAX_RETRIES = 3;
46 private final DataBroker dataBroker;
47 private final ManagedNewTransactionRunner txRunner;
48 private final VpnInterfaceManager vpnInterfaceManager;
49 private final JobCoordinator jobCoordinator;
51 Table<OperStatus, OperStatus, IntfTransitionState> stateTable = HashBasedTable.create();
53 enum IntfTransitionState {
59 private void initialize() {
60 // Interface State Transition Table
62 // ---------------------------------------------------------------
63 /* Up { STATE_IGNORE, STATE_DOWN, STATE_IGNORE }, */
64 /* Down { STATE_UP, STATE_IGNORE, STATE_IGNORE }, */
65 /* Unknown { STATE_UP, STATE_DOWN, STATE_IGNORE }, */
67 stateTable.put(Interface.OperStatus.Up, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
68 stateTable.put(Interface.OperStatus.Down, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
69 stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
70 stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
74 public InterfaceStateChangeListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
75 final JobCoordinator jobCoordinator) {
76 super(Interface.class, InterfaceStateChangeListener.class);
77 this.dataBroker = dataBroker;
78 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
79 this.vpnInterfaceManager = vpnInterfaceManager;
80 this.jobCoordinator = jobCoordinator;
86 LOG.info("{} start", getClass().getSimpleName());
87 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
92 protected InstanceIdentifier<Interface> getWildCardPath() {
93 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
97 protected InterfaceStateChangeListener getDataTreeChangeListener() {
98 return InterfaceStateChangeListener.this;
103 // TODO Clean up the exception handling
104 @SuppressWarnings("checkstyle:IllegalCatch")
105 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
107 if (L2vlan.class.equals(intrf.getType())) {
108 LOG.info("VPN Interface add event - intfName {} from InterfaceStateChangeListener",
110 jobCoordinator.enqueueJob("VPNINTERFACE-" + intrf.getName(), () -> {
111 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
112 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
113 ListenableFuture<Void> configFuture
114 = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
115 ListenableFuture<Void> operFuture
116 = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> {
117 final String interfaceName = intrf.getName();
118 LOG.info("Detected interface add event for interface {}", interfaceName);
119 final VpnInterface vpnIf =
120 VpnUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
122 for (VpnInstanceNames vpnInterfaceVpnInstance :
123 vpnIf.getVpnInstanceNames()) {
124 String vpnName = vpnInterfaceVpnInstance.getVpnName();
125 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
126 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
127 LOG.info("VPN Interface add event - intfName {} onto vpnName {} "
128 + "running oper-driven, VpnInstance not ready, holding"
129 + " on", vpnIf.getName(), vpnName);
130 } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
131 LOG.error("add: Ignoring addition of vpnInterface {}, as"
132 + " vpnInstance {} with primaryRd {} is already marked for"
133 + " deletion", interfaceName, vpnName, primaryRd);
135 BigInteger intfDpnId = BigInteger.ZERO;
137 intfDpnId = InterfaceUtils.getDpIdFromInterface(intrf);
138 } catch (Exception e) {
139 LOG.error("Unable to retrieve dpnId for interface {}. "
140 + "Process vpn interface add failed",intrf.getName(),
144 final BigInteger dpnId = intfDpnId;
145 final int ifIndex = intrf.getIfIndex();
146 LOG.info("VPN Interface add event - intfName {} onto vpnName {}"
147 + " running oper-driven", vpnIf.getName(), vpnName);
148 vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
149 ifIndex, false, writeConfigTxn, writeOperTxn, writeInvTxn,
157 futures.add(operFuture);
158 operFuture.get(); //Synchronous submit of operTxn
160 futures.add(configFuture);
161 //TODO: Allow immediateFailedFuture from writeCfgTxn to cancel writeInvTxn as well.
162 Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), true,
168 } catch (Exception e) {
169 LOG.error("Exception caught in Interface {} Operational State Up event", intrf.getName(), e);
174 // TODO Clean up the exception handling
175 @SuppressWarnings("checkstyle:IllegalCatch")
176 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
177 final String ifName = intrf.getName();
178 BigInteger dpId = BigInteger.ZERO;
180 if (L2vlan.class.equals(intrf.getType())) {
181 LOG.info("VPN Interface remove event - intfName {} from InterfaceStateChangeListener",
184 dpId = InterfaceUtils.getDpIdFromInterface(intrf);
185 } catch (Exception e) {
186 LOG.error("Unable to retrieve dpnId from interface operational data store for interface"
187 + " {}. Fetching from vpn interface op data store. ", ifName, e);
189 final BigInteger inputDpId = dpId;
190 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
191 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
192 ListenableFuture<Void> configFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(
193 writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
194 writeOperTxn -> futures.add(
195 txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
196 VpnInterface cfgVpnInterface =
197 VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
198 if (cfgVpnInterface == null) {
199 LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
202 for (VpnInstanceNames vpnInterfaceVpnInstance :
203 cfgVpnInterface.getVpnInstanceNames()) {
204 String vpnName = vpnInterfaceVpnInstance.getVpnName();
205 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
206 VpnUtil.getVpnInterfaceOpDataEntry(dataBroker, ifName, vpnName);
207 if (!optVpnInterface.isPresent()) {
208 LOG.debug("Interface {} vpn {} is not a vpninterface, or deletion"
209 + " triggered by northbound agent. ignoring.", ifName, vpnName);
212 final VpnInterfaceOpDataEntry vpnInterface = optVpnInterface.get();
213 String gwMac = intrf.getPhysAddress() != null ? intrf.getPhysAddress()
214 .getValue() : vpnInterface.getGatewayMacAddress();
215 BigInteger dpnId = inputDpId;
216 if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
217 dpnId = vpnInterface.getDpnId();
219 final int ifIndex = intrf.getIfIndex();
220 LOG.info("VPN Interface remove event - intfName {} onto vpnName {}"
221 + " running oper-driver", vpnInterface.getName(), vpnName);
222 vpnInterfaceManager.processVpnInterfaceDown(dpnId, ifName, ifIndex, gwMac,
223 vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
226 futures.add(configFuture);
227 Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), false,
232 } catch (Exception e) {
233 LOG.error("Exception observed in handling deletion of VPN Interface {}. ", ifName, e);
237 // TODO Clean up the exception handling
238 @SuppressWarnings("checkstyle:IllegalCatch")
240 protected void update(InstanceIdentifier<Interface> identifier,
241 Interface original, Interface update) {
242 final String ifName = update.getName();
244 if (update.getIfIndex() == null) {
247 if (L2vlan.class.equals(update.getType())) {
248 LOG.info("VPN Interface update event - intfName {} from InterfaceStateChangeListener",
250 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
251 List<ListenableFuture<Void>> futures = new ArrayList<>(3);
252 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> futures.add(
253 txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> futures.add(
254 txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
255 final VpnInterface vpnIf =
256 VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
258 final int ifIndex = update.getIfIndex();
259 BigInteger dpnId = BigInteger.ZERO;
261 dpnId = InterfaceUtils.getDpIdFromInterface(update);
262 } catch (Exception e) {
263 LOG.error("remove: Unable to retrieve dpnId for interface {}", ifName, e);
266 IntfTransitionState state = getTransitionState(original.getOperStatus(),
267 update.getOperStatus());
268 if (state.equals(IntfTransitionState.STATE_IGNORE)) {
269 LOG.info("InterfaceStateChangeListener: Interface {} state original {}"
270 + "updated {} not handled", ifName, original.getOperStatus(),
271 update.getOperStatus());
274 if (state.equals(IntfTransitionState.STATE_UP)) {
275 for (VpnInstanceNames vpnInterfaceVpnInstance :
276 vpnIf.getVpnInstanceNames()) {
277 String vpnName = vpnInterfaceVpnInstance.getVpnName();
278 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
279 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
281 "VPN Interface update event - intfName {} onto vpnName {} "
282 + "running oper-driven UP, VpnInstance not ready,"
283 + " holding on", vpnIf.getName(), vpnName);
284 } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
285 LOG.error("update: Ignoring UP event for vpnInterface {}, as "
286 + "vpnInstance {} with primaryRd {} is already marked for"
287 + " deletion", vpnIf.getName(), vpnName, primaryRd);
289 vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
290 ifIndex, true, writeConfigTxn, writeOperTxn, writeInvTxn,
294 } else if (state.equals(IntfTransitionState.STATE_DOWN)) {
295 for (VpnInstanceNames vpnInterfaceVpnInstance :
296 vpnIf.getVpnInstanceNames()) {
297 String vpnName = vpnInterfaceVpnInstance.getVpnName();
298 LOG.info("VPN Interface update event - intfName {} onto vpnName {}"
299 + " running oper-driven DOWN", vpnIf.getName(), vpnName);
300 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
301 VpnUtil.getVpnInterfaceOpDataEntry(dataBroker,
302 vpnIf.getName(), vpnName);
303 if (optVpnInterface.isPresent()) {
304 VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
305 vpnInterfaceManager.processVpnInterfaceDown(dpnId, vpnIf.getName(),
306 ifIndex, update.getPhysAddress().getValue(), vpnOpInterface,
307 true, writeConfigTxn, writeOperTxn, writeInvTxn);
310 "InterfaceStateChangeListener Update DOWN - vpnInterface {}"
311 + " not available, ignoring event", vpnIf.getName());
317 LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
323 } catch (Exception e) {
324 LOG.error("Exception observed in handling updation of VPN Interface {}. ", update.getName(), e);
328 private class PostVpnInterfaceThreadWorker implements FutureCallback<Void> {
329 private final String interfaceName;
330 private final boolean add;
331 private final String txnDestination;
333 PostVpnInterfaceThreadWorker(String interfaceName, boolean add, String transactionDest) {
334 this.interfaceName = interfaceName;
336 this.txnDestination = transactionDest;
340 public void onSuccess(Void voidObj) {
342 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} stored into destination {} successfully",
343 interfaceName, txnDestination);
345 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} removed successfully", interfaceName);
350 public void onFailure(Throwable throwable) {
352 LOG.error("InterfaceStateChangeListener: VrfEntries for {} failed to store into destination {}",
353 interfaceName, txnDestination, throwable);
355 LOG.error("InterfaceStateChangeListener: VrfEntries for {} removal failed", interfaceName, throwable);
356 VpnUtil.unsetScheduledToRemoveForVpnInterface(txRunner, interfaceName);
361 private IntfTransitionState getTransitionState(Interface.OperStatus original , Interface.OperStatus updated) {
362 IntfTransitionState transitionState = stateTable.get(original, updated);
364 if (transitionState == null) {
365 return IntfTransitionState.STATE_IGNORE;
367 return transitionState;