Search in sources :

Example 1 with DOMTransactionChain

use of org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain in project controller by opendaylight.

the class DOMTransactionChainTest method testTransactionChainNoConflict.

@Test
public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
    BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
    DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
    assertNotNull(txChain);
    /**
     * We alocate new read-write transaction and write /test.
     */
    DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
    /**
     * First transaction is marked as ready, we are able to allocate chained
     * transactions.
     */
    ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
    /**
     * We alocate chained transaction - read transaction.
     */
    DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
    /**
     * We test if we are able to read data from tx, read should not fail
     * since we are using chained transaction.
     */
    assertTestContainerExists(secondReadTx);
    /**
     * We alocate next transaction, which is still based on first one, but
     * is read-write.
     */
    DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
    /**
     * We commit first transaction.
     */
    assertCommitSuccessful(firstWriteTxFuture);
    /**
     * Allocates transaction from data store.
     */
    DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
    /**
     * We verify transaction is commited to store, container should exists
     * in datastore.
     */
    assertTestContainerExists(storeReadTx);
    /**
     * third transaction is sealed and commited.
     */
    ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
    assertCommitSuccessful(thirdDeleteTxFuture);
    /**
     * We close transaction chain.
     */
    txChain.close();
    listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
}
Also used : DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) DOMDataReadTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) Test(org.junit.Test)

Example 2 with DOMTransactionChain

use of org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain in project controller by opendaylight.

the class DOMTransactionChainTest method testTransactionChainNotSealed.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
    BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
    DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
    assertNotNull(txChain);
    /**
     * We alocate new read-write transaction and write /test
     */
    allocateAndWrite(txChain);
    /**
     * We alocate chained transaction - read transaction, note first one is
     * still not commited to datastore, so this allocation should fail with
     * IllegalStateException.
     */
    try {
        txChain.newReadOnlyTransaction();
        fail("Allocation of secondReadTx should fail with IllegalStateException");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
    }
}
Also used : DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with DOMTransactionChain

use of org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain in project controller by opendaylight.

the class TxchainDomWrite method executeList.

@Override
public void executeList() {
    final LogicalDatastoreType dsType = getDataStoreType();
    final YangInstanceIdentifier pid = YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
    final DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
    DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
    int txSubmitted = 0;
    int writeCnt = 0;
    for (MapEntryNode element : this.list) {
        YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
        if (oper == StartTestInput.Operation.PUT) {
            tx.put(dsType, yid, element);
        } else {
            tx.merge(dsType, yid, element);
        }
        writeCnt++;
        // Start performing the operation; submit the transaction at every n-th operation
        if (writeCnt == writesPerTx) {
            txSubmitted++;
            Futures.addCallback(tx.submit(), new FutureCallback<Void>() {

                @Override
                public void onSuccess(final Void result) {
                    txOk++;
                }

                @Override
                public void onFailure(final Throwable t) {
                    LOG.error("Transaction failed, {}", t);
                    txError++;
                }
            });
            tx = chain.newWriteOnlyTransaction();
            writeCnt = 0;
        }
    }
    try {
        txSubmitted++;
        tx.submit().checkedGet();
        txOk++;
    } catch (final TransactionCommitFailedException e) {
        LOG.error("Transaction failed", e);
        txError++;
    }
    try {
        chain.close();
    } catch (final IllegalStateException e) {
        LOG.error("Transaction close failed,", e);
    }
    LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
}
Also used : DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)

Example 4 with DOMTransactionChain

use of org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain in project controller by opendaylight.

the class TxchainDomDelete method executeList.

@Override
public void executeList() {
    final LogicalDatastoreType dsType = getDataStoreType();
    final org.opendaylight.yangtools.yang.common.QName olId = QName.create(OuterList.QNAME, "id");
    final YangInstanceIdentifier pid = YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
    final DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
    DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
    int txSubmitted = 0;
    int writeCnt = 0;
    for (int l = 0; l < outerListElem; l++) {
        YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, olId, l));
        tx.delete(dsType, yid);
        writeCnt++;
        if (writeCnt == writesPerTx) {
            txSubmitted++;
            Futures.addCallback(tx.submit(), new FutureCallback<Void>() {

                @Override
                public void onSuccess(final Void result) {
                    txOk++;
                }

                @Override
                public void onFailure(final Throwable t) {
                    LOG.error("Transaction failed, {}", t);
                    txError++;
                }
            });
            tx = chain.newWriteOnlyTransaction();
            writeCnt = 0;
        }
    }
    // We need to empty the transaction chain before closing it
    try {
        txSubmitted++;
        tx.submit().checkedGet();
        txOk++;
    } catch (final TransactionCommitFailedException e) {
        LOG.error("Transaction failed", e);
        txError++;
    }
    try {
        chain.close();
    } catch (final IllegalStateException e) {
        LOG.error("Transaction close failed,", e);
    }
    LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
}
Also used : DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) QName(org.opendaylight.yangtools.yang.common.QName) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)

Example 5 with DOMTransactionChain

use of org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain in project controller by opendaylight.

the class LegacyDOMDataBrokerAdapter method createTransactionChain.

@Override
public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
    AtomicReference<DOMTransactionChain> legacyChain = new AtomicReference<>();
    org.opendaylight.mdsal.common.api.TransactionChainListener delegateListener = new org.opendaylight.mdsal.common.api.TransactionChainListener() {

        @SuppressWarnings("rawtypes")
        @Override
        public void onTransactionChainFailed(final org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain, final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?> transaction, final Throwable cause) {
            listener.onTransactionChainFailed(legacyChain.get(), (AsyncTransaction) () -> transaction.getIdentifier(), cause instanceof Exception ? SUBMIT_EX_MAPPER.apply((Exception) cause) : cause);
        }

        @Override
        public void onTransactionChainSuccessful(org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain) {
            listener.onTransactionChainSuccessful(legacyChain.get());
        }
    };
    final org.opendaylight.mdsal.dom.api.DOMTransactionChain delegateChain = delegate().createTransactionChain(delegateListener);
    legacyChain.set(new DOMTransactionChain() {

        @Override
        public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
            return new DOMDataReadOnlyTransactionAdapter(delegateChain.newReadOnlyTransaction());
        }

        @Override
        public DOMDataReadWriteTransaction newReadWriteTransaction() {
            return new DOMDataTransactionAdapter(delegateChain.newReadWriteTransaction());
        }

        @Override
        public DOMDataWriteTransaction newWriteOnlyTransaction() {
            return new DOMDataTransactionAdapter(delegateChain.newWriteOnlyTransaction());
        }

        @Override
        public void close() {
            delegateChain.close();
        }
    });
    return legacyChain.get();
}
Also used : DOMDataReadOnlyTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) AtomicReference(java.util.concurrent.atomic.AtomicReference) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) DataStoreUnavailableException(org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) AsyncTransaction(org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)

Aggregations

DOMTransactionChain (org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain)7 Test (org.junit.Test)4 TransactionCommitFailedException (org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException)4 DOMDataReadWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction)4 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)4 AsyncTransaction (org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction)2 LogicalDatastoreType (org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType)2 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)2 TransactionChainListener (org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener)2 DOMDataReadOnlyTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction)2 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)2 Optional (com.google.common.base.Optional)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 InOrder (org.mockito.InOrder)1 DataStoreUnavailableException (org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException)1