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 static com.google.common.base.Preconditions.checkState;
12 import java.util.LinkedHashMap;
15 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreTreeNode;
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()}.
35 // FIXME: hide this class
36 public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
38 public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
40 public boolean apply(final NodeModification input) {
41 return input.getModificationType() == ModificationType.WRITE //
42 || input.getModificationType() == ModificationType.DELETE //
43 || input.getModificationType() == ModificationType.MERGE;
46 private final PathArgument identifier;
47 private ModificationType modificationType = ModificationType.UNMODIFIED;
50 private final Optional<StoreMetadataNode> original;
52 private NormalizedNode<?, ?> value;
54 private Optional<StoreMetadataNode> snapshotCache;
56 private final Map<PathArgument, NodeModification> childModification;
59 private boolean sealed = false;
61 protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
62 this.identifier = identifier;
63 this.original = original;
64 childModification = new LinkedHashMap<>();
72 public NormalizedNode<?, ?> getWrittenValue() {
77 public PathArgument getIdentifier() {
83 * Returns original store metadata
84 * @return original store metadata
86 public final Optional<StoreMetadataNode> getOriginal() {
91 * Returns modification type
93 * @return modification type
95 public final ModificationType getModificationType() {
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<NodeModification> getChild(final PathArgument child) {
109 return Optional.<NodeModification> fromNullable(childModification.get(child));
114 * Returns child modification if child was modified, creates {@link NodeModification}
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 NodeModification} for specified child, with {@link #getOriginal()}
122 * containing child metadata if child was present in original data.
124 public synchronized NodeModification modifyChild(final PathArgument child) {
127 if(modificationType == ModificationType.UNMODIFIED) {
128 updateModificationType(ModificationType.SUBTREE_MODIFIED);
130 final NodeModification potential = childModification.get(child);
131 if (potential != null) {
134 Optional<StoreMetadataNode> currentMetadata = Optional.absent();
135 if(original.isPresent()) {
136 currentMetadata = original.get().getChild(child);
138 NodeModification newlyCreated = new NodeModification(child,currentMetadata);
139 childModification.put(child, newlyCreated);
145 * Returns all recorded direct child modification
147 * @return all recorded direct child modifications
149 public Iterable<NodeModification> getModifications() {
150 return childModification.values();
156 * Records a delete for associated node.
159 public synchronized void delete() {
162 updateModificationType(ModificationType.DELETE);
163 childModification.clear();
169 * Records a write for associated node.
173 public synchronized void write(final NormalizedNode<?, ?> value) {
176 updateModificationType(ModificationType.WRITE);
177 childModification.clear();
181 public synchronized void merge(final NormalizedNode<?, ?> data) {
184 updateModificationType(ModificationType.MERGE);
185 // FIXME: Probably merge with previous value.
190 private void checkSealed() {
191 checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
194 public synchronized void seal() {
197 for(NodeModification child : childModification.values()) {
202 private void clearSnapshot() {
203 snapshotCache = null;
206 public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
207 snapshotCache = snapshot;
211 public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
212 return Optional.fromNullable(snapshotCache);
215 public boolean hasAdditionalModifications() {
216 return !childModification.isEmpty();
220 private void updateModificationType(final ModificationType type) {
221 modificationType = type;
226 public String toString() {
227 return "NodeModification [identifier=" + identifier + ", modificationType="
228 + modificationType + ", childModification=" + childModification + "]";
231 public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
232 return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));