Reuse MD-SAL transation for existence check 93/96393/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 31 May 2021 10:08:26 +0000 (12:08 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 31 May 2021 10:43:51 +0000 (12:43 +0200)
We should not be allocating a new transaction for the existence
check, but rather reuse the existing transaction.

JIRA: NETCONF-781
Change-Id: I8daca2bda91dfe8e99ef3e29686e10276a77387d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/BatchedExistenceCheck.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfTransaction.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PostDataTransactionUtilTest.java

index d6d0acdd4685f8c6c9420e1cacb9e630e64821be..b3b9d862eeebeecf5be73ea5ddc39f5d520a1928 100644 (file)
@@ -18,8 +18,7 @@ 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.mdsal.dom.api.DOMDataTreeReadOperations;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
@@ -36,35 +35,33 @@ final class BatchedExistenceCheck {
         this.outstanding = total;
     }
 
-    static BatchedExistenceCheck start(final DOMTransactionChain transactionChain,
+    static BatchedExistenceCheck start(final DOMDataTreeReadOperations tx,
                                        final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
                                        final Collection<? extends NormalizedNode<?, ?>> children) {
         final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
-        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);
-                    }
-
-                    @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);
-                        }
+        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));
+                @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);
                     }
-                }, MoreExecutors.directExecutor());
-            }
-            return ret;
+
+                    ret.complete(path, ReadFailedException.MAPPER.apply(e));
+                }
+            }, MoreExecutors.directExecutor());
         }
+        return ret;
     }
 
     Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
index a6bfdb322512bd2080b5250bc7480a09d40269c3..76c3ac6678160205a2eb490483dcb46eed35dd00 100644 (file)
@@ -37,7 +37,7 @@ final class MdsalRestconfTransaction extends RestconfTransaction {
     private final DOMTransactionChain transactionChain;
     private DOMDataTreeReadWriteTransaction rwTx;
 
-    MdsalRestconfTransaction(DOMTransactionChain transactionChain) {
+    MdsalRestconfTransaction(final DOMTransactionChain transactionChain) {
         this.transactionChain = requireNonNull(transactionChain);
         this.rwTx = transactionChain.newReadWriteTransaction();
     }
@@ -81,7 +81,7 @@ final class MdsalRestconfTransaction extends RestconfTransaction {
             final Collection<? extends NormalizedNode<?, ?>> children =
                 ((NormalizedNodeContainer<?, ?, ?>) data).getValue();
             final BatchedExistenceCheck check =
-                BatchedExistenceCheck.start(transactionChain, LogicalDatastoreType.CONFIGURATION, path, children);
+                BatchedExistenceCheck.start(verifyNotNull(rwTx), LogicalDatastoreType.CONFIGURATION, path, children);
 
             for (final NormalizedNode<?, ?> child : children) {
                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
index d68b9905330c4ec4d63837ba1b2c556dbe53d0f0..60cae46914734f0968a4f983887b7e3d78cb78ee 100644 (file)
@@ -427,7 +427,7 @@ public class RestconfDataServiceImplTest {
         final YangInstanceIdentifier node =
                 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
         doReturn(immediateFalseFluentFuture())
-                .when(this.read).exists(LogicalDatastoreType.CONFIGURATION, node);
+                .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
         doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
 
index 738d6c8a9671a9c4bf519dc0439fcf06a05358d3..07ffa36c104e968d068c6af3743299abb87089bb 100644 (file)
@@ -36,7 +36,6 @@ import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
@@ -72,8 +71,6 @@ public class PostDataTransactionUtilTest {
     @Mock
     private DOMDataTreeReadWriteTransaction readWrite;
     @Mock
-    private DOMDataTreeReadTransaction read;
-    @Mock
     private UriInfo uriInfo;
     @Mock
     private DOMDataBroker mockDataBroker;
@@ -191,8 +188,7 @@ public class PostDataTransactionUtilTest {
         final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
         final YangInstanceIdentifier node =
                 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
-        doReturn(read).when(this.transactionChain).newReadOnlyTransaction();
-        doReturn(immediateFalseFluentFuture()).when(this.read).exists(LogicalDatastoreType.CONFIGURATION, node);
+        doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
         doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
@@ -206,7 +202,7 @@ public class PostDataTransactionUtilTest {
         assertEquals(201, response.getStatus());
         assertThat(URLDecoder.decode(response.getLocation().toString(), StandardCharsets.UTF_8),
             containsString(identifier.getValue(identifier.keySet().iterator().next()).toString()));
-        verify(this.read).exists(LogicalDatastoreType.CONFIGURATION, node);
+        verify(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
         verify(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
 
         response = PostDataTransactionUtil.postData(this.uriInfo, payload,