BUG-1014: Moved recursive verify of written data to ready()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.java
index 3293a05b066edea7cc77a9e51796794063fd4c4c..a375a90d74dcbca18df6ffb06ccb916727b7c09d 100644 (file)
@@ -7,8 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 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;
@@ -28,15 +28,36 @@ final class OperationWithModification {
 
     void write(final NormalizedNode<?, ?> value) {
         modification.write(value);
-        applyOperation.verifyStructure(modification);
+        /**
+         * Fast validation of structure, full validation on written data will be run during seal.
+         */
+        applyOperation.verifyStructure(value, false);
     }
 
     private void recursiveMerge(final NormalizedNode<?,?> data) {
         if (data instanceof NormalizedNodeContainer<?,?,?>) {
             @SuppressWarnings({ "rawtypes", "unchecked" })
+            final
             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
-            for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
-                PathArgument childId = child.getIdentifier();
+
+            /*
+             * if there was write before on this node and it is of NormalizedNodeContainer type
+             * merge would overwrite our changes. So we create write modifications from data children to
+             * retain children created by past write operation.
+             * These writes will then be pushed down in the tree while there are merge modifications on these children
+             */
+            if (modification.getOperation().equals(LogicalOperation.WRITE)) {
+                @SuppressWarnings({ "rawtypes", "unchecked" })
+                final
+                NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
+                        (NormalizedNodeContainer) modification.getWrittenValue();
+                for (final NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
+                    final PathArgument childId = child.getIdentifier();
+                    forChild(childId).write(child);
+                }
+            }
+            for (final NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
+                final PathArgument childId = child.getIdentifier();
                 forChild(childId).recursiveMerge(child);
             }
         }
@@ -46,14 +67,15 @@ final class OperationWithModification {
 
     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.
+         * 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.
+         *
+         * FIXME: Should be this moved to recursive merge and run for each node?
          */
-        applyOperation.verifyStructure(modification.asNewlyWritten(data));
+        applyOperation.verifyStructure(data, false);
         recursiveMerge(data);
     }
 
@@ -79,17 +101,12 @@ final class OperationWithModification {
     }
 
     private OperationWithModification forChild(final PathArgument childId) {
-        ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
-
-        final boolean isOrdered;
-        if (childOp instanceof SchemaAwareApplyOperation) {
-            isOrdered = ((SchemaAwareApplyOperation) childOp).isOrdered();
-        } else {
-            isOrdered = true;
-        }
+        final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
+        Preconditions.checkArgument(maybeChildOp.isPresent(), "Attempted to apply operation to non-existent child %s", childId);
 
-        ModifiedNode childMod = modification.modifyChild(childId, isOrdered);
+        final ModificationApplyOperation childOp = maybeChildOp.get();
+        final ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
 
-        return from(childOp,childMod);
+        return from(childOp, childMod);
     }
 }