Define a feature-parent
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / ValueNodeModificationStrategy.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.tree.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import org.eclipse.jdt.annotation.NonNull;
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.tree.api.IncorrectDataStructureException;
18 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
19 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
20 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22
23 final class ValueNodeModificationStrategy<T extends DataSchemaNode, V extends NormalizedNode>
24         extends SchemaAwareApplyOperation<T> {
25     private final @NonNull Class<V> nodeClass;
26     private final @NonNull T schema;
27
28     ValueNodeModificationStrategy(final Class<V> nodeClass, final T schema) {
29         this.nodeClass = requireNonNull(nodeClass);
30         this.schema = requireNonNull(schema);
31     }
32
33     @Override
34     T getSchema() {
35         return schema;
36     }
37
38     @Override
39     public ModificationApplyOperation childByArg(final PathArgument arg) {
40         throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
41     }
42
43     @Override
44     protected ChildTrackingPolicy getChildPolicy() {
45         return ChildTrackingPolicy.NONE;
46     }
47
48     @Override
49     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
50             final Version version) {
51         throw new UnsupportedOperationException("Node " + schema + " is leaf type node. "
52             + " Subtree change is not allowed.");
53     }
54
55     @Override
56     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
57             final Version version) {
58         // Just overwrite whatever was there, but be sure to run validation
59         final NormalizedNode newValue = modification.getWrittenValue();
60         verifyWrittenValue(newValue);
61         modification.resolveModificationType(ModificationType.WRITE);
62         return applyWrite(modification, newValue, null, version);
63     }
64
65     @Override
66     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
67             final TreeNode currentMeta, final Version version) {
68         return TreeNode.of(newValue, version);
69     }
70
71     @Override
72     protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
73             final TreeNode currentMeta, final Version version) throws IncorrectDataStructureException {
74         throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
75     }
76
77     @Override
78     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) {
79         switch (node.getOperation()) {
80             // Delete performs a data dependency check on existence of the node. Performing a merge on DELETE means we
81             // are really performing a write.
82             case DELETE, WRITE -> node.write(value);
83             default -> node.updateValue(LogicalOperation.MERGE, value);
84         }
85     }
86
87     @Override
88     void verifyValue(final NormalizedNode writtenValue) {
89         verifyWrittenValue(writtenValue);
90     }
91
92     @Override
93     void recursivelyVerifyStructure(final NormalizedNode value) {
94         verifyWrittenValue(value);
95     }
96
97     @Override
98     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
99         return helper.add("value", nodeClass.getSimpleName());
100     }
101
102     private void verifyWrittenValue(final NormalizedNode value) {
103         checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);
104     }
105 }