Create NetconfDataTreeService with base and additional operations for netconf
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / TransactionUtil.java
index 0e4261c46a3af32962ac4319ad051c8981ae5ea8..ffe95f53663af8e0c6fdaed28b2b2201e51a0a8c 100644 (file)
@@ -7,18 +7,15 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
-import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FluentFuture;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
-import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -42,15 +39,12 @@ public final class TransactionUtil {
     /**
      * Merged parents of data.
      *
-     * @param path
-     *             path of data
-     * @param schemaContext
-     *             {@link SchemaContext}
-     * @param writeTx
-     *             write transaction
+     * @param path          path of data
+     * @param schemaContext {@link SchemaContext}
+     * @param strategy      object that perform the actual DS operations
      */
     public static void ensureParentsByMerge(final YangInstanceIdentifier path, final SchemaContext schemaContext,
-            final DOMDataTreeWriteTransaction writeTx) {
+                                            final RestconfStrategy strategy) {
         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
         YangInstanceIdentifier rootNormalizedPath = null;
 
@@ -71,35 +65,31 @@ public final class TransactionUtil {
             return;
         }
 
-        Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
-
         final NormalizedNode<?, ?> parentStructure = ImmutableNodes.fromInstanceId(schemaContext,
                 YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
-        writeTx.merge(LogicalDatastoreType.CONFIGURATION, rootNormalizedPath, parentStructure);
+        strategy.merge(LogicalDatastoreType.CONFIGURATION, rootNormalizedPath, parentStructure);
     }
 
     /**
      * Check if items already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
      * data does NOT already exists.
-     * @param transactionChain Transaction chain
-     * @param rwTransaction Transaction
-     * @param store Datastore
-     * @param path Path to be checked
+     *
+     * @param strategy      Object that perform the actual DS operations
+     * @param store         Datastore
+     * @param path          Path to be checked
      * @param operationType Type of operation (READ, POST, PUT, DELETE...)
      */
-    public static void checkItemExists(final DOMTransactionChain transactionChain,
-                                       final DOMDataTreeReadWriteTransaction rwTransaction,
+    public static void checkItemExists(final RestconfStrategy strategy,
                                        final LogicalDatastoreType store, final YangInstanceIdentifier path,
                                        final String operationType) {
-        final FluentFuture<Boolean> future = rwTransaction.exists(store, path);
+        final FluentFuture<Boolean> future = strategy.exists(store, path);
         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
 
         FutureCallbackTx.addCallback(future, operationType, response);
 
         if (!response.result) {
             // close transaction
-            rwTransaction.cancel();
-            transactionChain.close();
+            strategy.cancel();
             // throw error
             LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
             throw new RestconfDocumentedException(
@@ -110,25 +100,23 @@ public final class TransactionUtil {
     /**
      * Check if items do NOT already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
      * data already exists.
-     * @param transactionChain Transaction chain
-     * @param rwTransaction Transaction
-     * @param store Datastore
-     * @param path Path to be checked
+     *
+     * @param strategy      Object that perform the actual DS operations
+     * @param store         Datastore
+     * @param path          Path to be checked
      * @param operationType Type of operation (READ, POST, PUT, DELETE...)
      */
-    public static void checkItemDoesNotExists(final DOMTransactionChain transactionChain,
-                                              final DOMDataTreeReadWriteTransaction rwTransaction,
+    public static void checkItemDoesNotExists(final RestconfStrategy strategy,
                                               final LogicalDatastoreType store, final YangInstanceIdentifier path,
                                               final String operationType) {
-        final FluentFuture<Boolean> future = rwTransaction.exists(store, path);
+        final FluentFuture<Boolean> future = strategy.exists(store, path);
         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
 
         FutureCallbackTx.addCallback(future, operationType, response);
 
         if (response.result) {
             // close transaction
-            rwTransaction.cancel();
-            transactionChain.close();
+            strategy.cancel();
             // throw error
             LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
             throw new RestconfDocumentedException(