Do not pass RestconfStrategy to FutureCallbackTx
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtil.java
index 875438290a519224d0707312f1e6a8cd5fddba5d..30012ecd48de7d2b4c744cb18d44992474aacc82 100644 (file)
@@ -31,9 +31,9 @@ import org.opendaylight.restconf.common.context.WriterParameters;
 import org.opendaylight.restconf.common.context.WriterParameters.WriterParametersBuilder;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData.WithDefaults;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -141,7 +141,7 @@ public final class ReadDataTransactionUtil {
                 break;
             default:
                 throw new RestconfDocumentedException(
-                    new RestconfError(ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
+                    new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
                         "Invalid content parameter: " + contentValue, null,
                         "The content parameter value must be either config, nonconfig or all (default)"));
         }
@@ -153,7 +153,7 @@ public final class ReadDataTransactionUtil {
             if (value == null || value < RestconfDataServiceConstant.ReadData.MIN_DEPTH
                     || value > RestconfDataServiceConstant.ReadData.MAX_DEPTH) {
                 throw new RestconfDocumentedException(
-                        new RestconfError(ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
+                        new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
                                 "Invalid depth parameter: " + depth, null,
                                 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
             } else {
@@ -175,8 +175,8 @@ public final class ReadDataTransactionUtil {
             final String str = withDefaults.get(0);
             final WithDefaults val = WithDefaults.forValue(str);
             if (val == null) {
-                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL,
-                    RestconfError.ErrorTag.INVALID_VALUE, "Invalid with-defaults parameter: " + str, null,
+                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                    "Invalid with-defaults parameter: " + str, null,
                     "The with-defaults parameter must be a string in " + WithDefaults.possibleValues()));
             }
 
@@ -210,20 +210,15 @@ public final class ReadDataTransactionUtil {
                                                     final String withDefa, final EffectiveModelContext ctx) {
         switch (valueOfContent) {
             case RestconfDataServiceConstant.ReadData.CONFIG:
-                if (withDefa == null) {
-                    return readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true);
-                } else {
-                    return prepareDataByParamWithDef(
-                            readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true),
-                            path, withDefa, ctx);
-                }
+                final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
+                return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
             case RestconfDataServiceConstant.ReadData.NONCONFIG:
-                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, true);
+                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
             case RestconfDataServiceConstant.ReadData.ALL:
                 return readAllData(strategy, path, withDefa, ctx);
             default:
                 throw new RestconfDocumentedException(
-                        new RestconfError(ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
+                        new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
                                 "Invalid content parameter: " + valueOfContent, null,
                                 "The content parameter value must be either config, nonconfig or all (default)"));
         }
@@ -247,20 +242,16 @@ public final class ReadDataTransactionUtil {
             final @NonNull List<YangInstanceIdentifier> fields) {
         switch (valueOfContent) {
             case RestconfDataServiceConstant.ReadData.CONFIG:
-                if (withDefa == null) {
-                    return readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true, fields);
-                } else {
-                    return prepareDataByParamWithDef(
-                            readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true, fields),
-                            path, withDefa, ctx);
-                }
+                final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
+                    fields);
+                return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
             case RestconfDataServiceConstant.ReadData.NONCONFIG:
-                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, true, fields);
+                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
             case RestconfDataServiceConstant.ReadData.ALL:
                 return readAllData(strategy, path, withDefa, ctx, fields);
             default:
-                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL,
-                        RestconfError.ErrorTag.INVALID_VALUE, "Invalid content parameter: " + valueOfContent, null,
+                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                        "Invalid content parameter: " + valueOfContent, null,
                         "The content parameter value must be either config, nonconfig or all (default)"));
         }
     }
@@ -295,7 +286,7 @@ public final class ReadDataTransactionUtil {
                 .collect(Collectors.toSet());
             throw new RestconfDocumentedException(
                 "Not allowed parameters for " + READ_TYPE_TX + " operation: " + notAllowedParameters,
-                ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE);
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
     }
 
@@ -433,10 +424,8 @@ public final class ReadDataTransactionUtil {
      * @return {@link NormalizedNode}
      */
     static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
-            final LogicalDatastoreType store, final YangInstanceIdentifier path,
-            final boolean closeTransactionChain) {
-        final ListenableFuture<Optional<NormalizedNode>> listenableFuture = strategy.read(store, path);
-        return extractReadData(strategy, path, closeTransactionChain, listenableFuture);
+            final LogicalDatastoreType store, final YangInstanceIdentifier path) {
+        return extractReadData(strategy, path, strategy.read(store, path));
     }
 
     /**
@@ -453,21 +442,14 @@ public final class ReadDataTransactionUtil {
      */
     private static @Nullable NormalizedNode readDataViaTransaction(final @NonNull RestconfStrategy strategy,
             final @NonNull LogicalDatastoreType store, final @NonNull YangInstanceIdentifier path,
-            final boolean closeTransactionChain, final @NonNull List<YangInstanceIdentifier> fields) {
-        final ListenableFuture<Optional<NormalizedNode>> listenableFuture = strategy.read(store, path, fields);
-        return extractReadData(strategy, path, closeTransactionChain, listenableFuture);
+            final @NonNull List<YangInstanceIdentifier> fields) {
+        return extractReadData(strategy, path, strategy.read(store, path, fields));
     }
 
     private static NormalizedNode extractReadData(final RestconfStrategy strategy,
-            final YangInstanceIdentifier path, final boolean closeTransactionChain,
-            final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
+            final YangInstanceIdentifier path, final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
         final NormalizedNodeFactory dataFactory = new NormalizedNodeFactory();
-        if (closeTransactionChain) {
-            //Method close transactionChain if any
-            FutureCallbackTx.addCallback(dataFuture, READ_TYPE_TX, dataFactory, strategy, path);
-        } else {
-            FutureCallbackTx.addCallback(dataFuture, READ_TYPE_TX, dataFactory);
-        }
+        FutureCallbackTx.addCallback(dataFuture, READ_TYPE_TX, dataFactory, path);
         return dataFactory.build();
     }
 
@@ -483,22 +465,13 @@ public final class ReadDataTransactionUtil {
     private static @Nullable NormalizedNode readAllData(final @NonNull RestconfStrategy strategy,
             final YangInstanceIdentifier path, final String withDefa, final EffectiveModelContext ctx) {
         // PREPARE STATE DATA NODE
-        final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path,
-            false);
-
+        final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
         // PREPARE CONFIG DATA NODE
-        final NormalizedNode configDataNode;
-        //Here will be closed transactionChain if any
-        if (withDefa == null) {
-            configDataNode = readDataViaTransaction(
-                    strategy, LogicalDatastoreType.CONFIGURATION, path, true);
-        } else {
-            configDataNode = prepareDataByParamWithDef(
-                    readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true),
-                    path, withDefa, ctx);
-        }
+        final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION,
+            path);
 
-        return mergeConfigAndSTateDataIfNeeded(stateDataNode, configDataNode);
+        return mergeConfigAndSTateDataIfNeeded(stateDataNode,
+            withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
     }
 
     /**
@@ -517,20 +490,13 @@ public final class ReadDataTransactionUtil {
             final @NonNull EffectiveModelContext ctx, final @NonNull List<YangInstanceIdentifier> fields) {
         // PREPARE STATE DATA NODE
         final NormalizedNode stateDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path,
-            false, fields);
+            fields);
 
         // PREPARE CONFIG DATA NODE
-        final NormalizedNode configDataNode;
-        //Here will be closed transactionChain if any
-        if (withDefa == null) {
-            configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true, fields);
-        } else {
-            configDataNode = prepareDataByParamWithDef(
-                    readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true, fields),
-                    path, withDefa, ctx);
-        }
-
-        return mergeConfigAndSTateDataIfNeeded(stateDataNode, configDataNode);
+        final NormalizedNode configDataNode = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
+            fields);
+        return mergeConfigAndSTateDataIfNeeded(stateDataNode,
+            withDefa == null ? configDataNode : prepareDataByParamWithDef(configDataNode, path, withDefa, ctx));
     }
 
     private static NormalizedNode mergeConfigAndSTateDataIfNeeded(final NormalizedNode stateDataNode,