Fix automatic lifecycle delete stacking
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractValueNodeModificationStrategy.java
index 6f4e8497ac387879dcce3c2b11ad9d4916896a6a..ff50d991af3d23bb38c0bd12f0c764a22d0f887e 100644 (file)
@@ -8,9 +8,9 @@
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import com.google.common.base.Optional;
+
 import com.google.common.base.Preconditions;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import java.util.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.tree.IncorrectDataStructureException;
@@ -24,20 +24,21 @@ abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> e
     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
     private final T schema;
 
-    protected AbstractValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+    protected AbstractValueNodeModificationStrategy(final T schema,
+            final Class<? extends NormalizedNode<?, ?>> nodeClass) {
         this.nodeClass = Preconditions.checkNotNull(nodeClass);
         this.schema = schema;
     }
 
     @Override
-    protected final void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
+    protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
     }
 
     @Override
     public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
         throw new UnsupportedOperationException("Node " + schema.getPath()
-                + "is leaf type node. Child nodes not allowed");
+                + " is leaf type node. Child nodes not allowed");
     }
 
     @Override
@@ -46,29 +47,52 @@ abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> e
     }
 
     @Override
-    protected final TreeNode applyTouch(final ModifiedNode modification,
-            final TreeNode currentMeta, final Version version) {
+    protected final TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
+            final Version version) {
         throw new UnsupportedOperationException("Node " + schema.getPath()
-                + "is leaf type node. Subtree change is not allowed.");
+                + " is leaf type node. Subtree change is not allowed.");
     }
 
     @Override
     protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
             final Version version) {
-        // Just overwrite whatever was there
+        // Just overwrite whatever was there, but be sure to run validation
+        final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
+        verifyStructure(newValue, true);
         modification.resolveModificationType(ModificationType.WRITE);
-        return applyWrite(modification, null, version);
+        return applyWrite(modification, newValue, null, version);
     }
 
     @Override
-    protected final TreeNode applyWrite(final ModifiedNode modification,
+    protected final TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
             final Optional<TreeNode> currentMeta, final Version version) {
-        return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
+        return TreeNodeFactory.createTreeNode(newValue, version);
+    }
+
+    @Override
+    protected final void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
+            final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
+        throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
+    }
+
+    @Override
+    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
+
+        switch (node.getOperation()) {
+            // Delete performs a data dependency check on existence of the node. Performing a merge
+            // on DELETE means we
+            // are really performing a write.
+            case DELETE:
+            case WRITE:
+                node.write(value);
+                break;
+            default:
+                node.updateValue(LogicalOperation.MERGE, value);
+        }
     }
 
     @Override
-    protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
-            final Optional<TreeNode> current) throws IncorrectDataStructureException {
-        throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
+    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+        verifyStructure(value, false);
     }
-}
\ No newline at end of file
+}