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