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.Predicate;
12 import java.util.HashMap;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import javax.annotation.concurrent.GuardedBy;
21 import java.util.LinkedHashMap;
25 * Node Modification Node and Tree
27 * Tree which structurally resembles data tree and captures client modifications
28 * to the data store tree.
30 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
31 * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
33 final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<PathArgument>, NodeModification {
35 public static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
37 public boolean apply(final ModifiedNode input) {
38 switch (input.getType()) {
43 case SUBTREE_MODIFIED:
48 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
52 private final Map<PathArgument, ModifiedNode> children;
53 private final Optional<TreeNode> original;
54 private final PathArgument identifier;
55 private ModificationType modificationType = ModificationType.UNMODIFIED;
56 private Optional<TreeNode> snapshotCache;
57 private NormalizedNode<?, ?> value;
59 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, boolean isOrdered) {
60 this.identifier = identifier;
61 this.original = original;
64 children = new LinkedHashMap<>();
66 children = new HashMap<>();
75 public NormalizedNode<?, ?> getWrittenValue() {
80 public PathArgument getIdentifier() {
86 * Returns original store metadata
87 * @return original store metadata
90 public Optional<TreeNode> getOriginal() {
95 * Returns modification type
97 * @return modification type
100 public ModificationType getType() {
101 return modificationType;
106 * Returns child modification if child was modified
108 * @return Child modification if direct child or it's subtree
113 public Optional<ModifiedNode> getChild(final PathArgument child) {
114 return Optional.<ModifiedNode> fromNullable(children.get(child));
119 * Returns child modification if child was modified, creates {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode}
120 * for child otherwise.
122 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
123 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
126 * @return {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode} for specified child, with {@link #getOriginal()}
127 * containing child metadata if child was present in original data.
129 public ModifiedNode modifyChild(final PathArgument child, boolean isOrdered) {
131 if (modificationType == ModificationType.UNMODIFIED) {
132 updateModificationType(ModificationType.SUBTREE_MODIFIED);
134 final ModifiedNode potential = children.get(child);
135 if (potential != null) {
139 final Optional<TreeNode> currentMetadata;
140 if (original.isPresent()) {
141 final TreeNode orig = original.get();
142 currentMetadata = orig.getChild(child);
144 currentMetadata = Optional.absent();
147 ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata, isOrdered);
148 children.put(child, newlyCreated);
154 * Returns all recorded direct child modification
156 * @return all recorded direct child modifications
159 public Iterable<ModifiedNode> getChildren() {
160 return children.values();
165 * Records a delete for associated node.
168 public void delete() {
170 updateModificationType(ModificationType.DELETE);
177 * Records a write for associated node.
181 public void write(final NormalizedNode<?, ?> value) {
183 updateModificationType(ModificationType.WRITE);
188 public void merge(final NormalizedNode<?, ?> data) {
190 updateModificationType(ModificationType.MERGE);
191 // FIXME: Probably merge with previous value.
197 for (ModifiedNode child : children.values()) {
202 private void clearSnapshot() {
203 snapshotCache = null;
206 public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
207 snapshotCache = snapshot;
211 public Optional<Optional<TreeNode>> getSnapshotCache() {
212 return Optional.fromNullable(snapshotCache);
216 private void updateModificationType(final ModificationType type) {
217 modificationType = type;
222 public String toString() {
223 return "NodeModification [identifier=" + identifier + ", modificationType="
224 + modificationType + ", childModification=" + children + "]";
227 public static ModifiedNode createUnmodified(final TreeNode metadataTree, boolean isOrdered) {
228 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), isOrdered);