Search in sources :

Example 6 with ReadWriteTransaction

use of org.opendaylight.mdsal.binding.api.ReadWriteTransaction in project bgpcep by opendaylight.

the class NodeChangedListener method onDataTreeChanged.

@Override
public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
    final ReadWriteTransaction trans = this.dataProvider.newReadWriteTransaction();
    final Set<InstanceIdentifier<ReportedLsp>> lsps = new HashSet<>();
    final Set<InstanceIdentifier<Node>> nodes = new HashSet<>();
    final Map<InstanceIdentifier<?>, DataObject> original = new HashMap<>();
    final Map<InstanceIdentifier<?>, DataObject> updated = new HashMap<>();
    final Map<InstanceIdentifier<?>, DataObject> created = new HashMap<>();
    for (final DataTreeModification<?> change : changes) {
        final InstanceIdentifier<?> iid = change.getRootPath().getRootIdentifier();
        final DataObjectModification<?> rootNode = change.getRootNode();
        handleChangedNode(rootNode, iid, lsps, nodes, original, updated, created);
    }
    // Now walk all nodes, check for removals/additions and cascade them to LSPs
    for (final InstanceIdentifier<Node> iid : nodes) {
        enumerateLsps(iid, (Node) original.get(iid), lsps);
        enumerateLsps(iid, (Node) updated.get(iid), lsps);
        enumerateLsps(iid, (Node) created.get(iid), lsps);
    }
    // We now have list of all affected LSPs. Walk them create/remove them
    updateTransaction(trans, lsps, original, updated, created);
    trans.commit().addCallback(new FutureCallback<CommitInfo>() {

        @Override
        public void onSuccess(final CommitInfo result) {
            LOG.trace("Topology change committed successfully");
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("Failed to propagate a topology change, target topology became inconsistent", throwable);
        }
    }, MoreExecutors.directExecutor());
}
Also used : HashMap(java.util.HashMap) SupportingNode(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode) Node(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo) HashSet(java.util.HashSet)

Example 7 with ReadWriteTransaction

use of org.opendaylight.mdsal.binding.api.ReadWriteTransaction in project bgpcep by opendaylight.

the class ConnectedGraphServer method removeFromDataStore.

/**
 * Remove Graph or Graph components to the Data Store.
 *
 * @param <T>  As a generic method, T must be a Graph, Vertex, Edge or Prefix.
 * @param id   Instance Identifier of the Data Object
 * @param info Information to be logged
 */
private synchronized <T extends DataObject> void removeFromDataStore(final InstanceIdentifier<T> id, final String info) {
    final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
    trans.delete(LogicalDatastoreType.OPERATIONAL, id);
    trans.commit().addCallback(new FutureCallback<CommitInfo>() {

        @Override
        public void onSuccess(final CommitInfo result) {
            LOG.info("GraphModel: {} has been deleted in operational datastore ", info);
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("GraphModel: Cannot delete {} to the operational datastore (transaction: {})", info, trans.getIdentifier());
        }
    }, MoreExecutors.directExecutor());
}
Also used : ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo)

Example 8 with ReadWriteTransaction

use of org.opendaylight.mdsal.binding.api.ReadWriteTransaction in project bgpcep by opendaylight.

the class AbstractTopologyBuilder method onDataTreeChanged.

@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public synchronized void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
    if (networkTopologyTransaction) {
        if (this.closed.get()) {
            LOG.trace("Transaction chain was already closed, skipping update.");
            return;
        }
        // check if the transaction chain needed to be restarted due to a previous error
        if (restartTransactionChainOnDemand()) {
            LOG.debug("The data change {} is disregarded due to restart of listener {}", changes, this);
            return;
        }
        final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
        LOG.trace("Received data change {} event with transaction {}", changes, trans.getIdentifier());
        final AtomicBoolean transactionInError = new AtomicBoolean(false);
        for (final DataTreeModification<T> change : changes) {
            try {
                routeChanged(change, trans);
            } catch (final RuntimeException exc) {
                LOG.warn("Data change {} (transaction {}) was not completely propagated to listener {}", change, trans.getIdentifier(), this, exc);
                // trans.cancel() is not supported by PingPongTransactionChain, so we just skip the problematic
                // change.
                // trans.commit() must be called first to unlock the current transaction chain, to make the chain
                // closable so we cannot exit the #onDataTreeChanged() yet
                transactionInError.set(true);
                break;
            }
        }
        trans.commit().addCallback(new FutureCallback<CommitInfo>() {

            @Override
            public void onSuccess(final CommitInfo result) {
                // the data loss will not be able to be recovered. Thus we schedule a listener restart here
                if (transactionInError.get()) {
                    LOG.warn("Transaction {} committed successfully while exception captured. Rescheduling a" + " restart of listener {}", trans.getIdentifier(), AbstractTopologyBuilder.this);
                    scheduleListenerRestart();
                } else {
                    LOG.trace("Transaction {} committed successfully", trans.getIdentifier());
                }
            }

            @Override
            public void onFailure(final Throwable throwable) {
                // we do nothing but print out the log. Transaction chain restart will be done in
                // #onTransactionChainFailed()
                LOG.error("Failed to propagate change (transaction {}) by listener {}", trans.getIdentifier(), AbstractTopologyBuilder.this, throwable);
            }
        }, MoreExecutors.directExecutor());
    } else {
        for (final DataTreeModification<T> change : changes) {
            routeChanged(change, null);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo)

Aggregations

ReadWriteTransaction (org.opendaylight.mdsal.binding.api.ReadWriteTransaction)8 CommitInfo (org.opendaylight.mdsal.common.api.CommitInfo)6 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 OptimisticLockFailedException (org.opendaylight.mdsal.common.api.OptimisticLockFailedException)1 TransactionCommitFailedException (org.opendaylight.mdsal.common.api.TransactionCommitFailedException)1 MakeToastOutput (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput)1 ToasterStatus (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus)1 RoutingPolicy (org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy)1 PolicyDefinition (org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.PolicyDefinition)1 PolicyDefinitionKey (org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.PolicyDefinitionKey)1 Node (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node)1 SupportingNode (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode)1 DataObject (org.opendaylight.yangtools.yang.binding.DataObject)1 InstanceIdentifier (org.opendaylight.yangtools.yang.binding.InstanceIdentifier)1