Reduce use of FutureCallbackTx.addCallback() 87/107087/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 15:55:53 +0000 (17:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 16:03:25 +0000 (18:03 +0200)
Do not use addCallback() for checking the existence of data. This
inlines the synchronous call and allows it to be be further composed.

Change-Id: I6ea04ff197ae24626fc5ffb409902d1b13dd664d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/FutureCallbackTx.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PostDataTransactionUtil.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PutDataTransactionUtil.java

index c5089865a6f404e6c122a595b65c8c63918a7284..548c9be4f5c8265f03ac8212d21781e4a9cde276 100644 (file)
@@ -31,24 +31,6 @@ final class FutureCallbackTx {
         // Hidden on purpose
     }
 
-    /**
-     * Add callback to the future object.
-     *
-     * @param listenableFuture
-     *             future object
-     * @param txType
-     *             type of operation (READ, POST, PUT, DELETE)
-     * @param dataFactory
-     *             factory setting result
-     * @throws RestconfDocumentedException
-     *             if the Future throws an exception
-     */
-    // FIXME: this is a *synchronous operation* and has to die
-    static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType,
-                                final FutureDataFactory<? super T> dataFactory) throws RestconfDocumentedException {
-        addCallback(listenableFuture, txType, dataFactory, null);
-    }
-
     /**
      * Add callback to the future object and close transaction chain.
      *
index 2f2ddbad89bd651163fb2740c7f5a463adb24d06..a9d5d3f9b367f52470bf481b4ad6680d8ff28220 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.net.URI;
+import java.util.concurrent.ExecutionException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
@@ -207,18 +208,25 @@ public final class PostDataTransactionUtil {
     }
 
     /**
-     * Check if items do NOT already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
-     * data already exists.
+     * Check if items do NOT already exists at specified {@code path}.
      *
-     * @param isExistsFuture if checked data exists
-     * @param path           Path to be checked
+     * @param existsFuture if checked data exists
+     * @param path         Path to be checked
+     * @throws RestconfDocumentedException if data already exists.
      */
-    public static void checkItemDoesNotExists(final ListenableFuture<Boolean> isExistsFuture,
+    public static void checkItemDoesNotExists(final ListenableFuture<Boolean> existsFuture,
                                               final YangInstanceIdentifier path) {
-        final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
-        FutureCallbackTx.addCallback(isExistsFuture, POST_TX_TYPE, response);
+        final boolean exists;
+        try {
+            exists = existsFuture.get();
+        } catch (ExecutionException e) {
+            throw new RestconfDocumentedException("Failed to access " + path, e);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
+        }
 
-        if (response.result) {
+        if (exists) {
             LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
             throw new RestconfDocumentedException(
                 "Data already exists", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, path);
index 424fd376c0adcffaa3c9c1e9d832ed63c5352ee0..b66392175e8cca0f5869e3508c5d0fbc7e538ac6 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 import com.google.common.util.concurrent.FluentFuture;
-import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.ExecutionException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import org.opendaylight.mdsal.common.api.CommitInfo;
@@ -59,12 +59,19 @@ public final class PutDataTransactionUtil {
                                    final RestconfStrategy strategy, final WriteDataParams params) {
         final YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
 
-        final ListenableFuture<Boolean> existsFuture = strategy.exists(LogicalDatastoreType.CONFIGURATION, path);
-        final FutureDataFactory<Boolean> existsResponse = new FutureDataFactory<>();
-        FutureCallbackTx.addCallback(existsFuture, PUT_TX_TYPE, existsResponse);
+        final var existsFuture = strategy.exists(LogicalDatastoreType.CONFIGURATION, path);
+        final boolean exists;
+        try {
+            exists = existsFuture.get();
+        } catch (ExecutionException e) {
+            throw new RestconfDocumentedException("Failed to access " + path, e);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
+        }
 
         final ResponseFactory responseFactory =
-            new ResponseFactory(existsResponse.result ? Status.NO_CONTENT : Status.CREATED);
+            new ResponseFactory(exists ? Status.NO_CONTENT : Status.CREATED);
         final FluentFuture<? extends CommitInfo> submitData = submitData(path, schemaContext, strategy,
             payload.getData(), params);
         //This method will close transactionChain if any