Optimize ImmutableNodes.leafNode()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ModificationApplyOperation.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 java.util.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
17
18 /**
19  * Operation responsible for applying {@link ModifiedNode} on tree.
20  *
21  * <p>
22  * Operation is composite - operation on top level node consists of
23  * suboperations on child nodes. This allows to walk operation hierarchy and
24  * invoke suboperations independently.
25  *
26  * <p>
27  * <b>Implementation notes</b>
28  * <ul>
29  * <li>
30  * Implementations MUST expose all nested suboperations which operates on child
31  * nodes expose via {@link #getChild(PathArgument)} method.
32  * <li>Same suboperations SHOULD be used when invoked via
33  * {@link #apply(ModifiedNode, Optional, Version)} if applicable.
34  * </ul>
35  *
36  * <p>
37  * Hierarchical composite operation which is responsible for applying
38  * modification on particular subtree and creating updated subtree
39  */
40 abstract class ModificationApplyOperation implements StoreTreeNode<ModificationApplyOperation> {
41     /**
42      * Implementation of this operation must be stateless and must not change state of this object.
43      *
44      * @param modification
45      *            NodeModification to be applied
46      * @param storeMeta
47      *            Store Metadata Node on which NodeModification should be
48      *            applied
49      * @param version New subtree version of parent node
50      * @return new {@link TreeNode} if operation resulted in updating
51      *         node, {@link Optional#absent()} if {@link ModifiedNode}
52      *         resulted in deletion of this node.
53      * @throws IllegalArgumentException
54      *             If it is not possible to apply Operation on provided Metadata
55      *             node
56      */
57     abstract Optional<TreeNode> apply(ModifiedNode modification, Optional<TreeNode> storeMeta, Version version);
58
59     /**
60      * Checks if provided node modification could be applied to current metadata node.
61      *
62      * @param path Path to modification
63      * @param modification Modification
64      * @param current Metadata Node to which modification should be applied
65      * @param version Metadata version
66      * @throws DataValidationFailedException if the modification is not applicable
67      */
68     abstract void checkApplicable(ModificationPath path, NodeModification modification,
69             Optional<TreeNode> current, Version version) throws DataValidationFailedException;
70
71     /**
72      * Performs a quick structural verification of NodeModification, such as written values / types uses right
73      * structural elements.
74      *
75      * @param modification data to be verified.
76      * @throws IllegalArgumentException If provided NodeModification does not adhere to the
77      *         structure.
78      */
79     abstract void quickVerifyStructure(NormalizedNode<?, ?> modification);
80
81     /**
82      * Performs a full structural verification of NodeModification, such as written values / types uses right
83      * structural elements. Unlike {@link #quickVerifyStructure(NormalizedNode)} this includes recursively checking
84      * children, too.
85      *
86      * @param modification data to be verified.
87      * @throws IllegalArgumentException If provided NodeModification does not adhere to the
88      *         structure.
89      */
90     abstract void fullVerifyStructure(NormalizedNode<?, ?> modification);
91
92     /**
93      * Return the tracking policy for this node's children.
94      *
95      * @return Tracking policy, may not be null.
96      */
97     abstract ChildTrackingPolicy getChildPolicy();
98
99     /**
100      * Stage a merge operation into a {@link ModifiedNode} such that it will be processed correctly by
101      * {@link #apply(ModifiedNode, Optional, Version)}. This method is the context which is introducing this operation,
102      * and so any overheads are charged to whoever is in control of the access pattern.
103      *
104      * @param modification Original modification node
105      * @param value Value which should be merge into the modification
106      * @param version Data version as carried in the containing {@link InMemoryDataTreeModification}
107      */
108     abstract void mergeIntoModifiedNode(ModifiedNode modification, NormalizedNode<?, ?> value, Version version);
109
110     /**
111      * Returns a suboperation for specified tree node.
112      *
113      * @return Reference to suboperation for specified tree node, {@link Optional#empty()}
114      *         if suboperation is not supported for specified tree node.
115      */
116     @Override
117     public abstract Optional<ModificationApplyOperation> getChild(PathArgument child);
118
119     abstract void recursivelyVerifyStructure(NormalizedNode<?, ?> value);
120 }