b725c0b0f408b260ca6765183230748f97bd8e87
[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
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23
24 abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
25     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
26     private final T schema;
27
28     protected AbstractValueNodeModificationStrategy(final T schema,
29             final Class<? extends NormalizedNode<?, ?>> nodeClass) {
30         this.nodeClass = Preconditions.checkNotNull(nodeClass);
31         this.schema = schema;
32     }
33
34     @Override
35     protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
36         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
37     }
38
39     @Override
40     public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
41         throw new UnsupportedOperationException("Node " + schema.getPath()
42                 + "is leaf type node. Child nodes not allowed");
43     }
44
45     @Override
46     protected final ChildTrackingPolicy getChildPolicy() {
47         return ChildTrackingPolicy.NONE;
48     }
49
50     @Override
51     protected final TreeNode applyTouch(final ModifiedNode modification,
52             final TreeNode currentMeta, final Version version) {
53         throw new UnsupportedOperationException("Node " + schema.getPath()
54                 + "is leaf type node. Subtree change is not allowed.");
55     }
56
57     @Override
58     protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
59             final Version version) {
60         // Just overwrite whatever was there, but be sure to run validation
61         verifyStructure(modification.getWrittenValue(), true);
62         modification.resolveModificationType(ModificationType.WRITE);
63         return applyWrite(modification, null, version);
64     }
65
66     @Override
67     protected final TreeNode applyWrite(final ModifiedNode modification,
68             final Optional<TreeNode> currentMeta, final Version version) {
69         return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
70     }
71
72     @Override
73     protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
74             final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
75         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
76     }
77
78     @Override
79     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
80
81         switch (node.getOperation()) {
82             // Delete performs a data dependency check on existence of the node. Performing a merge
83             // on DELETE means we
84             // are really performing a write.
85             case DELETE:
86             case WRITE:
87                 node.write(value);
88                 break;
89             default:
90                 node.updateValue(LogicalOperation.MERGE, value);
91         }
92     }
93
94     @Override
95     void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
96         verifyStructure(value, false);
97     }
98 }