use of org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction 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();
}
use of org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction in project controller by opendaylight.
the class ShardedDOMDataBrokerDelegatingTransactionChainTest method testTransactionChainFailed.
@Test
public void testTransactionChainFailed() {
final DOMDataTreeWriteTransaction writeTxDelegate = mock(DOMDataTreeWriteTransaction.class);
doReturn("DELEGATE-WRITE-TX-1").when(writeTxDelegate).getIdentifier();
doReturn(writeTxDelegate).when(delegateTxChain).newWriteOnlyTransaction();
doNothing().when(txChainlistener).onTransactionChainFailed(any(), any(), any());
// verify writetx fail
txChain.newWriteOnlyTransaction();
txChain.onTransactionChainFailed(delegateTxChain, writeTxDelegate, new Throwable("Fail"));
final ArgumentCaptor<AsyncTransaction> txCaptor = ArgumentCaptor.forClass(AsyncTransaction.class);
final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(txChainlistener).onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
assertEquals("DOM-CHAIN-1-0", txCaptor.getValue().getIdentifier());
assertEquals("Fail", throwableCaptor.getValue().getMessage());
// verify readtx fail
final DOMDataTreeReadTransaction readTxDelegate = mock(DOMDataTreeReadTransaction.class);
doReturn("DELEGATE-READ-TX-1").when(readTxDelegate).getIdentifier();
doReturn(readTxDelegate).when(delegateTxChain).newReadOnlyTransaction();
doNothing().when(txChainlistener).onTransactionChainFailed(any(), any(), any());
txChain.newReadOnlyTransaction();
txChain.onTransactionChainFailed(delegateTxChain, readTxDelegate, new Throwable("Fail"));
verify(txChainlistener, times(2)).onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
assertEquals("DOM-CHAIN-1-1", txCaptor.getValue().getIdentifier());
assertEquals("Fail", throwableCaptor.getValue().getMessage());
// verify readwritetx fail, we must check both read and write failure
// translates to returned readwritetx
// we can reuse write and read tx delegates, just return different
// identifiers to avoid conflicts in keys in tx dictionary
doReturn("DELEGATE-WRITE-RWTX-1").when(writeTxDelegate).getIdentifier();
doReturn("DELEGATE-READ-RWTX-1").when(readTxDelegate).getIdentifier();
txChain.newReadWriteTransaction();
txChain.onTransactionChainFailed(delegateTxChain, writeTxDelegate, new Throwable("Fail"));
verify(txChainlistener, times(3)).onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
assertEquals("DOM-CHAIN-1-2", txCaptor.getValue().getIdentifier());
assertEquals("Fail", throwableCaptor.getValue().getMessage());
txChain.onTransactionChainFailed(delegateTxChain, readTxDelegate, new Throwable("Fail"));
verify(txChainlistener, times(4)).onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
assertEquals("DOM-CHAIN-1-2", txCaptor.getValue().getIdentifier());
assertEquals("Fail", throwableCaptor.getValue().getMessage());
}
use of org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction in project controller by opendaylight.
the class LegacyDOMDataBrokerAdapterTest method testTransactionChain.
@SuppressWarnings("rawtypes")
@Test
public void testTransactionChain() throws Exception {
TransactionChainListener mockListener = mock(TransactionChainListener.class);
doNothing().when(mockListener).onTransactionChainSuccessful(anyObject());
doNothing().when(mockListener).onTransactionChainFailed(anyObject(), anyObject(), anyObject());
DOMTransactionChain chain = adapter.createTransactionChain(mockListener);
// Test read-only tx
DOMDataReadOnlyTransaction readTx = chain.newReadOnlyTransaction();
CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = readTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
Optional<NormalizedNode<?, ?>> readOptional = readFuture.get();
assertEquals("isPresent", true, readOptional.isPresent());
assertEquals("NormalizedNode", dataNode, readOptional.get());
// Test write-only tx
DOMDataWriteTransaction writeTx = chain.newWriteOnlyTransaction();
writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
verify(mockWriteTx).write(TestModel.TEST_PATH, dataNode);
CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
submitFuture.get(5, TimeUnit.SECONDS);
InOrder inOrder = inOrder(mockCommitCohort);
inOrder.verify(mockCommitCohort).canCommit();
inOrder.verify(mockCommitCohort).preCommit();
inOrder.verify(mockCommitCohort).commit();
// Test read-write tx
DOMDataReadWriteTransaction readWriteTx = chain.newReadWriteTransaction();
readFuture = readWriteTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
readOptional = readFuture.get();
assertEquals("isPresent", true, readOptional.isPresent());
assertEquals("NormalizedNode", dataNode, readOptional.get());
chain.close();
verify(mockListener).onTransactionChainSuccessful(chain);
// Test failed chain
doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.TransactionCommitFailedException("mock", (Throwable) null))).when(mockCommitCohort).canCommit();
chain = adapter.createTransactionChain(mockListener);
writeTx = chain.newWriteOnlyTransaction();
try {
writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
fail("Expected TransactionCommitFailedException");
} catch (TransactionCommitFailedException e) {
// expected
}
ArgumentCaptor<AsyncTransaction> failedTx = ArgumentCaptor.forClass(AsyncTransaction.class);
verify(mockListener).onTransactionChainFailed(eq(chain), failedTx.capture(), any(TransactionCommitFailedException.class));
}
Aggregations