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.Predicate;
26 * Node Modification Node and Tree
28 * Tree which structurally resembles data tree and captures client modifications
29 * to the data store tree.
31 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
32 * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
34 final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<PathArgument>, NodeModification {
36 public static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
38 public boolean apply(final ModifiedNode input) {
39 switch (input.getType()) {
44 case SUBTREE_MODIFIED:
49 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
53 private final Map<PathArgument, ModifiedNode> children = new LinkedHashMap<>();
54 private final Optional<TreeNode> original;
55 private final PathArgument identifier;
56 private ModificationType modificationType = ModificationType.UNMODIFIED;
57 private Optional<TreeNode> snapshotCache;
58 private NormalizedNode<?, ?> value;
60 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original) {
61 this.identifier = identifier;
62 this.original = original;
70 public NormalizedNode<?, ?> getWrittenValue() {
75 public PathArgument getIdentifier() {
81 * Returns original store metadata
82 * @return original store metadata
85 public Optional<TreeNode> getOriginal() {
90 * Returns modification type
92 * @return modification type
95 public ModificationType getType() {
96 return modificationType;
101 * Returns child modification if child was modified
103 * @return Child modification if direct child or it's subtree
108 public Optional<ModifiedNode> getChild(final PathArgument child) {
109 return Optional.<ModifiedNode> fromNullable(children.get(child));
114 * Returns child modification if child was modified, creates {@link ModifiedNode}
115 * for child otherwise.
117 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
118 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
121 * @return {@link ModifiedNode} for specified child, with {@link #getOriginal()}
122 * containing child metadata if child was present in original data.
124 public ModifiedNode modifyChild(final PathArgument child) {
126 if (modificationType == ModificationType.UNMODIFIED) {
127 updateModificationType(ModificationType.SUBTREE_MODIFIED);
129 final ModifiedNode potential = children.get(child);
130 if (potential != null) {
134 final Optional<TreeNode> currentMetadata;
135 if (original.isPresent()) {
136 final TreeNode orig = original.get();
137 currentMetadata = orig.getChild(child);
139 currentMetadata = Optional.absent();
142 ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata);
143 children.put(child, newlyCreated);
149 * Returns all recorded direct child modification
151 * @return all recorded direct child modifications
154 public Iterable<ModifiedNode> getChildren() {
155 return children.values();
160 * Records a delete for associated node.
163 public void delete() {
165 updateModificationType(ModificationType.DELETE);
172 * Records a write for associated node.
176 public void write(final NormalizedNode<?, ?> value) {
178 updateModificationType(ModificationType.WRITE);
183 public void merge(final NormalizedNode<?, ?> data) {
185 updateModificationType(ModificationType.MERGE);
186 // FIXME: Probably merge with previous value.
192 for (ModifiedNode child : children.values()) {
197 private void clearSnapshot() {
198 snapshotCache = null;
201 public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
202 snapshotCache = snapshot;
206 public Optional<Optional<TreeNode>> getSnapshotCache() {
207 return Optional.fromNullable(snapshotCache);
211 private void updateModificationType(final ModificationType type) {
212 modificationType = type;
217 public String toString() {
218 return "NodeModification [identifier=" + identifier + ", modificationType="
219 + modificationType + ", childModification=" + children + "]";
222 public static ModifiedNode createUnmodified(final TreeNode metadataTree) {
223 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree));