Centralize access decoding 05/107105/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 22:42:10 +0000 (00:42 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 22:42:10 +0000 (00:42 +0200)
We have a number of places which perform access syncing, duplicating
code. Introduce TransactionUtil.syncAccess() to deal with them.

Change-Id: I645cbee00c3a28fd5bbf52683e5253f6f096d11f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfTransaction.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
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/TransactionUtil.java

index 1da4a25aefcdf35ea3e109bde8e8275a0c5abccc..612ba41b94f4b03c2fc8e7411e2e33db7b97613d 100644 (file)
@@ -14,7 +14,6 @@ import static org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransacti
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.Collection;
 import java.util.Map;
-import java.util.concurrent.ExecutionException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
@@ -54,18 +53,7 @@ final class MdsalRestconfTransaction extends RestconfTransaction {
 
     @Override
     public void delete(final YangInstanceIdentifier path) {
-        final var existsFuture = verifyNotNull(rwTx).exists(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);
-        }
-
-        if (!exists) {
+        if (!TransactionUtil.syncAccess(verifyNotNull(rwTx).exists(CONFIGURATION, path), path)) {
             LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
             throw new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
                 path);
index cff96ea2348a27977035d33323de6d8551a8d799..1e8c4b22a04373993017da374f4e409aaa36d58b 100644 (file)
@@ -10,7 +10,6 @@ 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.UriInfo;
 import org.opendaylight.mdsal.common.api.CommitInfo;
@@ -206,20 +205,10 @@ public final class PostDataTransactionUtil {
      */
     public static void checkItemDoesNotExists(final ListenableFuture<Boolean> existsFuture,
                                               final YangInstanceIdentifier 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);
-        }
-
-        if (exists) {
+        if (TransactionUtil.syncAccess(existsFuture, path)) {
             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);
+            throw new RestconfDocumentedException("Data already exists", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS,
+                path);
         }
     }
 }
index 6c617cf55d629a5aae8fca08b82945c6e8dd1369..1dfef6a9548c091cf8a1e8fd29b9d8e3d2f3f151 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 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;
@@ -57,17 +56,7 @@ public final class PutDataTransactionUtil {
      */
     public static Response putData(final YangInstanceIdentifier path, final NormalizedNode data,
             final EffectiveModelContext schemaContext, final RestconfStrategy strategy, final WriteDataParams params) {
-        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 var exists = TransactionUtil.syncAccess(strategy.exists(LogicalDatastoreType.CONFIGURATION, path), path);
         TransactionUtil.syncCommit(submitData(path, schemaContext, strategy, data, params), PUT_TX_TYPE, path);
         // TODO: Status.CREATED implies a location...
         return exists ? Response.noContent().build() : Response.status(Status.CREATED).build();
index 66aa977c2c9d95f3690c713dd92936add3359026..e7ab32d1acd746ee98156f576a783e391cfad21c 100644 (file)
@@ -13,7 +13,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Optional;
-import java.util.concurrent.ExecutionException;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
@@ -315,14 +314,7 @@ public final class ReadDataTransactionUtil {
 
     private static @Nullable NormalizedNode extractReadData(final YangInstanceIdentifier path,
             final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
-        try {
-            return dataFuture.get().orElse(null);
-        } 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);
-        }
+        return TransactionUtil.syncAccess(dataFuture, path).orElse(null);
     }
 
     /**
index 5d63bcb1630c90b3ba3bfc9a814e084b64adbd77..2d25fbaa015c835121a52d4e7250cd18e14fce56 100644 (file)
@@ -75,6 +75,26 @@ public final class TransactionUtil {
             ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.of(normalizedPathWithoutChildArgs)));
     }
 
+    /**
+     * Synchronize access to a path resource, translating any failure to a {@link RestconfDocumentedException}.
+     *
+     * @param <T> The type being accessed
+     * @param future Access future
+     * @param path Path being accessed
+     * @return The accessed value
+     * @throws RestconfDocumentedException if commit fails
+     */
+    public static <T> T syncAccess(final ListenableFuture<T> future, final YangInstanceIdentifier path) {
+        try {
+            return future.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);
+        }
+    }
+
     /**
      * Synchronize commit future, translating any failure to a {@link RestconfDocumentedException}.
      *