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