Inline checkItemExists() 64/107064/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 25 Jul 2023 16:52:43 +0000 (18:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 25 Jul 2023 20:52:29 +0000 (22:52 +0200)
This method is used only from MdsalRestconfTransaction, inline it there
in a much simpler form. This makes the flow clearer and eliminates a
user of FutureCallbackTx.

Change-Id: I2f00d0266991d5a7dc0ff8cc9b58b3b26a268d2d
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/DeleteDataTransactionUtil.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/FutureDataFactory.java

index 9ccc63d74967ae83c86879a7418f7c2f0338f9a5..35dbeb20d278733bc330890e4ac292974a6666d2 100644 (file)
@@ -9,19 +9,18 @@ package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
 
 import static com.google.common.base.Verify.verifyNotNull;
 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
-import static org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil.DELETE_TX_TYPE;
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil.checkItemDoesNotExists;
 
 import com.google.common.util.concurrent.FluentFuture;
 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;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -33,8 +32,12 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 final class MdsalRestconfTransaction extends RestconfTransaction {
+    private static final Logger LOG = LoggerFactory.getLogger(MdsalRestconfTransaction.class);
+
     private DOMDataTreeReadWriteTransaction rwTx;
 
     MdsalRestconfTransaction(final DOMDataBroker dataBroker) {
@@ -51,8 +54,23 @@ final class MdsalRestconfTransaction extends RestconfTransaction {
 
     @Override
     public void delete(final YangInstanceIdentifier path) {
-        final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(CONFIGURATION, path);
-        DeleteDataTransactionUtil.checkItemExists(isExists, path, DELETE_TX_TYPE);
+        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) {
+            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);
+        }
+
         rwTx.delete(CONFIGURATION, path);
     }
 
index 29b2b3cc4b8b32bb14678c9c463178a88d870edb..89dfb3acb81f5c53e401edcbb8a0ad8c253f318e 100644 (file)
@@ -14,19 +14,12 @@ import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
-import org.opendaylight.yangtools.yang.common.ErrorTag;
-import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Util class for delete specific data in config DS.
  */
 public final class DeleteDataTransactionUtil {
-    private static final Logger LOG = LoggerFactory.getLogger(DeleteDataTransactionUtil.class);
-    public static final String DELETE_TX_TYPE = "DELETE";
-
     private DeleteDataTransactionUtil() {
         // Hidden on purpose
     }
@@ -49,28 +42,7 @@ public final class DeleteDataTransactionUtil {
         final FluentFuture<? extends CommitInfo> future = transaction.commit();
         final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
         //This method will close transactionChain if any
-        FutureCallbackTx.addCallback(future, DELETE_TX_TYPE, response, path);
+        FutureCallbackTx.addCallback(future, "DELETE", response, path);
         return response.build();
     }
-
-    /**
-     * Check if items already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
-     * data does NOT already exists.
-     *
-     * @param isExistsFuture if checked data exists
-     * @param path           Path to be checked
-     * @param operationType  Type of operation (READ, POST, PUT, DELETE...)
-     */
-    public static void checkItemExists(final FluentFuture<Boolean> isExistsFuture,
-                                       final YangInstanceIdentifier path,
-                                       final String operationType) {
-        final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
-        FutureCallbackTx.addCallback(isExistsFuture, operationType, response);
-
-        if (!response.result) {
-            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 70ec0a6799b1e445a2c2c04a6b9d07177ae1b424..a64b7cce3450edb96ccb0abc1da93780fa09b9f5 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 class FutureDataFactory<T> {
 
-    protected T result;
+    protected T result = null;
     private boolean statusFail = false;
 
     void setResult(final T result) {
@@ -17,7 +17,7 @@ class FutureDataFactory<T> {
     }
 
     void setFailureStatus() {
-        this.statusFail = true;
+        statusFail = true;
     }
 
     boolean getFailureStatus() {