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;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.LinkedHashMap;
18 import javax.annotation.Nonnull;
19 import javax.annotation.concurrent.NotThreadSafe;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
27 * Node Modification Node and Tree
29 * Tree which structurally resembles data tree and captures client modifications
30 * to the data store tree.
32 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
33 * and {@link TreeNode} which represents original state as tracked by {@link #getOriginal()}.
36 final class ModifiedNode extends NodeModification implements StoreTreeNode<ModifiedNode> {
37 static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
39 public boolean apply(final @Nonnull ModifiedNode input) {
40 Preconditions.checkNotNull(input);
41 switch (input.getOperation()) {
51 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getOperation()));
55 private final Map<PathArgument, ModifiedNode> children;
56 private final Optional<TreeNode> original;
57 private final PathArgument identifier;
58 private LogicalOperation operation = LogicalOperation.NONE;
59 private Optional<TreeNode> snapshotCache;
60 private NormalizedNode<?, ?> value;
62 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, final boolean isOrdered) {
63 this.identifier = identifier;
64 this.original = original;
67 children = new LinkedHashMap<>();
69 children = new HashMap<>();
74 * Return the value which was written to this node.
76 * @return Currently-written value
78 public NormalizedNode<?, ?> getWrittenValue() {
83 public PathArgument getIdentifier() {
88 Optional<TreeNode> getOriginal() {
94 LogicalOperation getOperation() {
100 * Returns child modification if child was modified
102 * @return Child modification if direct child or it's subtree
107 public Optional<ModifiedNode> getChild(final PathArgument child) {
108 return Optional.<ModifiedNode> fromNullable(children.get(child));
113 * Returns child modification if child was modified, creates {@link ModifiedNode}
114 * for child otherwise.
116 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
117 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
120 * @return {@link ModifiedNode} for specified child, with {@link #getOriginal()}
121 * containing child metadata if child was present in original data.
123 ModifiedNode modifyChild(final PathArgument child, final boolean isOrdered) {
125 if (operation == LogicalOperation.NONE) {
126 updateModificationType(LogicalOperation.TOUCH);
128 final ModifiedNode potential = children.get(child);
129 if (potential != null) {
133 final Optional<TreeNode> currentMetadata;
134 if (original.isPresent()) {
135 final TreeNode orig = original.get();
136 currentMetadata = orig.getChild(child);
138 currentMetadata = Optional.absent();
141 final ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata, isOrdered);
142 children.put(child, newlyCreated);
147 * Returns all recorded direct child modification
149 * @return all recorded direct child modifications
152 Collection<ModifiedNode> getChildren() {
153 return children.values();
157 * Records a delete for associated node.
160 final LogicalOperation newType;
165 // We need to record this delete.
166 newType = LogicalOperation.DELETE;
172 * We are canceling a previous modification. This is a bit tricky,
173 * as the original write may have just introduced the data, or it
174 * may have modified it.
176 * As documented in BUG-2470, a delete of data introduced in this
177 * transaction needs to be turned into a no-op.
179 newType = original.isPresent() ? LogicalOperation.DELETE : LogicalOperation.NONE;
182 throw new IllegalStateException("Unhandled deletion of node with " + operation);
188 updateModificationType(newType);
192 * Records a write for associated node.
196 void write(final NormalizedNode<?, ?> value) {
198 updateModificationType(LogicalOperation.WRITE);
203 void merge(final NormalizedNode<?, ?> value) {
205 updateModificationType(LogicalOperation.MERGE);
208 * Blind overwrite of any previous data is okay, no matter whether the node
209 * is simple or complex type.
211 * If this is a simple or complex type with unkeyed children, this merge will
212 * be turned into a write operation, overwriting whatever was there before.
214 * If this is a container with keyed children, there are two possibilities:
215 * - if it existed before, this value will never be consulted and the children
216 * will get explicitly merged onto the original data.
217 * - if it did not exist before, this value will be used as a seed write and
218 * children will be merged into it.
219 * In either case we rely on OperationWithModification to manipulate the children
220 * before calling this method, so unlike a write we do not want to clear them.
226 * Seal the modification node and prune any children which has not been
232 // Walk all child nodes and remove any children which have not
234 final Iterator<ModifiedNode> it = children.values().iterator();
235 while (it.hasNext()) {
236 final ModifiedNode child = it.next();
239 if (child.operation == LogicalOperation.NONE) {
244 // A TOUCH node without any children is a no-op
245 if (operation == LogicalOperation.TOUCH && children.isEmpty()) {
246 updateModificationType(LogicalOperation.NONE);
250 private void clearSnapshot() {
251 snapshotCache = null;
254 Optional<TreeNode> getSnapshot() {
255 return snapshotCache;
258 Optional<TreeNode> setSnapshot(final Optional<TreeNode> snapshot) {
259 snapshotCache = Preconditions.checkNotNull(snapshot);
263 private void updateModificationType(final LogicalOperation type) {
269 public String toString() {
270 return "NodeModification [identifier=" + identifier + ", modificationType="
271 + operation + ", childModification=" + children + "]";
275 * Create a node which will reflect the state of this node, except it will behave as newly-written
276 * value. This is useful only for merge validation.
278 * @param value Value associated with the node
279 * @return An isolated node. This node should never reach a datatree.
281 ModifiedNode asNewlyWritten(final NormalizedNode<?, ?> value) {
282 final ModifiedNode ret = new ModifiedNode(getIdentifier(), Optional.<TreeNode>absent(), false);
287 public static ModifiedNode createUnmodified(final TreeNode metadataTree, final boolean isOrdered) {
288 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), isOrdered);