2 * Copyright (c) 2013 Cisco Systems, Inc. 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.bgpcep.bgp.topology.provider;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.bgpcep.topology.TopologyReference;
19 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
20 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.protocol.bgp.rib.RibReference;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public abstract class AbstractTopologyBuilder<T extends Route> implements AutoCloseable, ClusteredDataTreeChangeListener<T>, TopologyReference, TransactionChainListener {
53 private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologyBuilder.class);
54 // we limit the listener reset interval to be 5 min at most
55 private static final long LISTENER_RESET_LIMIT_IN_MILLSEC = 5 * 60 * 1000;
56 private static final int LISTENER_RESET_ENFORCE_COUNTER = 3;
57 private final InstanceIdentifier<Topology> topology;
58 private final RibReference locRibReference;
59 private final DataBroker dataProvider;
60 private final Class<? extends AddressFamily> afi;
61 private final Class<? extends SubsequentAddressFamily> safi;
62 private final TopologyKey topologyKey;
63 private final TopologyTypes topologyTypes;
64 private final long listenerResetLimitInMillsec;
65 private final int listenerResetEnforceCounter;
68 private ListenerRegistration<AbstractTopologyBuilder<T>> listenerRegistration = null;
70 private BindingTransactionChain chain = null;
72 private boolean closed = false;
75 protected long listenerScheduledRestartTime = 0;
78 protected int listenerScheduledRestartEnforceCounter = 0;
80 protected AbstractTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
81 final TopologyId topologyId, final TopologyTypes types, final Class<? extends AddressFamily> afi,
82 final Class<? extends SubsequentAddressFamily> safi, final long listenerResetLimitInMillsec,
83 final int listenerResetEnforceCounter) {
84 this.dataProvider = dataProvider;
85 this.locRibReference = Preconditions.checkNotNull(locRibReference);
86 this.topologyKey = new TopologyKey(Preconditions.checkNotNull(topologyId));
87 this.topologyTypes = types;
90 this.listenerResetLimitInMillsec = listenerResetLimitInMillsec;
91 this.listenerResetEnforceCounter = listenerResetEnforceCounter;
92 this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, this.topologyKey).build();
94 LOG.debug("Initiating topology builder from {} at {}. AFI={}, SAFI={}", locRibReference, this.topology, this.afi, this.safi);
95 initTransactionChain();
96 initOperationalTopology();
97 registerDataChangeListener();
100 protected AbstractTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
101 final TopologyId topologyId, final TopologyTypes types, final Class<? extends AddressFamily> afi,
102 final Class<? extends SubsequentAddressFamily> safi) {
103 this(dataProvider, locRibReference, topologyId, types, afi, safi, LISTENER_RESET_LIMIT_IN_MILLSEC, LISTENER_RESET_ENFORCE_COUNTER);
107 public final InstanceIdentifier<Tables> tableInstanceIdentifier(final Class<? extends AddressFamily> afi,
108 final Class<? extends SubsequentAddressFamily> safi) {
109 return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).build();
113 * Register to data tree change listener
115 private synchronized void registerDataChangeListener() {
116 Preconditions.checkState(this.listenerRegistration == null, "Topology Listener on topology %s has been registered before.", this.getInstanceIdentifier());
117 final InstanceIdentifier<Tables> tablesId = this.locRibReference.getInstanceIdentifier().child(LocRib.class).child(Tables.class, new TablesKey(this.afi, this.safi));
118 final DataTreeIdentifier<T> id = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getRouteWildcard(tablesId));
120 this.listenerRegistration = this.dataProvider.registerDataTreeChangeListener(id, this);
121 LOG.debug("Registered listener {} on topology {}. Timestamp={}", this, this.getInstanceIdentifier(), this.listenerScheduledRestartTime);
125 * Unregister to data tree change listener
127 private final synchronized void unregisterDataChangeListener() {
128 if (this.listenerRegistration != null) {
129 LOG.debug("Unregistered listener {} on topology {}", this, this.getInstanceIdentifier());
130 this.listenerRegistration.close();
131 this.listenerRegistration = null;
135 protected abstract InstanceIdentifier<T> getRouteWildcard(InstanceIdentifier<Tables> tablesId);
137 protected abstract void createObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);
139 protected abstract void removeObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);
142 public final InstanceIdentifier<Topology> getInstanceIdentifier() {
143 return this.topology;
147 public final synchronized void close() throws TransactionCommitFailedException {
149 LOG.trace("Transaction chain was already closed.");
153 LOG.info("Shutting down builder for {}", getInstanceIdentifier());
154 unregisterDataChangeListener();
155 destroyOperationalTopology();
156 destroyTransactionChain();
160 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
162 LOG.trace("Transaction chain was already closed, skipping update.");
165 // check if the transaction chain needed to be restarted due to a previous error
166 if (restartTransactionChainOnDemand()) {
167 LOG.debug("The data change {} is disregarded due to restart of listener {}", changes, this);
170 final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
171 LOG.debug("Received data change {} event with transaction {}", changes, trans.getIdentifier());
172 final AtomicBoolean transactionInError = new AtomicBoolean(false);
173 for (final DataTreeModification<T> change : changes) {
175 routeChanged(change, trans);
176 } catch (final RuntimeException e) {
177 LOG.warn("Data change {} (transaction {}) was not completely propagated to listener {}", change, trans.getIdentifier(), this, e);
178 // trans.cancel() is not supported by PingPongTransactionChain, so we just skip the problematic change
179 // trans.submit() must be called first to unlock the current transaction chain, to make the chain closable
180 // so we cannot exit the #onDataTreeChanged() yet
181 transactionInError.set(true);
185 Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
187 public void onSuccess(final Void result) {
188 // as we are enforcing trans.submit(), in some cases the transaction execution actually could be successfully even when an
189 // exception is captured, thus #onTransactionChainFailed() never get invoked. Though the transaction chain remains usable,
190 // the data loss will not be able to be recovered. Thus we schedule a listener restart here
191 if (transactionInError.get()) {
192 LOG.warn("Transaction {} committed successfully while exception captured. Rescheduling a restart of listener {}", trans
193 .getIdentifier(), AbstractTopologyBuilder.this);
194 scheduleListenerRestart();
196 LOG.trace("Transaction {} committed successfully", trans.getIdentifier());
201 public void onFailure(final Throwable t) {
202 // we do nothing but print out the log. Transaction chain restart will be done in #onTransactionChainFailed()
203 LOG.error("Failed to propagate change (transaction {}) by listener {}", trans.getIdentifier(), AbstractTopologyBuilder.this, t);
209 protected void routeChanged(final DataTreeModification<T> change, final ReadWriteTransaction trans) {
210 final DataObjectModification<T> root = change.getRootNode();
211 switch (root.getModificationType()) {
213 removeObject(trans, change.getRootPath().getRootIdentifier(), root.getDataBefore());
215 case SUBTREE_MODIFIED:
217 if (root.getDataBefore() != null) {
218 removeObject(trans, change.getRootPath().getRootIdentifier(), root.getDataBefore());
220 createObject(trans, change.getRootPath().getRootIdentifier(), root.getDataAfter());
223 throw new IllegalArgumentException("Unhandled modification type " + root.getModificationType());
227 private synchronized void initOperationalTopology() {
228 Preconditions.checkNotNull(this.chain, "A valid transaction chain must be provided.");
229 final WriteTransaction trans = this.chain.newWriteOnlyTransaction();
230 trans.put(LogicalDatastoreType.OPERATIONAL, this.topology,
231 new TopologyBuilder().setKey(this.topologyKey).setServerProvided(Boolean.TRUE).setTopologyTypes(this.topologyTypes)
232 .setLink(Collections.<Link>emptyList()).setNode(Collections.<Node>emptyList()).build(), true);
233 Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
235 public void onSuccess(final Void result) {
236 LOG.trace("Transaction {} committed successfully", trans.getIdentifier());
240 public void onFailure(final Throwable t) {
241 LOG.error("Failed to initialize topology {} (transaction {}) by listener {}", AbstractTopologyBuilder.this.topology,
242 trans.getIdentifier(), AbstractTopologyBuilder.this, t);
248 * Destroy the current operational topology data. Note a valid transaction must be provided
249 * @throws TransactionCommitFailedException
251 private synchronized void destroyOperationalTopology() {
252 Preconditions.checkNotNull(this.chain, "A valid transaction chain must be provided.");
253 final WriteTransaction trans = this.chain.newWriteOnlyTransaction();
254 trans.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier());
256 trans.submit().checkedGet();
257 } catch (TransactionCommitFailedException e) {
258 LOG.error("Unable to reset operational topology {} (transaction {})", this.topology, trans.getIdentifier(), e);
263 * Reset a transaction chain by closing the current chain and starting a new one
265 private synchronized void initTransactionChain() {
266 LOG.debug("Initializing transaction chain for topology {}", this);
267 Preconditions.checkState(this.chain == null, "Transaction chain has to be closed before being initialized");
268 this.chain = this.dataProvider.createTransactionChain(this);
272 * Destroy the current transaction chain
274 private synchronized void destroyTransactionChain() {
275 if (this.chain != null) {
276 LOG.debug("Destroy transaction chain for topology {}", this);
277 // we cannot close the transaction chain, as it will close the AbstractDOMForwardedTransactionFactory
278 // and the transaction factory cannot be reopen even if we recreate the transaction chain
279 // so we abandon the chain directly
280 // FIXME we want to close the transaction chain gracefully once the PingPongTransactionChain get improved
281 // and the above problem get resolved.
283 // this.chain.close();
284 // } catch (Exception e) {
285 // // the close() may not succeed when the transaction chain is locked
286 // LOG.error("Unable to close transaction chain {} for topology builder {}", this.chain, getInstanceIdentifier());
293 * Reset the data change listener to its initial status
294 * By resetting the listener we will be able to recover all the data lost before
297 protected synchronized void resetListener() {
298 Preconditions.checkNotNull(this.listenerRegistration, "Listener on topology %s hasn't been initialized.", this);
299 LOG.debug("Resetting data change listener for topology builder {}", getInstanceIdentifier());
300 // unregister current listener to prevent incoming data tree change first
301 unregisterDataChangeListener();
302 // create new transaction chain to reset the chain status
303 resetTransactionChain();
304 // reset the operational topology data so that we can have clean status
305 destroyOperationalTopology();
306 initOperationalTopology();
307 // re-register the data change listener to reset the operational topology
308 // we are expecting to receive all the pre-exist route change on the next onDataTreeChanged() call
309 registerDataChangeListener();
313 * Reset the transaction chain only so that the PingPong transaction chain will become usable again.
314 * However, there will be data loss if we do not apply the previous failed transaction again
317 protected synchronized void resetTransactionChain() {
318 LOG.debug("Resetting transaction chain for topology builder {}", getInstanceIdentifier());
319 destroyTransactionChain();
320 initTransactionChain();
324 * There are a few reasons we want to schedule a listener restart in a delayed manner:
325 * 1. we should avoid restarting the listener as when the topology is big, there might be huge overhead
326 * rebuilding the whole linkstate topology again and again
327 * 2. the #onTransactionChainFailed() normally get invoked after a delay. During that time gap, more
328 * data changes might still be pushed to #onDataTreeChanged(). And because #onTransactionChainFailed()
329 * is not invoked yet, listener restart/transaction chain restart is not done. Thus the new changes
330 * will still cause error and another #onTransactionChainFailed() might be invoked later. The listener
331 * will be restarted again in that case, which is unexpected. Restarting of transaction chain only introduce
332 * little overhead and it's okay to be restarted within a small time window
334 * Note: when the listener is restarted, we can disregard all the incoming data changes before the restart is
335 * done, as after the listener unregister/reregister, the first #onDataTreeChanged() call will contain the a
336 * complete set of existing changes
338 * @return if the listener get restarted, return true; otherwise false
341 protected synchronized boolean restartTransactionChainOnDemand() {
342 if (this.listenerScheduledRestartTime > 0) {
343 // when the #this.listenerScheduledRestartTime timer timed out we can reset the listener, otherwise we should only reset the transaction chain
344 if (System.currentTimeMillis() > this.listenerScheduledRestartTime) {
345 // reset the the restart timer
346 this.listenerScheduledRestartTime = 0;
347 this.listenerScheduledRestartEnforceCounter = 0;
351 resetTransactionChain();
358 protected synchronized void scheduleListenerRestart() {
359 if (0 == this.listenerScheduledRestartTime) {
360 this.listenerScheduledRestartTime = System.currentTimeMillis() + this.listenerResetLimitInMillsec;
361 } else if (System.currentTimeMillis() > this.listenerScheduledRestartTime
362 && ++this.listenerScheduledRestartEnforceCounter < this.listenerResetEnforceCounter) {
363 // if the transaction failure happens again, we will delay the listener restart up to #LISTENER_RESET_LIMIT_IN_MILLSEC times
364 this.listenerScheduledRestartTime += this.listenerResetLimitInMillsec;
366 LOG.debug("A listener restart was scheduled at {} (current system time is {})", this.listenerScheduledRestartTime, System.currentTimeMillis());
370 public final synchronized void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
371 LOG.error("Topology builder for {} failed in transaction {}.", getInstanceIdentifier(), transaction != null ? transaction.getIdentifier() : null, cause);
372 scheduleListenerRestart();
373 restartTransactionChainOnDemand();
377 public final void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
378 LOG.info("Topology builder for {} shut down", getInstanceIdentifier());