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