use of org.opendaylight.controller.md.sal.common.api.data.ReadFailedException in project controller by opendaylight.
the class AbstractReadWriteTransaction method ensureParentsByMerge.
@Override
protected final void ensureParentsByMerge(final LogicalDatastoreType store, final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier normalizedPath, final InstanceIdentifier<?> path) {
List<PathArgument> currentArguments = new ArrayList<>();
DataNormalizationOperation<?> currentOp = getCodec().getDataNormalizer().getRootOperation();
Iterator<PathArgument> iterator = normalizedPath.getPathArguments().iterator();
while (iterator.hasNext()) {
PathArgument currentArg = iterator.next();
try {
currentOp = currentOp.getChild(currentArg);
} catch (DataNormalizationException e) {
throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e);
}
currentArguments.add(currentArg);
org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier currentPath = org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create(currentArguments);
final Boolean exists;
try {
exists = getDelegate().exists(store, currentPath).checkedGet();
} catch (ReadFailedException e) {
LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e);
throw new IllegalStateException("Failed to read pre-existing data", e);
}
if (!exists && iterator.hasNext()) {
getDelegate().merge(store, currentPath, currentOp.createDefault(currentArg));
}
}
}
use of org.opendaylight.controller.md.sal.common.api.data.ReadFailedException in project controller by opendaylight.
the class SnapshotBackedReadTransaction method read.
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
checkNotNull(path, "Path must not be null.");
final DataTreeSnapshot snapshot = stableSnapshot;
if (snapshot == null) {
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
}
try {
return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
} catch (RuntimeException e) {
LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
}
}
use of org.opendaylight.controller.md.sal.common.api.data.ReadFailedException in project controller by opendaylight.
the class SnapshotBackedReadWriteTransaction method read.
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
checkNotNull(path, "Path must not be null.");
final Optional<NormalizedNode<?, ?>> result;
try {
result = readSnapshotNode(path);
} catch (RuntimeException e) {
LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
}
if (result == null) {
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
} else {
return Futures.immediateCheckedFuture(result);
}
}
use of org.opendaylight.controller.md.sal.common.api.data.ReadFailedException 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));
}
use of org.opendaylight.controller.md.sal.common.api.data.ReadFailedException in project controller by opendaylight.
the class LegacyDOMDataBrokerAdapterTest method testReadWriteTransaction.
@Test
public void testReadWriteTransaction() throws Exception {
DOMDataReadWriteTransaction tx = adapter.newReadWriteTransaction();
CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = tx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
Optional<NormalizedNode<?, ?>> readOptional = readFuture.get();
assertEquals("isPresent", true, readOptional.isPresent());
assertEquals("NormalizedNode", dataNode, readOptional.get());
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
verify(mockReadWriteTx).write(TestModel.TEST_PATH, dataNode);
CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
submitFuture.get(5, TimeUnit.SECONDS);
InOrder inOrder = inOrder(mockCommitCohort);
inOrder.verify(mockCommitCohort).canCommit();
inOrder.verify(mockCommitCohort).preCommit();
inOrder.verify(mockCommitCohort).commit();
}
Aggregations