InstanceIdentifierContext does not take generics
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / PutDataTransactionUtil.java
index 3c31d3e26acf6a7be1aec8a32bebfc900eeb8d1e..6e688b3f6efdacde8a0ac0a79d4378ad14e3fc3e 100644 (file)
@@ -15,14 +15,16 @@ import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 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.InsertParam;
+import org.opendaylight.restconf.nb.rfc8040.PointParam;
+import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
+import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.PostPutQueryParameters.Insert;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
@@ -52,12 +54,11 @@ public final class PutDataTransactionUtil {
      * @param payload       data to put
      * @param schemaContext reference to {@link EffectiveModelContext}
      * @param strategy      object that perform the actual DS operations
-     * @param point         query parameter
-     * @param insert        query parameter
+     * @param params        {@link WriteDataParams}
      * @return {@link Response}
      */
-    public static Response putData(final NormalizedNodeContext payload, final EffectiveModelContext schemaContext,
-                                   final RestconfStrategy strategy, final Insert insert, final String point) {
+    public static Response putData(final NormalizedNodePayload payload, final EffectiveModelContext schemaContext,
+                                   final RestconfStrategy strategy, final WriteDataParams params) {
         final YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
 
         final FluentFuture<Boolean> existsFuture = strategy.exists(LogicalDatastoreType.CONFIGURATION, path);
@@ -67,9 +68,9 @@ public final class PutDataTransactionUtil {
         final ResponseFactory responseFactory =
             new ResponseFactory(existsResponse.result ? Status.NO_CONTENT : Status.CREATED);
         final FluentFuture<? extends CommitInfo> submitData = submitData(path, schemaContext, strategy,
-            payload.getData(), insert, point);
+            payload.getData(), params);
         //This method will close transactionChain if any
-        FutureCallbackTx.addCallback(submitData, PUT_TX_TYPE, responseFactory, strategy, path);
+        FutureCallbackTx.addCallback(submitData, PUT_TX_TYPE, responseFactory, path);
         return responseFactory.build();
     }
 
@@ -80,16 +81,16 @@ public final class PutDataTransactionUtil {
      * @param schemaContext {@link SchemaContext}
      * @param strategy      object that perform the actual DS operations
      * @param data          data
-     * @param point         query parameter
-     * @param insert        query parameter
+     * @param params        {@link WriteDataParams}
      * @return {@link FluentFuture}
      */
     private static FluentFuture<? extends CommitInfo> submitData(final YangInstanceIdentifier path,
                                                                  final EffectiveModelContext schemaContext,
                                                                  final RestconfStrategy strategy,
                                                                  final NormalizedNode data,
-                                                                 final Insert insert, final String point) {
+                                                                 final WriteDataParams params) {
         final RestconfTransaction transaction = strategy.prepareWriteExecution();
+        final InsertParam insert = params.insert();
         if (insert == null) {
             return makePut(path, schemaContext, transaction, data);
         }
@@ -102,9 +103,9 @@ public final class PutDataTransactionUtil {
                 if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                     return makePut(path, schemaContext, transaction, data);
                 }
-                transaction.remove(LogicalDatastoreType.CONFIGURATION, path.getParent());
-                transaction.replace(LogicalDatastoreType.CONFIGURATION, path, data, schemaContext);
-                transaction.replace(LogicalDatastoreType.CONFIGURATION, path.getParent(), readData, schemaContext);
+                transaction.remove(path.getParent());
+                transaction.replace(path, data, schemaContext);
+                transaction.replace(path.getParent(), readData, schemaContext);
                 return transaction.commit();
             case LAST:
                 return makePut(path, schemaContext, transaction, data);
@@ -113,39 +114,39 @@ public final class PutDataTransactionUtil {
                 if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                     return makePut(path, schemaContext, transaction, data);
                 }
-                insertWithPointPut(transaction, path, data, schemaContext, point, (NormalizedNodeContainer<?>) readData,
-                    true);
+                insertWithPointPut(transaction, path, data, schemaContext, params.getPoint(),
+                    (NormalizedNodeContainer<?>) readData, true);
                 return transaction.commit();
             case AFTER:
                 readData = readList(strategy, path.getParent());
                 if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                     return makePut(path, schemaContext, transaction, data);
                 }
-                insertWithPointPut(transaction, path, data, schemaContext, point, (NormalizedNodeContainer<?>) readData,
-                    false);
+                insertWithPointPut(transaction, path, data, schemaContext, params.getPoint(),
+                    (NormalizedNodeContainer<?>) readData, false);
                 return transaction.commit();
             default:
                 throw new RestconfDocumentedException(
                         "Used bad value of insert parameter. Possible values are first, last, before or after, "
-                                + "but was: " + insert, RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
+                                + "but was: " + insert, ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
         }
     }
 
     // FIXME: this method is only called from a context where we are modifying data. This should be part of strategy,
     //        requiring an already-open transaction. It also must return a future, so it can be properly composed.
     static NormalizedNode readList(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
-        return ReadDataTransactionUtil.readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
-            false);
+        return ReadDataTransactionUtil.readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
     }
 
     private static void insertWithPointPut(final RestconfTransaction transaction,
                                            final YangInstanceIdentifier path,
                                            final NormalizedNode data,
-                                           final EffectiveModelContext schemaContext, final String point,
+                                           final EffectiveModelContext schemaContext, final PointParam point,
                                            final NormalizedNodeContainer<?> readList, final boolean before) {
-        transaction.remove(LogicalDatastoreType.CONFIGURATION, path.getParent());
-        final InstanceIdentifierContext<?> instanceIdentifier =
-            ParserIdentifier.toInstanceIdentifier(point, schemaContext, Optional.empty());
+        transaction.remove(path.getParent());
+        final InstanceIdentifierContext instanceIdentifier =
+            // FIXME: Point should be able to give us this method
+            ParserIdentifier.toInstanceIdentifier(point.value(), schemaContext, Optional.empty());
         int lastItemPosition = 0;
         for (final NormalizedNode nodeChild : readList.body()) {
             if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
@@ -158,15 +159,13 @@ public final class PutDataTransactionUtil {
         }
         int lastInsertedPosition = 0;
         final NormalizedNode emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path.getParent());
-        transaction.merge(LogicalDatastoreType.CONFIGURATION,
-            YangInstanceIdentifier.create(emptySubtree.getIdentifier()),
-            emptySubtree);
+        transaction.merge(YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
         for (final NormalizedNode nodeChild : readList.body()) {
             if (lastInsertedPosition == lastItemPosition) {
-                transaction.replace(LogicalDatastoreType.CONFIGURATION, path, data, schemaContext);
+                transaction.replace(path, data, schemaContext);
             }
             final YangInstanceIdentifier childPath = path.getParent().node(nodeChild.getIdentifier());
-            transaction.replace(LogicalDatastoreType.CONFIGURATION, childPath, nodeChild, schemaContext);
+            transaction.replace(childPath, nodeChild, schemaContext);
             lastInsertedPosition++;
         }
     }
@@ -175,7 +174,7 @@ public final class PutDataTransactionUtil {
                                                               final SchemaContext schemaContext,
                                                               final RestconfTransaction transaction,
                                                               final NormalizedNode data) {
-        transaction.replace(LogicalDatastoreType.CONFIGURATION, path, data, schemaContext);
+        transaction.replace(path, data, schemaContext);
         return transaction.commit();
     }
 
@@ -188,7 +187,7 @@ public final class PutDataTransactionUtil {
         if (dataSchemaNode instanceof ListSchemaNode) {
             if (!((ListSchemaNode) dataSchemaNode).isUserOrdered()) {
                 throw new RestconfDocumentedException("Insert parameter can be used only with ordered-by user list.",
-                        RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
+                        ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
             }
             return dataSchemaNode;
         }
@@ -196,11 +195,11 @@ public final class PutDataTransactionUtil {
             if (!((LeafListSchemaNode) dataSchemaNode).isUserOrdered()) {
                 throw new RestconfDocumentedException(
                         "Insert parameter can be used only with ordered-by user leaf-list.",
-                        RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
+                        ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
             }
             return dataSchemaNode;
         }
         throw new RestconfDocumentedException("Insert parameter can be used only with list or leaf-list",
-                RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
+                ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
     }
 }