2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Predicate;
13 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
24 * Node Modification Node and Tree
26 * Tree which structurally resembles data tree and captures client modifications
27 * to the data store tree.
29 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
30 * and {@link TreeNode} which represents original state as tracked by {@link #getOriginal()}.
33 final class ModifiedNode extends NodeModification implements StoreTreeNode<ModifiedNode> {
34 static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
36 public boolean apply(@Nonnull final ModifiedNode input) {
37 Preconditions.checkNotNull(input);
38 switch (input.getOperation()) {
48 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getOperation()));
52 private final Map<PathArgument, ModifiedNode> children;
53 private final Optional<TreeNode> original;
54 private final PathArgument identifier;
55 private LogicalOperation operation = LogicalOperation.NONE;
56 private Optional<TreeNode> snapshotCache;
57 private NormalizedNode<?, ?> value;
58 private ModificationType modType;
60 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, final ChildTrackingPolicy childPolicy) {
61 this.identifier = identifier;
62 this.original = original;
63 this.children = childPolicy.createMap();
67 * Return the value which was written to this node.
69 * @return Currently-written value
71 public NormalizedNode<?, ?> getWrittenValue() {
76 public PathArgument getIdentifier() {
81 Optional<TreeNode> getOriginal() {
86 LogicalOperation getOperation() {
92 * Returns child modification if child was modified
94 * @return Child modification if direct child or it's subtree
99 public Optional<ModifiedNode> getChild(final PathArgument child) {
100 return Optional.<ModifiedNode> fromNullable(children.get(child));
105 * Returns child modification if child was modified, creates {@link ModifiedNode}
106 * for child otherwise.
108 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
109 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
111 * @param child child identifier, may not be null
112 * @param childPolicy child tracking policy for the node we are looking for
113 * @return {@link ModifiedNode} for specified child, with {@link #getOriginal()}
114 * containing child metadata if child was present in original data.
116 ModifiedNode modifyChild(@Nonnull final PathArgument child, @Nonnull final ChildTrackingPolicy childPolicy) {
118 if (operation == LogicalOperation.NONE) {
119 updateOperationType(LogicalOperation.TOUCH);
121 final ModifiedNode potential = children.get(child);
122 if (potential != null) {
126 final Optional<TreeNode> currentMetadata;
127 if (original.isPresent()) {
128 final TreeNode orig = original.get();
129 currentMetadata = orig.getChild(child);
131 currentMetadata = Optional.absent();
134 final ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata, childPolicy);
135 children.put(child, newlyCreated);
140 * Returns all recorded direct child modification
142 * @return all recorded direct child modifications
145 Collection<ModifiedNode> getChildren() {
146 return children.values();
150 * Records a delete for associated node.
153 final LogicalOperation newType;
158 // We need to record this delete.
159 newType = LogicalOperation.DELETE;
165 * We are canceling a previous modification. This is a bit tricky,
166 * as the original write may have just introduced the data, or it
167 * may have modified it.
169 * As documented in BUG-2470, a delete of data introduced in this
170 * transaction needs to be turned into a no-op.
172 newType = original.isPresent() ? LogicalOperation.DELETE : LogicalOperation.NONE;
175 throw new IllegalStateException("Unhandled deletion of node with " + operation);
181 updateOperationType(newType);
185 * Records a write for associated node.
189 void write(final NormalizedNode<?, ?> value) {
191 updateOperationType(LogicalOperation.WRITE);
196 void merge(final NormalizedNode<?, ?> value) {
198 updateOperationType(LogicalOperation.MERGE);
201 * Blind overwrite of any previous data is okay, no matter whether the node
202 * is simple or complex type.
204 * If this is a simple or complex type with unkeyed children, this merge will
205 * be turned into a write operation, overwriting whatever was there before.
207 * If this is a container with keyed children, there are two possibilities:
208 * - if it existed before, this value will never be consulted and the children
209 * will get explicitly merged onto the original data.
210 * - if it did not exist before, this value will be used as a seed write and
211 * children will be merged into it.
212 * In either case we rely on OperationWithModification to manipulate the children
213 * before calling this method, so unlike a write we do not want to clear them.
219 * Seal the modification node and prune any children which has not been modified.
223 void seal(final ModificationApplyOperation schema) {
226 // A TOUCH node without any children is a no-op
229 if (children.isEmpty()) {
230 updateOperationType(LogicalOperation.NONE);
234 schema.verifyStructure(value, true);
241 private void clearSnapshot() {
242 snapshotCache = null;
245 Optional<TreeNode> getSnapshot() {
246 return snapshotCache;
249 Optional<TreeNode> setSnapshot(final Optional<TreeNode> snapshot) {
250 snapshotCache = Preconditions.checkNotNull(snapshot);
254 private void updateOperationType(final LogicalOperation type) {
261 public String toString() {
262 return "NodeModification [identifier=" + identifier + ", modificationType="
263 + operation + ", childModification=" + children + "]";
266 void resolveModificationType(@Nonnull final ModificationType type) {
271 * Return the physical modification done to data. May return null if the
272 * operation has not been applied to the underlying tree. This is different
273 * from the logical operation in that it can actually be a no-op if the
274 * operation has no side-effects (like an empty merge on a container).
276 * @return Modification type.
278 ModificationType getModificationType() {
283 * Create a node which will reflect the state of this node, except it will behave as newly-written
284 * value. This is useful only for merge validation.
286 * @param value Value associated with the node
287 * @return An isolated node. This node should never reach a datatree.
289 ModifiedNode asNewlyWritten(final NormalizedNode<?, ?> value) {
291 * We are instantiating an "equivalent" of this node. Currently the only callsite does not care
292 * about the actual iteration order, so we do not have to specify the same tracking policy as
293 * we were instantiated with. Since this is the only time we need to know that policy (it affects
294 * only things in constructor), we do not want to retain it (saves some memory on per-instance
297 * We could reconstruct it using two instanceof checks (to undo what the constructor has done),
298 * which would give perfect results. The memory saving would be at most 32 bytes of a short-lived
299 * object, so let's not bother with that.
301 final ModifiedNode ret = new ModifiedNode(getIdentifier(), Optional.<TreeNode>absent(), ChildTrackingPolicy.UNORDERED);
306 public static ModifiedNode createUnmodified(final TreeNode metadataTree, final ChildTrackingPolicy childPolicy) {
307 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), childPolicy);