Split transaction lifecycle
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / BatchedExistenceCheck.java
index 8cfbab1182b0cefb87a7cfe8f952ac0119549044..d6d0acdd4685f8c6c9420e1cacb9e630e64821be 100644 (file)
@@ -18,6 +18,8 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
@@ -34,34 +36,35 @@ final class BatchedExistenceCheck {
         this.outstanding = total;
     }
 
-    static BatchedExistenceCheck start(final RestconfStrategy read,
+    static BatchedExistenceCheck start(final DOMTransactionChain transactionChain,
                                        final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
                                        final Collection<? extends NormalizedNode<?, ?>> children) {
         final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
-        for (NormalizedNode<?, ?> child : children) {
-            final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
-            read.exists(datastore, path).addCallback(new FutureCallback<Boolean>() {
-                @Override
-                public void onSuccess(final Boolean result) {
-                    ret.complete(path, result);
-                }
-
-                @Override
-                @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
-                public void onFailure(final Throwable throwable) {
-                    final Exception e;
-                    if (throwable instanceof Exception) {
-                        e = (Exception) throwable;
-                    } else {
-                        e = new ExecutionException(throwable);
+        try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
+            for (NormalizedNode<?, ?> child : children) {
+                final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
+                tx.exists(datastore, path).addCallback(new FutureCallback<Boolean>() {
+                    @Override
+                    public void onSuccess(final Boolean result) {
+                        ret.complete(path, result);
                     }
 
-                    ret.complete(path, ReadFailedException.MAPPER.apply(e));
-                }
-            }, MoreExecutors.directExecutor());
-        }
+                    @Override
+                    @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
+                    public void onFailure(final Throwable throwable) {
+                        final Exception e;
+                        if (throwable instanceof Exception) {
+                            e = (Exception) throwable;
+                        } else {
+                            e = new ExecutionException(throwable);
+                        }
 
-        return ret;
+                        ret.complete(path, ReadFailedException.MAPPER.apply(e));
+                    }
+                }, MoreExecutors.directExecutor());
+            }
+            return ret;
+        }
     }
 
     Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {