Refactor ModificationApplyOperation
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / 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.tree.impl;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.Nullable;
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.tree.StoreTreeNode;
17 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
18 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
19 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
20
21 /**
22  * An operation responsible for applying {@link ModifiedNode} on tree. The operation is a hierachical composite -
23  * the operation on top level node consists of 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>Implementations MUST expose all nested suboperations which operates on child nodes expose via
30  *       {@link #childByArg(PathArgument)} method.</li>
31  *   <li>Same suboperations SHOULD be used when invoked via {@link #apply(ModifiedNode, TreeNode, Version)},
32  *       if applicable.</li>
33  *   <li>There are exactly two base implementations:
34  *     <ul>
35  *       <li>{@link SchemaAwareApplyOperation}, which serves as the base class for stateful mutators -- directly
36  *           impacting the layout and transitions of the {@link TreeNode} hierarchy.
37  *       <li>{@link AbstractValidation}, which serves as the base class for stateless checks, which work purely on top
38  *           of the {@link TreeNode} hierarchy. These are always overlaid on top of some other
39  *           {@link ModificationApplyOperation}, ultimately leading to a {@link SchemaAwareApplyOperation}.
40  *     </ul>
41  *     This allows baseline invocations from {@link OperationWithModification} to be bimorphic in the first line of
42  *     dispatch.
43  *   </li>
44  * </ul>
45  */
46 abstract sealed class ModificationApplyOperation implements StoreTreeNode<ModificationApplyOperation>
47         permits AbstractValidation, SchemaAwareApplyOperation {
48     /**
49      * Implementation of this operation must be stateless and must not change state of this object.
50      *
51      * @param modification NodeModification to be applied
52      * @param currentMeta Store Metadata Node on which NodeModification should be applied
53      * @param version New subtree version of parent node
54      * @return new {@link TreeNode} if operation resulted in updating node, {@link Optional#empty()} if
55      *         {@link ModifiedNode} resulted in deletion of this node.
56      * @throws IllegalArgumentException If it is not possible to apply Operation on provided Metadata node
57      */
58     abstract Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta,
59             Version version);
60
61     /**
62      * Checks if provided node modification could be applied to current metadata node.
63      *
64      * @param path Path to modification
65      * @param modification Modification
66      * @param currentMeta Metadata Node to which modification should be applied
67      * @param version Metadata version
68      * @throws DataValidationFailedException if the modification is not applicable
69      */
70     abstract void checkApplicable(ModificationPath path, NodeModification modification,
71         @Nullable TreeNode currentMeta, Version version) throws DataValidationFailedException;
72
73     /**
74      * Performs a quick structural verification of NodeModification, such as written values / types uses right
75      * structural elements.
76      *
77      * @param modification data to be verified.
78      * @throws IllegalArgumentException If provided NodeModification does not adhere to the
79      *         structure.
80      */
81     abstract void quickVerifyStructure(NormalizedNode modification);
82
83     /**
84      * Performs a full structural verification of NodeModification, such as written values / types uses right
85      * structural elements. Unlike {@link #quickVerifyStructure(NormalizedNode)} this includes recursively checking
86      * children, too.
87      *
88      * @param modification data to be verified.
89      * @throws IllegalArgumentException If provided NodeModification does not adhere to the
90      *         structure.
91      */
92     abstract void fullVerifyStructure(NormalizedNode modification);
93
94     /**
95      * Return the tracking policy for this node's children.
96      *
97      * @return Tracking policy, may not be null.
98      */
99     abstract ChildTrackingPolicy getChildPolicy();
100
101     /**
102      * Stage a merge operation into a {@link ModifiedNode} such that it will be processed correctly by
103      * {@link #apply(ModifiedNode, TreeNode, Version)}. This method is the context which is introducing this operation,
104      * and so any overheads are charged to whoever is in control of the access pattern.
105      *
106      * @param modification Original modification node
107      * @param value Value which should be merge into the modification
108      * @param version Data version as carried in the containing {@link InMemoryDataTreeModification}
109      */
110     abstract void mergeIntoModifiedNode(ModifiedNode modification, NormalizedNode value, Version version);
111
112     /**
113      * {@inheritDoc}
114      *
115      * @return Reference to suboperation for specified tree node, {@code null} if suboperation is not supported for
116      *         specified tree node.
117      */
118     @Override
119     public abstract ModificationApplyOperation childByArg(PathArgument arg);
120
121     abstract void recursivelyVerifyStructure(NormalizedNode value);
122
123     abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
124
125     @Override
126     public final String toString() {
127         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
128     }
129 }