BUG-4295: instantiate MERGE operations lazily
[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, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
29         this.nodeClass = Preconditions.checkNotNull(nodeClass);
30         this.schema = schema;
31     }
32
33     @Override
34     protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
35         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
36     }
37
38     @Override
39     public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
40         throw new UnsupportedOperationException("Node " + schema.getPath()
41                 + "is leaf type node. Child nodes not allowed");
42     }
43
44     @Override
45     protected final ChildTrackingPolicy getChildPolicy() {
46         return ChildTrackingPolicy.NONE;
47     }
48
49     @Override
50     protected final TreeNode applyTouch(final ModifiedNode modification,
51             final TreeNode currentMeta, final Version version) {
52         throw new UnsupportedOperationException("Node " + schema.getPath()
53                 + "is leaf type node. Subtree change is not allowed.");
54     }
55
56     @Override
57     protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
58             final Version version) {
59         // Just overwrite whatever was there
60         modification.resolveModificationType(ModificationType.WRITE);
61         return applyWrite(modification, null, version);
62     }
63
64     @Override
65     protected final TreeNode applyWrite(final ModifiedNode modification,
66             final Optional<TreeNode> currentMeta, final Version version) {
67         return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
68     }
69
70     @Override
71     protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
72             final Optional<TreeNode> current) throws IncorrectDataStructureException {
73         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
74     }
75
76     @Override
77     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
78
79         switch (node.getOperation()) {
80             // Delete performs a data dependency check on existence of the node. Performing a merge
81             // on DELETE means we
82             // are really performing a write.
83             case DELETE:
84             case WRITE:
85                 node.write(value);
86                 break;
87             default:
88                 node.updateValue(LogicalOperation.MERGE, value);
89         }
90     }
91
92     @Override
93     void recursivelyVerifyStructure(NormalizedNode<?, ?> value) {
94         verifyStructure(value, false);
95     }
96 }