Migrate restconf to MD-SAL APIs
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / PatchDataTransactionUtil.java
index bee24e9bd8eae3460931bdbf0ce9154832bca8da..0444d0ad5cab2aedd28a8ccdbd2589af1adff7d3 100644 (file)
@@ -5,19 +5,19 @@
  * 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 com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FluentFuture;
 import java.util.ArrayList;
 import java.util.List;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import javax.ws.rs.core.Response.Status;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
@@ -26,7 +26,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.common.patch.PatchStatusEntity;
-import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.PatchData;
@@ -57,7 +56,7 @@ public final class PatchDataTransactionUtil {
                                                final SchemaContextRef schemaContextRef) {
         final List<PatchStatusEntity> editCollection = new ArrayList<>();
         boolean noError = true;
-        final DOMDataReadWriteTransaction tx = transactionNode.getTransactionChain().newReadWriteTransaction();
+        final DOMDataTreeReadWriteTransaction tx = transactionNode.getTransactionChain().newReadWriteTransaction();
 
         for (final PatchEntity patchEntity : context.getData()) {
             if (noError) {
@@ -131,8 +130,8 @@ public final class PatchDataTransactionUtil {
 
         // if no errors then submit transaction, otherwise cancel
         if (noError) {
-            final ResponseFactory response = new ResponseFactory();
-            final CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
+            final ResponseFactory response = new ResponseFactory(Status.OK);
+            final FluentFuture<? extends CommitInfo> future = tx.commit();
 
             try {
                 FutureCallbackTx.addCallback(future, PatchData.PATCH_TX_TYPE, response);
@@ -145,7 +144,7 @@ public final class PatchDataTransactionUtil {
             return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true, null);
         } else {
             tx.cancel();
-            RestConnectorProvider.resetTransactionChainForAdapaters(transactionNode.getTransactionChain());
+            transactionNode.getTransactionChainHandler().reset();
             return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection),
                     false, null);
         }
@@ -162,7 +161,7 @@ public final class PatchDataTransactionUtil {
     private static void createDataWithinTransaction(final LogicalDatastoreType dataStore,
                                                     final YangInstanceIdentifier path,
                                                     final NormalizedNode<?, ?> payload,
-                                                    final DOMDataReadWriteTransaction rwTransaction,
+                                                    final DOMDataTreeReadWriteTransaction rwTransaction,
                                                     final SchemaContextRef schemaContextRef) {
         LOG.trace("POST {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
         createData(payload, schemaContextRef.get(), path, rwTransaction, dataStore, true);
@@ -176,7 +175,7 @@ public final class PatchDataTransactionUtil {
      */
     private static void deleteDataWithinTransaction(final LogicalDatastoreType dataStore,
                                                     final YangInstanceIdentifier path,
-                                                    final DOMDataReadWriteTransaction readWriteTransaction) {
+                                                    final DOMDataTreeReadWriteTransaction readWriteTransaction) {
         LOG.trace("Delete {} within Restconf Patch: {}", dataStore.name(), path);
         checkItemExistsWithinTransaction(readWriteTransaction, dataStore, path);
         readWriteTransaction.delete(dataStore, path);
@@ -193,7 +192,7 @@ public final class PatchDataTransactionUtil {
     private static void mergeDataWithinTransaction(final LogicalDatastoreType dataStore,
                                                    final YangInstanceIdentifier path,
                                                    final NormalizedNode<?, ?> payload,
-                                                   final DOMDataReadWriteTransaction writeTransaction,
+                                                   final DOMDataTreeWriteTransaction writeTransaction,
                                                    final SchemaContextRef schemaContextRef) {
         LOG.trace("Merge {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
         TransactionUtil.ensureParentsByMerge(path, schemaContextRef.get(), writeTransaction);
@@ -208,7 +207,7 @@ public final class PatchDataTransactionUtil {
      */
     private static void removeDataWithinTransaction(final LogicalDatastoreType dataStore,
                                                     final YangInstanceIdentifier path,
-                                                    final DOMDataWriteTransaction writeTransaction) {
+                                                    final DOMDataTreeWriteTransaction writeTransaction) {
         LOG.trace("Remove {} within Restconf Patch: {}", dataStore.name(), path);
         writeTransaction.delete(dataStore, path);
     }
@@ -225,7 +224,7 @@ public final class PatchDataTransactionUtil {
                                                      final YangInstanceIdentifier path,
                                                      final NormalizedNode<?, ?> payload,
                                                      final SchemaContextRef schemaContextRef,
-                                                     final DOMDataReadWriteTransaction rwTransaction) {
+                                                     final DOMDataTreeReadWriteTransaction rwTransaction) {
         LOG.trace("PUT {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
         createData(payload, schemaContextRef.get(), path, rwTransaction, dataStore, false);
     }
@@ -241,7 +240,8 @@ public final class PatchDataTransactionUtil {
      * @param errorIfExists Enable checking for existence of data (throws error if already exists)
      */
     private static void createData(final NormalizedNode<?, ?> payload, final SchemaContext schemaContext,
-                                   final YangInstanceIdentifier path, final DOMDataReadWriteTransaction rwTransaction,
+                                   final YangInstanceIdentifier path,
+                                   final DOMDataTreeReadWriteTransaction rwTransaction,
                                    final LogicalDatastoreType dataStore, final boolean errorIfExists) {
         if (payload instanceof MapNode) {
             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
@@ -273,18 +273,17 @@ public final class PatchDataTransactionUtil {
      * @param store Datastore
      * @param path Path to be checked
      */
-    public static void checkItemExistsWithinTransaction(final DOMDataReadWriteTransaction rwTransaction,
+    public static void checkItemExistsWithinTransaction(final DOMDataTreeReadTransaction rwTransaction,
                                                 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
-        final CheckedFuture<Boolean, ReadFailedException> future = rwTransaction.exists(store, path);
+        final FluentFuture<Boolean> future = rwTransaction.exists(store, path);
         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
 
         FutureCallbackTx.addCallback(future, PatchData.PATCH_TX_TYPE, response);
 
         if (!response.result) {
-            final String errMsg = "Operation via Restconf was not executed because data does not exist";
-            LOG.trace("{}:{}", errMsg, path);
-            throw new RestconfDocumentedException(
-                    "Data does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, 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);
         }
     }
 
@@ -295,18 +294,17 @@ public final class PatchDataTransactionUtil {
      * @param store Datastore
      * @param path Path to be checked
      */
-    public static void checkItemDoesNotExistsWithinTransaction(final DOMDataReadWriteTransaction rwTransaction,
+    public static void checkItemDoesNotExistsWithinTransaction(final DOMDataTreeReadTransaction rwTransaction,
                                                final LogicalDatastoreType store, final YangInstanceIdentifier path) {
-        final CheckedFuture<Boolean, ReadFailedException> future = rwTransaction.exists(store, path);
+        final FluentFuture<Boolean> future = rwTransaction.exists(store, path);
         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
 
         FutureCallbackTx.addCallback(future, PatchData.PATCH_TX_TYPE, response);
 
         if (response.result) {
-            final String errMsg = "Operation via Restconf was not executed because data already exists";
-            LOG.trace("{}:{}", errMsg, path);
-            throw new RestconfDocumentedException(
-                    "Data already exists", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, 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);
         }
     }
-}
\ No newline at end of file
+}