Fix child ordering assumptions
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.java
index e1df47c7d4df2d20ad65e7caf2860b77726801b0..6d20d385ee194b908f6aa015679324fb5cbd3e39 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 
@@ -25,15 +26,39 @@ final class OperationWithModification {
         this.applyOperation = op;
     }
 
-    public OperationWithModification write(final NormalizedNode<?, ?> value) {
+    void write(final NormalizedNode<?, ?> value) {
         modification.write(value);
         applyOperation.verifyStructure(modification);
-        return this;
     }
 
-    public OperationWithModification delete() {
+    private void recursiveMerge(final NormalizedNode<?,?> data) {
+        if (data instanceof NormalizedNodeContainer<?,?,?>) {
+            @SuppressWarnings({ "rawtypes", "unchecked" })
+            NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
+            for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
+                PathArgument childId = child.getIdentifier();
+                forChild(childId).recursiveMerge(child);
+            }
+        }
+
+        modification.merge(data);
+    }
+
+    void merge(final NormalizedNode<?, ?> data) {
+        /*
+         * A merge operation will end up overwriting parts of the tree, retaining others.
+         * We want to make sure we do not validate the complete resulting structure, but
+         * rather just what was written. In order to do that, we first pretend the data
+         * was written, run verification and then perform the merge -- with the explicit
+         * assumption that adding the newly-validated data with the previously-validated
+         * data will not result in invalid data.
+         */
+        applyOperation.verifyStructure(modification.asNewlyWritten(data));
+        recursiveMerge(data);
+    }
+
+    void delete() {
         modification.delete();
-        return this;
     }
 
     public ModifiedNode getModification() {
@@ -51,22 +76,11 @@ final class OperationWithModification {
     public static OperationWithModification from(final ModificationApplyOperation operation,
             final ModifiedNode modification) {
         return new OperationWithModification(operation, modification);
-
     }
 
-    public void merge(final NormalizedNode<?, ?> data) {
-        modification.merge(data);
-        applyOperation.verifyStructure(modification);
-
-    }
-
-    public OperationWithModification forChild(final PathArgument childId) {
+    private OperationWithModification forChild(final PathArgument childId) {
         ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
-        boolean isOrdered = true;
-        if (childOp instanceof SchemaAwareApplyOperation) {
-            isOrdered = ((SchemaAwareApplyOperation) childOp).isOrdered();
-        }
-        ModifiedNode childMod = modification.modifyChild(childId, isOrdered);
+        ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
 
         return from(childOp,childMod);
     }