BUG-4684: validate changes against effective state
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractValueNodeModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22
23 abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
24     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
25     private final T schema;
26
27     protected AbstractValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
28         this.nodeClass = Preconditions.checkNotNull(nodeClass);
29         this.schema = schema;
30     }
31
32     @Override
33     protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
34         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
35     }
36
37     @Override
38     public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
39         throw new UnsupportedOperationException("Node " + schema.getPath()
40                 + "is leaf type node. Child nodes not allowed");
41     }
42
43     @Override
44     protected final ChildTrackingPolicy getChildPolicy() {
45         return ChildTrackingPolicy.NONE;
46     }
47
48     @Override
49     protected final TreeNode applyTouch(final ModifiedNode modification,
50             final TreeNode currentMeta, final Version version) {
51         throw new UnsupportedOperationException("Node " + schema.getPath()
52                 + "is leaf type node. Subtree change is not allowed.");
53     }
54
55     @Override
56     protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
57             final Version version) {
58         // Just overwrite whatever was there
59         modification.resolveModificationType(ModificationType.WRITE);
60         return applyWrite(modification, null, version);
61     }
62
63     @Override
64     protected final TreeNode applyWrite(final ModifiedNode modification,
65             final Optional<TreeNode> currentMeta, final Version version) {
66         return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
67     }
68
69     @Override
70     protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
71             final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
72         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
73     }
74
75     @Override
76     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
77
78         switch (node.getOperation()) {
79             // Delete performs a data dependency check on existence of the node. Performing a merge
80             // on DELETE means we
81             // are really performing a write.
82             case DELETE:
83             case WRITE:
84                 node.write(value);
85                 break;
86             default:
87                 node.updateValue(LogicalOperation.MERGE, value);
88         }
89     }
90
91     @Override
92     void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
93         verifyStructure(value, false);
94     }
95 }