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;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.LinkedHashMap;
15 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Predicate;
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()}.
34 public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
36 public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
38 public boolean apply(final NodeModification input) {
39 return input.getModificationType() == ModificationType.WRITE //
40 || input.getModificationType() == ModificationType.DELETE //
41 || input.getModificationType() == ModificationType.MERGE;
44 private final PathArgument identifier;
45 private ModificationType modificationType = ModificationType.UNMODIFIED;
48 private final Optional<StoreMetadataNode> original;
50 private NormalizedNode<?, ?> value;
52 private Optional<StoreMetadataNode> snapshotCache;
54 private final Map<PathArgument, NodeModification> childModification;
57 private boolean sealed = false;
59 protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
60 this.identifier = identifier;
61 this.original = original;
62 childModification = new LinkedHashMap<>();
70 public NormalizedNode<?, ?> getWritenValue() {
75 public PathArgument getIdentifier() {
81 * Returns original store metadata
82 * @return original store metadata
84 public final Optional<StoreMetadataNode> getOriginal() {
89 * Returns modification type
91 * @return modification type
93 public final ModificationType getModificationType() {
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<NodeModification> getChild(final PathArgument child) {
107 return Optional.<NodeModification> fromNullable(childModification.get(child));
112 * Returns child modification if child was modified, creates {@link NodeModification}
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 NodeModification} for specified child, with {@link #getOriginal()}
120 * containing child metadata if child was present in original data.
122 public synchronized NodeModification modifyChild(final PathArgument child) {
125 if(modificationType == ModificationType.UNMODIFIED) {
126 updateModificationType(ModificationType.SUBTREE_MODIFIED);
128 final NodeModification potential = childModification.get(child);
129 if (potential != null) {
132 Optional<StoreMetadataNode> currentMetadata = Optional.absent();
133 if(original.isPresent()) {
134 currentMetadata = original.get().getChild(child);
136 NodeModification newlyCreated = new NodeModification(child,currentMetadata);
137 childModification.put(child, newlyCreated);
143 * Returns all recorded direct child modification
145 * @return all recorded direct child modifications
147 public Iterable<NodeModification> getModifications() {
148 return childModification.values();
154 * Records a delete for associated node.
157 public synchronized void delete() {
160 updateModificationType(ModificationType.DELETE);
161 childModification.clear();
167 * Records a write for associated node.
171 public synchronized void write(final NormalizedNode<?, ?> value) {
174 updateModificationType(ModificationType.WRITE);
175 childModification.clear();
179 public synchronized void merge(final NormalizedNode<?, ?> data) {
182 updateModificationType(ModificationType.MERGE);
183 // FIXME: Probably merge with previous value.
188 private void checkSealed() {
189 checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
192 public synchronized void seal() {
195 for(NodeModification child : childModification.values()) {
200 private void clearSnapshot() {
201 snapshotCache = null;
204 public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
205 snapshotCache = snapshot;
209 public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
210 return Optional.fromNullable(snapshotCache);
213 public boolean hasAdditionalModifications() {
214 return !childModification.isEmpty();
218 private void updateModificationType(final ModificationType type) {
219 modificationType = type;
224 public String toString() {
225 return "NodeModification [identifier=" + identifier + ", modificationType="
226 + modificationType + ", childModification=" + childModification + "]";
229 public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
230 return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));