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.HashMap;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 import javax.annotation.concurrent.GuardedBy;
23 import java.util.LinkedHashMap;
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 StoreMetadataNode} which represents original state {@link #getOriginal()}.
35 final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<PathArgument>, NodeModification {
37 public 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.getType()) {
46 case SUBTREE_MODIFIED:
51 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
55 private final Map<PathArgument, ModifiedNode> children;
56 private final Optional<TreeNode> original;
57 private final PathArgument identifier;
58 private ModificationType modificationType = ModificationType.UNMODIFIED;
59 private Optional<TreeNode> snapshotCache;
60 private NormalizedNode<?, ?> value;
62 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, boolean isOrdered) {
63 this.identifier = identifier;
64 this.original = original;
67 children = new LinkedHashMap<>();
69 children = new HashMap<>();
78 public NormalizedNode<?, ?> getWrittenValue() {
83 public PathArgument getIdentifier() {
89 * Returns original store metadata
90 * @return original store metadata
93 public Optional<TreeNode> getOriginal() {
98 * Returns modification type
100 * @return modification type
103 public ModificationType getType() {
104 return modificationType;
109 * Returns child modification if child was modified
111 * @return Child modification if direct child or it's subtree
116 public Optional<ModifiedNode> getChild(final PathArgument child) {
117 return Optional.<ModifiedNode> fromNullable(children.get(child));
122 * Returns child modification if child was modified, creates {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode}
123 * for child otherwise.
125 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
126 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
129 * @return {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode} for specified child, with {@link #getOriginal()}
130 * containing child metadata if child was present in original data.
132 public ModifiedNode modifyChild(final PathArgument child, boolean isOrdered) {
134 if (modificationType == ModificationType.UNMODIFIED) {
135 updateModificationType(ModificationType.SUBTREE_MODIFIED);
137 final ModifiedNode potential = children.get(child);
138 if (potential != null) {
142 final Optional<TreeNode> currentMetadata;
143 if (original.isPresent()) {
144 final TreeNode orig = original.get();
145 currentMetadata = orig.getChild(child);
147 currentMetadata = Optional.absent();
150 ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata, isOrdered);
151 children.put(child, newlyCreated);
157 * Returns all recorded direct child modification
159 * @return all recorded direct child modifications
162 public Iterable<ModifiedNode> getChildren() {
163 return children.values();
168 * Records a delete for associated node.
171 public void delete() {
172 final ModificationType newType;
174 switch (modificationType) {
177 // We need to record this delete.
178 newType = ModificationType.DELETE;
181 case SUBTREE_MODIFIED:
184 * We are canceling a previous modification. This is a bit tricky,
185 * as the original write may have just introduced the data, or it
186 * may have modified it.
188 * As documented in BUG-2470, a delete of data introduced in this
189 * transaction needs to be turned into a no-op.
191 newType = original.isPresent() ? ModificationType.DELETE : ModificationType.UNMODIFIED;
194 throw new IllegalStateException("Unhandled deletion of node with " + modificationType);
200 updateModificationType(newType);
205 * Records a write for associated node.
209 public void write(final NormalizedNode<?, ?> value) {
211 updateModificationType(ModificationType.WRITE);
216 public void merge(final NormalizedNode<?, ?> data) {
218 updateModificationType(ModificationType.MERGE);
219 // FIXME: Probably merge with previous value.
225 for (ModifiedNode child : children.values()) {
230 private void clearSnapshot() {
231 snapshotCache = null;
234 public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
235 snapshotCache = snapshot;
239 public Optional<Optional<TreeNode>> getSnapshotCache() {
240 return Optional.fromNullable(snapshotCache);
244 private void updateModificationType(final ModificationType type) {
245 modificationType = type;
250 public String toString() {
251 return "NodeModification [identifier=" + identifier + ", modificationType="
252 + modificationType + ", childModification=" + children + "]";
255 public static ModifiedNode createUnmodified(final TreeNode metadataTree, boolean isOrdered) {
256 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), isOrdered);