Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / PatchDataTransactionUtil.java
index f9d2f371b90ff60364a18c566bee833c7dc21291..b9ca9798243a16a05ab2b7d3518b1a7423966cf7 100644 (file)
@@ -25,11 +25,9 @@ import org.opendaylight.restconf.common.patch.PatchEntity;
 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.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-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;
@@ -56,15 +54,15 @@ public final class PatchDataTransactionUtil {
                                                final EffectiveModelContext schemaContext) {
         final List<PatchStatusEntity> editCollection = new ArrayList<>();
         boolean noError = true;
-        strategy.prepareReadWriteExecution();
+        final RestconfTransaction transaction = strategy.prepareWriteExecution();
 
         for (final PatchEntity patchEntity : context.getData()) {
             if (noError) {
                 switch (patchEntity.getOperation()) {
                     case CREATE:
                         try {
-                            createDataWithinTransaction(LogicalDatastoreType.CONFIGURATION,
-                                    patchEntity.getTargetNode(), patchEntity.getNode(), strategy, schemaContext);
+                            createDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(),
+                                schemaContext, transaction);
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                         } catch (final RestconfDocumentedException e) {
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(),
@@ -74,8 +72,7 @@ public final class PatchDataTransactionUtil {
                         break;
                     case DELETE:
                         try {
-                            deleteDataWithinTransaction(LogicalDatastoreType.CONFIGURATION, patchEntity.getTargetNode(),
-                                    strategy);
+                            deleteDataWithinTransaction(patchEntity.getTargetNode(), transaction);
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                         } catch (final RestconfDocumentedException e) {
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(),
@@ -85,8 +82,8 @@ public final class PatchDataTransactionUtil {
                         break;
                     case MERGE:
                         try {
-                            mergeDataWithinTransaction(LogicalDatastoreType.CONFIGURATION,
-                                    patchEntity.getTargetNode(), patchEntity.getNode(), strategy, schemaContext);
+                            mergeDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(),
+                                schemaContext, transaction);
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                         } catch (final RestconfDocumentedException e) {
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(),
@@ -96,8 +93,8 @@ public final class PatchDataTransactionUtil {
                         break;
                     case REPLACE:
                         try {
-                            replaceDataWithinTransaction(LogicalDatastoreType.CONFIGURATION,
-                                    patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext, strategy);
+                            replaceDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(),
+                                schemaContext, transaction);
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                         } catch (final RestconfDocumentedException e) {
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(),
@@ -107,8 +104,7 @@ public final class PatchDataTransactionUtil {
                         break;
                     case REMOVE:
                         try {
-                            removeDataWithinTransaction(LogicalDatastoreType.CONFIGURATION, patchEntity.getTargetNode(),
-                                    strategy);
+                            removeDataWithinTransaction(patchEntity.getTargetNode(), transaction);
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                         } catch (final RestconfDocumentedException e) {
                             editCollection.add(new PatchStatusEntity(patchEntity.getEditId(),
@@ -131,186 +127,91 @@ public final class PatchDataTransactionUtil {
         // if no errors then submit transaction, otherwise cancel
         if (noError) {
             final ResponseFactory response = new ResponseFactory(Status.OK);
-            final FluentFuture<? extends CommitInfo> future = strategy.commit();
+            final FluentFuture<? extends CommitInfo> future = transaction.commit();
 
             try {
                 //This method will close transactionChain if any
-                FutureCallbackTx.addCallback(future, PATCH_TX_TYPE, response, strategy.getTransactionChain());
+                FutureCallbackTx.addCallback(future, PATCH_TX_TYPE, response, strategy, null);
             } catch (final RestconfDocumentedException e) {
                 // if errors occurred during transaction commit then patch failed and global errors are reported
                 return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false,
                         Lists.newArrayList(e.getErrors()));
             }
 
-            return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection),
-                    true, null);
+            return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true, null);
         } else {
-            strategy.cancel();
-            return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection),
-                    false, null);
+            transaction.cancel();
+            return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false, null);
         }
     }
 
     /**
      * Create data within one transaction, return error if already exists.
      *
-     * @param dataStore     Datastore to write data to
      * @param path          Path for data to be created
      * @param payload       Data to be created
-     * @param strategy      Object that perform the actual DS operations
-     * @param schemaContext Global schema context
+     * @param transaction   A handle to a set of DS operations
      */
-    private static void createDataWithinTransaction(final LogicalDatastoreType dataStore,
-                                                    final YangInstanceIdentifier path,
-                                                    final NormalizedNode<?, ?> payload,
-                                                    final RestconfStrategy strategy,
-                                                    final EffectiveModelContext schemaContext) {
-        LOG.trace("POST {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
-        createData(payload, schemaContext, path, strategy, dataStore, true);
+    private static void createDataWithinTransaction(final YangInstanceIdentifier path, final NormalizedNode payload,
+                                                    final EffectiveModelContext schemaContext,
+                                                    final RestconfTransaction transaction) {
+        LOG.trace("POST {} within Restconf Patch: {} with payload {}", LogicalDatastoreType.CONFIGURATION.name(),
+            path, payload);
+        transaction.create(LogicalDatastoreType.CONFIGURATION, path, payload, schemaContext);
     }
 
     /**
-     * Check if data exists and remove it within one transaction.
+     * Remove data within one transaction.
      *
-     * @param dataStore            Datastore to delete data from
-     * @param path                 Path for data to be deleted
-     * @param strategy             Object that perform the actual DS operations
+     * @param path     Path for data to be deleted
+     * @param transaction   A handle to a set of DS operations
      */
-    private static void deleteDataWithinTransaction(final LogicalDatastoreType dataStore,
-                                                    final YangInstanceIdentifier path,
-                                                    final RestconfStrategy strategy) {
-        LOG.trace("Delete {} within Restconf Patch: {}", dataStore.name(), path);
-        final FluentFuture<Boolean> future = strategy.exists(dataStore, path);
-        final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
-
-        FutureCallbackTx.addCallback(future, PATCH_TX_TYPE, 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);
-        }
-
-        strategy.delete(dataStore, path);
+    private static void deleteDataWithinTransaction(final YangInstanceIdentifier path,
+                                                    final RestconfTransaction transaction) {
+        LOG.trace("Delete {} within Restconf Patch: {}", LogicalDatastoreType.CONFIGURATION.name(), path);
+        transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
     }
 
     /**
      * Merge data within one transaction.
      *
-     * @param dataStore     Datastore to merge data to
-     * @param path          Path for data to be merged
-     * @param payload       Data to be merged
-     * @param strategy      Object that perform the actual DS operations
-     * @param schemaContext Global schema context
+     * @param path     Path for data to be merged
+     * @param payload  Data to be merged
+     * @param transaction   A handle to a set of DS operations
      */
-    private static void mergeDataWithinTransaction(final LogicalDatastoreType dataStore,
-                                                   final YangInstanceIdentifier path,
-                                                   final NormalizedNode<?, ?> payload,
-                                                   final RestconfStrategy strategy,
-                                                   final EffectiveModelContext schemaContext) {
-        LOG.trace("Merge {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
-        TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
-        strategy.merge(dataStore, path, payload);
+    private static void mergeDataWithinTransaction(final YangInstanceIdentifier path, final NormalizedNode payload,
+                                                   final EffectiveModelContext schemaContext,
+                                                   final RestconfTransaction transaction) {
+        LOG.trace("Merge {} within Restconf Patch: {} with payload {}", LogicalDatastoreType.CONFIGURATION.name(),
+            path, payload);
+        TransactionUtil.ensureParentsByMerge(path, schemaContext, transaction);
+        transaction.merge(LogicalDatastoreType.CONFIGURATION, path, payload);
     }
 
     /**
      * Do NOT check if data exists and remove it within one transaction.
      *
-     * @param dataStore        Datastore to delete data from
-     * @param path             Path for data to be deleted
-     * @param strategy         Object that perform the actual DS operations
+     * @param path     Path for data to be deleted
+     * @param transaction   A handle to a set of DS operations
      */
-    private static void removeDataWithinTransaction(final LogicalDatastoreType dataStore,
-                                                    final YangInstanceIdentifier path,
-                                                    final RestconfStrategy strategy) {
-        LOG.trace("Remove {} within Restconf Patch: {}", dataStore.name(), path);
-        strategy.delete(dataStore, path);
+    private static void removeDataWithinTransaction(final YangInstanceIdentifier path,
+                                                    final RestconfTransaction transaction) {
+        LOG.trace("Remove {} within Restconf Patch: {}", LogicalDatastoreType.CONFIGURATION.name(), path);
+        transaction.remove(LogicalDatastoreType.CONFIGURATION, path);
     }
 
     /**
      * Create data within one transaction, replace if already exists.
      *
-     * @param dataStore     Datastore to write data to
      * @param path          Path for data to be created
      * @param payload       Data to be created
-     * @param schemaContext Global schema context
-     * @param strategy      Object that perform the actual DS operations
+     * @param transaction   A handle to a set of DS operations
      */
-    private static void replaceDataWithinTransaction(final LogicalDatastoreType dataStore,
-                                                     final YangInstanceIdentifier path,
-                                                     final NormalizedNode<?, ?> payload,
+    private static void replaceDataWithinTransaction(final YangInstanceIdentifier path, final NormalizedNode payload,
                                                      final EffectiveModelContext schemaContext,
-                                                     final RestconfStrategy strategy) {
-        LOG.trace("PUT {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
-        createData(payload, schemaContext, path, strategy, dataStore, false);
-    }
-
-    /**
-     * Create data within one transaction. If {@code errorIfExists} is set to {@code true} then data will be checked
-     * for existence before created, otherwise they will be overwritten.
-     *
-     * @param payload       Data to be created
-     * @param schemaContext Global schema context
-     * @param path          Path for data to be created
-     * @param strategy      Object that perform the actual DS operations
-     * @param dataStore     Datastore to write data to
-     * @param errorIfExists Enable checking for existence of data (throws error if already exists)
-     */
-    private static void createData(final NormalizedNode<?, ?> payload, final EffectiveModelContext schemaContext,
-                                   final YangInstanceIdentifier path,
-                                   final RestconfStrategy strategy,
-                                   final LogicalDatastoreType dataStore, final boolean errorIfExists) {
-        if (payload instanceof MapNode) {
-            final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
-            strategy.merge(dataStore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
-            TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
-            for (final MapEntryNode child : ((MapNode) payload).getValue()) {
-                final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
-
-                if (errorIfExists) {
-                    checkItemDoesNotExistsWithinTransaction(strategy, dataStore, childPath);
-                }
-
-                if (errorIfExists) {
-                    strategy.create(dataStore, childPath, child);
-                } else {
-                    strategy.replace(dataStore, childPath, child);
-                }
-            }
-        } else {
-            if (errorIfExists) {
-                checkItemDoesNotExistsWithinTransaction(strategy, dataStore, path);
-            }
-
-            TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
-            if (errorIfExists) {
-                strategy.create(dataStore, path, payload);
-            } else {
-                strategy.replace(dataStore, path, payload);
-            }
-        }
-    }
-
-    /**
-     * Check if items do NOT already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
-     * data already exists.
-     *
-     * @param strategy      Object that perform the actual DS operations
-     * @param store         Datastore
-     * @param path          Path to be checked
-     */
-    public static void checkItemDoesNotExistsWithinTransaction(final RestconfStrategy strategy,
-                                                               final LogicalDatastoreType store,
-                                                               final YangInstanceIdentifier path) {
-        final FluentFuture<Boolean> future = strategy.exists(store, path);
-        final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
-
-        FutureCallbackTx.addCallback(future, PATCH_TX_TYPE, response);
-
-        if (response.result) {
-            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);
-        }
+                                                     final RestconfTransaction transaction) {
+        LOG.trace("PUT {} within Restconf Patch: {} with payload {}",
+            LogicalDatastoreType.CONFIGURATION.name(), path, payload);
+        transaction.replace(LogicalDatastoreType.CONFIGURATION, path, payload, schemaContext);
     }
 }