Eliminate PatchDataTransactionUtil 08/107808/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Sep 2023 01:27:34 +0000 (03:27 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Sep 2023 10:00:19 +0000 (12:00 +0200)
This class is hosting a single method, which is strongly related to
RestconfStrategy.

JIRA: NETCONF-1107
Change-Id: I15b32b055a9be8787d32be5ede12e1d40a539a94
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PatchDataTransactionUtil.java [deleted file]
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/AbstractRestconfStrategyTest.java

index fcc0014cfe0ac64e17078aa01a7f717de2520363..382b2b61541ef5f1ce920d0669bb1685bc60f8e5 100644 (file)
@@ -83,7 +83,6 @@ import org.opendaylight.restconf.nb.rfc8040.monitoring.RestconfStateStreams;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
@@ -775,7 +774,7 @@ public final class RestconfDataServiceImpl {
 
     @VisibleForTesting
     PatchStatusContext yangPatchData(final InstanceIdentifierContext targetResource, final PatchContext context) {
-        return PatchDataTransactionUtil.patchData(context, getRestconfStrategy(targetResource.getMountPoint()),
+        return getRestconfStrategy(targetResource.getMountPoint()).patchData(context,
             targetResource.getSchemaContext());
     }
 
index f90ec808e61d2a8066896e2ca83f70c069d87203..7a29ecd8067e2fb2b1bda0b5b86a94c842480ae2 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
@@ -25,8 +26,12 @@ import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.api.query.PointParam;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.common.errors.RestconfFuture;
 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
+import org.opendaylight.restconf.common.patch.PatchContext;
+import org.opendaylight.restconf.common.patch.PatchStatusContext;
+import org.opendaylight.restconf.common.patch.PatchStatusEntity;
 import org.opendaylight.restconf.nb.rfc8040.Insert;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierDeserializer;
@@ -369,6 +374,98 @@ public abstract class RestconfStrategy {
         };
     }
 
+    /**
+     * Process edit operations of one {@link PatchContext}.
+     *
+     * @param patch    Patch context to be processed
+     * @param context  Global schema context
+     * @return {@link PatchStatusContext}
+     */
+    public final @NonNull PatchStatusContext patchData(final PatchContext patch, final EffectiveModelContext context) {
+        final var editCollection = new ArrayList<PatchStatusEntity>();
+        final var tx = prepareWriteExecution();
+
+        boolean noError = true;
+        for (var patchEntity : patch.getData()) {
+            if (noError) {
+                final var targetNode = patchEntity.getTargetNode();
+                final var editId = patchEntity.getEditId();
+
+                switch (patchEntity.getOperation()) {
+                    case Create:
+                        try {
+                            tx.create(targetNode, patchEntity.getNode(), context);
+                            editCollection.add(new PatchStatusEntity(editId, true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
+                            noError = false;
+                        }
+                        break;
+                    case Delete:
+                        try {
+                            tx.delete(targetNode);
+                            editCollection.add(new PatchStatusEntity(editId, true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
+                            noError = false;
+                        }
+                        break;
+                    case Merge:
+                        try {
+                            TransactionUtil.ensureParentsByMerge(targetNode, context, tx);
+                            tx.merge(targetNode, patchEntity.getNode());
+                            editCollection.add(new PatchStatusEntity(editId, true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
+                            noError = false;
+                        }
+                        break;
+                    case Replace:
+                        try {
+                            tx.replace(targetNode, patchEntity.getNode(), context);
+                            editCollection.add(new PatchStatusEntity(editId, true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
+                            noError = false;
+                        }
+                        break;
+                    case Remove:
+                        try {
+                            tx.remove(targetNode);
+                            editCollection.add(new PatchStatusEntity(editId, true, null));
+                        } catch (RestconfDocumentedException e) {
+                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
+                            noError = false;
+                        }
+                        break;
+                    default:
+                        editCollection.add(new PatchStatusEntity(editId, false, List.of(
+                            new RestconfError(ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED,
+                                "Not supported Yang Patch operation"))));
+                        noError = false;
+                        break;
+                }
+            } else {
+                break;
+            }
+        }
+
+        // if no errors then submit transaction, otherwise cancel
+        if (noError) {
+            try {
+                TransactionUtil.syncCommit(tx.commit(), "PATCH", null);
+            } catch (RestconfDocumentedException e) {
+                // if errors occurred during transaction commit then patch failed and global errors are reported
+                return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), false, e.getErrors());
+            }
+
+            return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), true, null);
+        } else {
+            tx.cancel();
+            return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), false, null);
+        }
+    }
+
     private static void insertWithPointPost(final RestconfTransaction tx, final YangInstanceIdentifier path,
             final NormalizedNode data, final PointParam point, final NormalizedNodeContainer<?> readList,
             final YangInstanceIdentifier grandParentPath, final boolean before, final EffectiveModelContext context) {
diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PatchDataTransactionUtil.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PatchDataTransactionUtil.java
deleted file mode 100644 (file)
index 6646308..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.nb.rfc8040.rests.utils;
-
-import java.util.ArrayList;
-import java.util.List;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError;
-import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.common.patch.PatchStatusContext;
-import org.opendaylight.restconf.common.patch.PatchStatusEntity;
-import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
-import org.opendaylight.yangtools.yang.common.ErrorTag;
-import org.opendaylight.yangtools.yang.common.ErrorType;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-
-public final class PatchDataTransactionUtil {
-    private PatchDataTransactionUtil() {
-        // Hidden on purpose
-    }
-
-    /**
-     * Process edit operations of one {@link PatchContext}.
-     *
-     * @param patch    Patch context to be processed
-     * @param strategy object that perform the actual DS operations
-     * @param context  Global schema context
-     * @return {@link PatchStatusContext}
-     */
-    public static PatchStatusContext patchData(final PatchContext patch, final RestconfStrategy strategy,
-            final EffectiveModelContext context) {
-        final var editCollection = new ArrayList<PatchStatusEntity>();
-        final var tx = strategy.prepareWriteExecution();
-
-        boolean noError = true;
-        for (var patchEntity : patch.getData()) {
-            if (noError) {
-                final var targetNode = patchEntity.getTargetNode();
-                final var editId = patchEntity.getEditId();
-
-                switch (patchEntity.getOperation()) {
-                    case Create:
-                        try {
-                            tx.create(targetNode, patchEntity.getNode(), context);
-                            editCollection.add(new PatchStatusEntity(editId, true, null));
-                        } catch (RestconfDocumentedException e) {
-                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
-                            noError = false;
-                        }
-                        break;
-                    case Delete:
-                        try {
-                            tx.delete(targetNode);
-                            editCollection.add(new PatchStatusEntity(editId, true, null));
-                        } catch (RestconfDocumentedException e) {
-                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
-                            noError = false;
-                        }
-                        break;
-                    case Merge:
-                        try {
-                            TransactionUtil.ensureParentsByMerge(targetNode, context, tx);
-                            tx.merge(targetNode, patchEntity.getNode());
-                            editCollection.add(new PatchStatusEntity(editId, true, null));
-                        } catch (RestconfDocumentedException e) {
-                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
-                            noError = false;
-                        }
-                        break;
-                    case Replace:
-                        try {
-                            tx.replace(targetNode, patchEntity.getNode(), context);
-                            editCollection.add(new PatchStatusEntity(editId, true, null));
-                        } catch (RestconfDocumentedException e) {
-                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
-                            noError = false;
-                        }
-                        break;
-                    case Remove:
-                        try {
-                            tx.remove(targetNode);
-                            editCollection.add(new PatchStatusEntity(editId, true, null));
-                        } catch (RestconfDocumentedException e) {
-                            editCollection.add(new PatchStatusEntity(editId, false, e.getErrors()));
-                            noError = false;
-                        }
-                        break;
-                    default:
-                        editCollection.add(new PatchStatusEntity(editId, false, List.of(
-                            new RestconfError(ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED,
-                                "Not supported Yang Patch operation"))));
-                        noError = false;
-                        break;
-                }
-            } else {
-                break;
-            }
-        }
-
-        // if no errors then submit transaction, otherwise cancel
-        if (noError) {
-            try {
-                TransactionUtil.syncCommit(tx.commit(), "PATCH", null);
-            } catch (RestconfDocumentedException e) {
-                // if errors occurred during transaction commit then patch failed and global errors are reported
-                return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), false, e.getErrors());
-            }
-
-            return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), true, null);
-        } else {
-            tx.cancel();
-            return new PatchStatusContext(patch.getPatchId(), List.copyOf(editCollection), false, null);
-        }
-    }
-}
index 1530a461535a23e30299839dc5fb3fa147403c01..981519b3f43fe15eb8a533e71a3cef01b44c2cd0 100644 (file)
@@ -33,7 +33,6 @@ import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchEntity;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
@@ -334,10 +333,9 @@ abstract class AbstractRestconfStrategyTest extends AbstractJukeboxTest {
 
     @Test
     public final void testDeleteNonexistentData() {
-        final var patchStatusContext = PatchDataTransactionUtil.patchData(new PatchContext(
+        final var patchStatusContext = deleteNonexistentDataTestStrategy().patchData(new PatchContext(
             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, GAP_IID),
-            List.of(new PatchEntity("edit", Operation.Delete, CREATE_AND_DELETE_TARGET)), "patchD"),
-            deleteNonexistentDataTestStrategy(), JUKEBOX_SCHEMA);
+            List.of(new PatchEntity("edit", Operation.Delete, CREATE_AND_DELETE_TARGET)), "patchD"), JUKEBOX_SCHEMA);
         assertFalse(patchStatusContext.ok());
     }
 
@@ -475,7 +473,7 @@ abstract class AbstractRestconfStrategyTest extends AbstractJukeboxTest {
     }
 
     private static void patch(final PatchContext patchContext, final RestconfStrategy strategy, final boolean failed) {
-        final var patchStatusContext = PatchDataTransactionUtil.patchData(patchContext, strategy, JUKEBOX_SCHEMA);
+        final var patchStatusContext = strategy.patchData(patchContext, JUKEBOX_SCHEMA);
         for (var entity : patchStatusContext.editCollection()) {
             if (failed) {
                 assertTrue("Edit " + entity.getEditId() + " failed", entity.isOk());