Search in sources :

Example 1 with ReadWriteTransaction

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

the class TestUtil method verifyHAOpNode.

static void verifyHAOpNode(Node d1GlobalOpNode, Node haGlobalOpNode, Node d1PsOpNode, Node haPsOpNode, InstanceIdentifier<Node> haId, InstanceIdentifier<Node> d1PsId, InstanceIdentifier<Node> haPsId, DataBroker dataBroker) throws ReadFailedException {
    ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
    TestComparators.compareLogicalSwitches(d1GlobalOpNode, haGlobalOpNode, haId);
    // TestComparators.compareRemoteUcastMacs(d1GlobalOpNode, haGlobalOpNode, haId);
    // TestComparators.compareRemoteMcastMacs(d1GlobalOpNode, haGlobalOpNode, haId);
    TestComparators.compareLocalUcastMacs(d1GlobalOpNode, haGlobalOpNode, haId);
    // TestComparators.compareLocalMcastMacs(d1GlobalOpNode, haGlobalOpNode, haId);
    TestComparators.verifySwitches(haGlobalOpNode, haPsOpNode);
    TestComparators.verifySwitches(d1GlobalOpNode, d1PsOpNode);
// TestComparators.comparePhysicalSwitches(d1PsOpNode, haPsOpNode, d1PsId, haPsId, transaction, "s3",
// d1GlobalOpNode, haGlobalOpNode);
}
Also used : ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction)

Example 2 with ReadWriteTransaction

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

the class OpendaylightToaster method checkStatusAndMakeToast.

private void checkStatusAndMakeToast(final MakeToastInput input, final SettableFuture<RpcResult<MakeToastOutput>> futureResult, final int tries) {
    // Read the ToasterStatus and, if currently Up, try to write the status to Down.
    // If that succeeds, then we essentially have an exclusive lock and can proceed
    // to make toast.
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    FluentFuture<Optional<Toaster>> readFuture = tx.read(OPERATIONAL, TOASTER_IID);
    final ListenableFuture<? extends CommitInfo> commitFuture = Futures.transformAsync(readFuture, toasterData -> {
        ToasterStatus toasterStatus = ToasterStatus.Up;
        if (toasterData.isPresent()) {
            toasterStatus = toasterData.get().getToasterStatus();
        }
        LOG.debug("Read toaster status: {}", toasterStatus);
        if (toasterStatus == ToasterStatus.Up) {
            if (outOfBread()) {
                LOG.debug("Toaster is out of bread");
                tx.cancel();
                return Futures.immediateFailedFuture(new TransactionCommitFailedException("", makeToasterOutOfBreadError()));
            }
            LOG.debug("Setting Toaster status to Down");
            // We're not currently making toast - try to update the status to Down
            // to indicate we're going to make toast. This acts as a lock to prevent
            // concurrent toasting.
            tx.put(OPERATIONAL, TOASTER_IID, buildToaster(ToasterStatus.Down));
            return tx.commit();
        }
        LOG.debug("Oops - already making toast!");
        // Return an error since we are already making toast. This will get
        // propagated to the commitFuture below which will interpret the null
        // TransactionStatus in the RpcResult as an error condition.
        tx.cancel();
        return Futures.immediateFailedFuture(new TransactionCommitFailedException("", makeToasterInUseError()));
    }, MoreExecutors.directExecutor());
    Futures.addCallback(commitFuture, new FutureCallback<CommitInfo>() {

        @Override
        public void onSuccess(final CommitInfo result) {
            // OK to make toast
            currentMakeToastTask.set(executor.submit(new MakeToastTask(input, futureResult)));
        }

        @Override
        public void onFailure(final Throwable ex) {
            if (ex instanceof OptimisticLockFailedException) {
                if (tries - 1 > 0) {
                    LOG.debug("Got OptimisticLockFailedException - trying again");
                    checkStatusAndMakeToast(input, futureResult, tries - 1);
                } else {
                    futureResult.set(RpcResultBuilder.<MakeToastOutput>failed().withError(ErrorType.APPLICATION, ex.getMessage()).build());
                }
            } else if (ex instanceof TransactionCommitFailedException) {
                LOG.debug("Failed to commit Toaster status", ex);
                // Probably already making toast.
                futureResult.set(RpcResultBuilder.<MakeToastOutput>failed().withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());
            } else {
                LOG.debug("Unexpected error committing Toaster status", ex);
                futureResult.set(RpcResultBuilder.<MakeToastOutput>failed().withError(ErrorType.APPLICATION, "Unexpected error committing Toaster status", ex).build());
            }
        }
    }, MoreExecutors.directExecutor());
}
Also used : Optional(java.util.Optional) TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) MakeToastOutput(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput) ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction) ToasterStatus(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo) OptimisticLockFailedException(org.opendaylight.mdsal.common.api.OptimisticLockFailedException)

Example 3 with ReadWriteTransaction

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

the class AbstractStatementRegistryTest method loadStatement.

protected List<Statement> loadStatement(final String policyName) throws ExecutionException, InterruptedException {
    final ReadWriteTransaction rt = getDataBroker().newReadWriteTransaction();
    final PolicyDefinition policy = rt.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(RoutingPolicy.class).child(PolicyDefinitions.class).child(PolicyDefinition.class, new PolicyDefinitionKey(policyName))).get().get();
    return policy.getStatements().getStatement();
}
Also used : PolicyDefinition(org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.PolicyDefinition) ReadWriteTransaction(org.opendaylight.mdsal.binding.api.ReadWriteTransaction) RoutingPolicy(org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy) PolicyDefinitionKey(org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.PolicyDefinitionKey)

Example 4 with ReadWriteTransaction

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

the class ConnectedGraphServer method addToDataStore.

/**
 * Add 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 data  Data Object (Graph, Vertex, Edge or Prefix)
 * @param info  Information to be logged
 */
private synchronized <T extends DataObject> void addToDataStore(final InstanceIdentifier<T> id, final T data, final String info) {
    final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
    trans.put(LogicalDatastoreType.OPERATIONAL, id, data);
    trans.commit().addCallback(new FutureCallback<CommitInfo>() {

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

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("GrahModel: Cannot write {} 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 5 with ReadWriteTransaction

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

the class ConnectedGraphServer method updateToDataStore.

/**
 * Update Graph components (Vertex, Edge or Prefix ) to the Data Store. Old value identified by its Instance ID
 * will be remove first before adding the new value.
 *
 * @param <T>   As a generic method, T must be a Vertex, Edge or Prefix.
 * @param id    Instance Identifier of the Data Object
 * @param data  Data Object (Vertex, Edge or Prefix)
 * @param old   Instance Identifier of the previous version of the Data Object
 * @param info  Information to be logged
 */
private synchronized <T extends DataObject> void updateToDataStore(final InstanceIdentifier<T> id, final T data, final InstanceIdentifier<T> old, final String info) {
    final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
    if (old != null) {
        trans.delete(LogicalDatastoreType.OPERATIONAL, old);
    }
    trans.put(LogicalDatastoreType.OPERATIONAL, id, data);
    trans.commit().addCallback(new FutureCallback<CommitInfo>() {

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

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("GrahModel: Cannot write {} 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)

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