Do not use Optional in returns
[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 org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
16 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
17 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
18 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
19
20 /**
21  * An operation responsible for applying {@link ModifiedNode} on tree. The operation is a hierachical composite -
22  * the operation on top level node consists of suboperations on child nodes. This allows to walk operation hierarchy and
23  * invoke suboperations independently.
24  *
25  * <p>
26  * <b>Implementation notes</b>
27  * <ul>
28  *   <li>Implementations MUST expose all nested suboperations which operates on child nodes expose via
29  *       {@link #childByArg(PathArgument)} method.</li>
30  *   <li>Same suboperations SHOULD be used when invoked via {@link #apply(ModifiedNode, TreeNode, Version)},
31  *       if applicable.</li>
32  *   <li>There are exactly two base implementations:
33  *     <ul>
34  *       <li>{@link SchemaAwareApplyOperation}, which serves as the base class for stateful mutators -- directly
35  *           impacting the layout and transitions of the {@link TreeNode} hierarchy.
36  *       <li>{@link AbstractValidation}, which serves as the base class for stateless checks, which work purely on top
37  *           of the {@link TreeNode} hierarchy. These are always overlaid on top of some other
38  *           {@link ModificationApplyOperation}, ultimately leading to a {@link SchemaAwareApplyOperation}.
39  *     </ul>
40  *     This allows baseline invocations from {@link OperationWithModification} to be bimorphic in the first line of
41  *     dispatch.
42  *   </li>
43  * </ul>
44  */
45 abstract sealed class ModificationApplyOperation implements StoreTreeNode<ModificationApplyOperation>
46         permits AbstractValidation, SchemaAwareApplyOperation {
47     /**
48      * Implementation of this operation must be stateless and must not change state of this object.
49      *
50      * @param modification NodeModification to be applied
51      * @param currentMeta Store Metadata Node on which NodeModification should be applied
52      * @param version New subtree version of parent node
53      * @return new {@link TreeNode} if operation resulted in updating node, {@code null} if {@link ModifiedNode}
54      *         resulted in deletion of this node.
55      * @throws IllegalArgumentException If it is not possible to apply Operation on provided Metadata node
56      */
57     abstract @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, 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 currentMeta 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         @Nullable TreeNode currentMeta, 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, TreeNode, 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      * {@inheritDoc}
112      *
113      * @return Reference to suboperation for specified tree node, {@code null} if suboperation is not supported for
114      *         specified tree node.
115      */
116     @Override
117     public abstract ModificationApplyOperation childByArg(PathArgument arg);
118
119     abstract void recursivelyVerifyStructure(NormalizedNode value);
120
121     abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
122
123     @Override
124     public final String toString() {
125         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
126     }
127 }