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.controller.md.sal.dom.store.impl.tree.data;
10 import java.util.LinkedHashMap;
13 import javax.annotation.concurrent.GuardedBy;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreTreeNode;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Preconditions;
24 import com.google.common.base.Predicate;
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 ModifiedNode input) {
40 switch (input.getType()) {
45 case SUBTREE_MODIFIED:
50 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
53 private final PathArgument identifier;
54 private ModificationType modificationType = ModificationType.UNMODIFIED;
57 private final Optional<TreeNode> original;
59 private NormalizedNode<?, ?> value;
61 private Optional<TreeNode> snapshotCache;
63 private final Map<PathArgument, ModifiedNode> childModification;
66 private boolean sealed = false;
68 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original) {
69 this.identifier = identifier;
70 this.original = original;
71 childModification = new LinkedHashMap<>();
79 public NormalizedNode<?, ?> getWrittenValue() {
84 public PathArgument getIdentifier() {
90 * Returns original store metadata
91 * @return original store metadata
94 public final Optional<TreeNode> getOriginal() {
99 * Returns modification type
101 * @return modification type
104 public final ModificationType getType() {
105 return modificationType;
110 * Returns child modification if child was modified
112 * @return Child modification if direct child or it's subtree
117 public Optional<ModifiedNode> getChild(final PathArgument child) {
118 return Optional.<ModifiedNode> fromNullable(childModification.get(child));
123 * Returns child modification if child was modified, creates {@link ModifiedNode}
124 * for child otherwise.
126 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
127 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
130 * @return {@link ModifiedNode} for specified child, with {@link #getOriginal()}
131 * containing child metadata if child was present in original data.
133 public synchronized ModifiedNode modifyChild(final PathArgument child) {
136 if (modificationType == ModificationType.UNMODIFIED) {
137 updateModificationType(ModificationType.SUBTREE_MODIFIED);
139 final ModifiedNode potential = childModification.get(child);
140 if (potential != null) {
144 final Optional<TreeNode> currentMetadata;
145 if (original.isPresent()) {
146 final TreeNode orig = original.get();
147 currentMetadata = orig.getChild(child);
149 currentMetadata = Optional.absent();
152 ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata);
153 childModification.put(child, newlyCreated);
159 * Returns all recorded direct child modification
161 * @return all recorded direct child modifications
164 public Iterable<ModifiedNode> getChildren() {
165 return childModification.values();
170 * Records a delete for associated node.
173 public synchronized void delete() {
176 updateModificationType(ModificationType.DELETE);
177 childModification.clear();
183 * Records a write for associated node.
187 public synchronized void write(final NormalizedNode<?, ?> value) {
190 updateModificationType(ModificationType.WRITE);
191 childModification.clear();
195 public synchronized void merge(final NormalizedNode<?, ?> data) {
198 updateModificationType(ModificationType.MERGE);
199 // FIXME: Probably merge with previous value.
204 private void checkSealed() {
205 Preconditions.checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
208 public synchronized void seal() {
211 for(ModifiedNode child : childModification.values()) {
216 private void clearSnapshot() {
217 snapshotCache = null;
220 public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
221 snapshotCache = snapshot;
225 public Optional<Optional<TreeNode>> getSnapshotCache() {
226 return Optional.fromNullable(snapshotCache);
230 private void updateModificationType(final ModificationType type) {
231 modificationType = type;
236 public String toString() {
237 return "NodeModification [identifier=" + identifier + ", modificationType="
238 + modificationType + ", childModification=" + childModification + "]";
241 public static ModifiedNode createUnmodified(final TreeNode metadataTree) {
242 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree));