*/
package org.opendaylight.bgpcep.bgp.topology.provider;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
-
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.ExecutionException;
-
+import javax.annotation.concurrent.GuardedBy;
import org.opendaylight.bgpcep.topology.TopologyReference;
import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
private final RibReference locRibReference;
private final Class<T> idClass;
+ @GuardedBy("this")
+ private boolean closed = false;
+
protected AbstractTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
final TopologyId topologyId, final TopologyTypes types, final Class<T> idClass) {
this.locRibReference = Preconditions.checkNotNull(locRibReference);
this.chain = dataProvider.createTransactionChain(this);
final TopologyKey tk = new TopologyKey(Preconditions.checkNotNull(topologyId));
- this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).toInstance();
+ this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).build();
LOG.debug("Initiating topology builder from {} at {}", locRibReference, this.topology);
- final ReadWriteTransaction t = this.chain.newReadWriteTransaction();
- final Optional<Topology> o;
- try {
- o = t.read(LogicalDatastoreType.OPERATIONAL, this.topology).get();
- } catch (InterruptedException | ExecutionException e) {
- throw new IllegalStateException("Failed to read topology " + topology, e);
- }
- Preconditions.checkState(!o.isPresent(), "Data provider conflict detected on object {}", this.topology);
+ final WriteTransaction t = this.chain.newWriteOnlyTransaction();
t.put(LogicalDatastoreType.OPERATIONAL, this.topology,
new TopologyBuilder().setKey(tk).setServerProvided(Boolean.TRUE).setTopologyTypes(types)
- .setLink(Collections.<Link>emptyList()).setNode(Collections.<Node>emptyList()).build());
+ .setLink(Collections.<Link>emptyList()).setNode(Collections.<Node>emptyList()).build(), true);
Futures.addCallback(t.submit(), new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
public final InstanceIdentifier<Tables> tableInstanceIdentifier(final Class<? extends AddressFamily> afi,
final Class<? extends SubsequentAddressFamily> safi) {
- return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).toInstance();
+ return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).build();
}
protected abstract void createObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);
}
@Override
- public final void onLocRIBChange(final ReadWriteTransaction trans,
+ public final synchronized void onLocRIBChange(final ReadWriteTransaction trans,
final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
LOG.debug("Received data change {} event with transaction {}", event, trans.getIdentifier());
+ if (this.closed) {
+ LOG.trace("Transaction chain was already closed, skipping update.");
+ return;
+ }
// FIXME: speed this up
final Set<InstanceIdentifier<T>> ids = new HashSet<>();
}
@Override
- public final void close() throws TransactionCommitFailedException {
+ public final synchronized void close() throws TransactionCommitFailedException {
LOG.info("Shutting down builder for {}", getInstanceIdentifier());
final WriteTransaction trans = this.chain.newWriteOnlyTransaction();
trans.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier());
trans.submit().checkedGet();
this.chain.close();
+ this.closed = true;
}
@Override