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 org.opendaylight.yangtools.concepts.Identifiable;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import javax.annotation.concurrent.GuardedBy;
20 import java.util.LinkedHashMap;
24 * Node Modification Node and Tree
26 * Tree which structurally resembles data tree and captures client modifications
27 * to the data store tree.
29 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
30 * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
32 final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<PathArgument>, NodeModification {
34 public static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
36 public boolean apply(final ModifiedNode input) {
37 switch (input.getType()) {
42 case SUBTREE_MODIFIED:
47 throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
51 private final Map<PathArgument, ModifiedNode> children = new LinkedHashMap<>();
52 private final Optional<TreeNode> original;
53 private final PathArgument identifier;
54 private ModificationType modificationType = ModificationType.UNMODIFIED;
55 private Optional<TreeNode> snapshotCache;
56 private NormalizedNode<?, ?> value;
58 private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original) {
59 this.identifier = identifier;
60 this.original = original;
68 public NormalizedNode<?, ?> getWrittenValue() {
73 public PathArgument getIdentifier() {
79 * Returns original store metadata
80 * @return original store metadata
83 public Optional<TreeNode> getOriginal() {
88 * Returns modification type
90 * @return modification type
93 public ModificationType getType() {
94 return modificationType;
99 * Returns child modification if child was modified
101 * @return Child modification if direct child or it's subtree
106 public Optional<ModifiedNode> getChild(final PathArgument child) {
107 return Optional.<ModifiedNode> fromNullable(children.get(child));
112 * Returns child modification if child was modified, creates {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode}
113 * for child otherwise.
115 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
116 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
119 * @return {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ModifiedNode} for specified child, with {@link #getOriginal()}
120 * containing child metadata if child was present in original data.
122 public ModifiedNode modifyChild(final PathArgument child) {
124 if (modificationType == ModificationType.UNMODIFIED) {
125 updateModificationType(ModificationType.SUBTREE_MODIFIED);
127 final ModifiedNode potential = children.get(child);
128 if (potential != null) {
132 final Optional<TreeNode> currentMetadata;
133 if (original.isPresent()) {
134 final TreeNode orig = original.get();
135 currentMetadata = orig.getChild(child);
137 currentMetadata = Optional.absent();
140 ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata);
141 children.put(child, newlyCreated);
147 * Returns all recorded direct child modification
149 * @return all recorded direct child modifications
152 public Iterable<ModifiedNode> getChildren() {
153 return children.values();
158 * Records a delete for associated node.
161 public void delete() {
163 updateModificationType(ModificationType.DELETE);
170 * Records a write for associated node.
174 public void write(final NormalizedNode<?, ?> value) {
176 updateModificationType(ModificationType.WRITE);
181 public void merge(final NormalizedNode<?, ?> data) {
183 updateModificationType(ModificationType.MERGE);
184 // FIXME: Probably merge with previous value.
190 for (ModifiedNode child : children.values()) {
195 private void clearSnapshot() {
196 snapshotCache = null;
199 public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
200 snapshotCache = snapshot;
204 public Optional<Optional<TreeNode>> getSnapshotCache() {
205 return Optional.fromNullable(snapshotCache);
209 private void updateModificationType(final ModificationType type) {
210 modificationType = type;
215 public String toString() {
216 return "NodeModification [identifier=" + identifier + ", modificationType="
217 + modificationType + ", childModification=" + children + "]";
220 public static ModifiedNode createUnmodified(final TreeNode metadataTree) {
221 return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree));