Unify ModificationApplyOperation.toString()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ValueNodeModificationStrategy.java
index 3df0c80e959eef38d79d91e1ad45e28a20894d28..0251d535ba3d4090f1eed4e4b02aaf8f960d811a 100644 (file)
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Optional;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects.ToStringHelper;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-
-import static com.google.common.base.Preconditions.checkArgument;
-
-abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
 
-    private final T schema;
-    private final Class<? extends NormalizedNode<?, ?>> nodeClass;
+final class ValueNodeModificationStrategy<T extends DataSchemaNode, V extends NormalizedNode<?, ?>>
+        extends SchemaAwareApplyOperation<T> {
+    private final @NonNull Class<V> nodeClass;
+    private final @NonNull T schema;
 
-    protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
-        super();
-        this.schema = schema;
-        this.nodeClass = nodeClass;
+    ValueNodeModificationStrategy(final Class<V> nodeClass, final T schema) {
+        this.nodeClass = requireNonNull(nodeClass);
+        this.schema = requireNonNull(schema);
     }
 
     @Override
-    protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
-        checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
+    T getSchema() {
+        return schema;
     }
 
     @Override
     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
-        throw new UnsupportedOperationException("Node " + schema.getPath()
-                + "is leaf type node. Child nodes not allowed");
+        throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
     }
 
     @Override
-    protected TreeNode applySubtreeChange(final ModifiedNode modification,
-            final TreeNode currentMeta, final Version version) {
-        throw new UnsupportedOperationException("Node " + schema.getPath()
-                + "is leaf type node. Subtree change is not allowed.");
+    protected ChildTrackingPolicy getChildPolicy() {
+        return ChildTrackingPolicy.NONE;
+    }
+
+    @Override
+    protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
+            final Version version) {
+        throw new UnsupportedOperationException("Node " + schema + " is leaf type node. "
+            + " Subtree change is not allowed.");
     }
 
     @Override
     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
             final Version version) {
-        // Just overwrite whatever was there
-        return applyWrite(modification, null, version);
+        // Just overwrite whatever was there, but be sure to run validation
+        final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
+        verifyWrittenValue(newValue);
+        modification.resolveModificationType(ModificationType.WRITE);
+        return applyWrite(modification, newValue, null, version);
     }
 
     @Override
-    protected TreeNode applyWrite(final ModifiedNode modification,
-            final Optional<TreeNode> currentMeta, final Version version) {
-        return TreeNodeFactory.createTreeNodeRecursively(modification.getWrittenValue(), version);
+    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
+            final Optional<? extends TreeNode> currentMeta, final Version version) {
+        return TreeNodeFactory.createTreeNode(newValue, version);
     }
 
     @Override
-    protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
-            final Optional<TreeNode> current) throws IncorrectDataStructureException {
-        throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
+    protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
+            final Optional<? extends TreeNode> current, final Version version) throws IncorrectDataStructureException {
+        throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
     }
 
-    public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
-        @SuppressWarnings({ "unchecked", "rawtypes" })
-        protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
-            super(schema, (Class) LeafSetEntryNode.class);
+    @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);
         }
     }
 
-    public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
-        @SuppressWarnings({ "unchecked", "rawtypes" })
-        protected LeafModificationStrategy(final LeafSchemaNode schema) {
-            super(schema, (Class) LeafNode.class);
-        }
+    @Override
+    void verifyValue(final NormalizedNode<?, ?> writtenValue) {
+        verifyWrittenValue(writtenValue);
+    }
+
+    @Override
+    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+        verifyWrittenValue(value);
+    }
+
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return helper.add("value", nodeClass.getSimpleName());
+    }
+
+    private void verifyWrittenValue(final NormalizedNode<?, ?> value) {
+        checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);
     }
-}
\ No newline at end of file
+}