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.ModificationType;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreTreeNode;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import com.google.common.base.Optional;
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 NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
37 public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
39 public boolean apply(final NodeModification input) {
40 return input.getModificationType() == ModificationType.WRITE //
41 || input.getModificationType() == ModificationType.DELETE //
42 || input.getModificationType() == ModificationType.MERGE;
45 private final PathArgument identifier;
46 private ModificationType modificationType = ModificationType.UNMODIFIED;
49 private final Optional<StoreMetadataNode> original;
51 private NormalizedNode<?, ?> value;
53 private Optional<StoreMetadataNode> snapshotCache;
55 private final Map<PathArgument, NodeModification> childModification;
58 private boolean sealed = false;
60 protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
61 this.identifier = identifier;
62 this.original = original;
63 childModification = new LinkedHashMap<>();
71 public NormalizedNode<?, ?> getWrittenValue() {
76 public PathArgument getIdentifier() {
82 * Returns original store metadata
83 * @return original store metadata
85 public final Optional<StoreMetadataNode> getOriginal() {
90 * Returns modification type
92 * @return modification type
94 public final ModificationType getModificationType() {
95 return modificationType;
100 * Returns child modification if child was modified
102 * @return Child modification if direct child or it's subtree
107 public Optional<NodeModification> getChild(final PathArgument child) {
108 return Optional.<NodeModification> fromNullable(childModification.get(child));
113 * Returns child modification if child was modified, creates {@link NodeModification}
114 * for child otherwise.
116 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
117 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
120 * @return {@link NodeModification} for specified child, with {@link #getOriginal()}
121 * containing child metadata if child was present in original data.
123 public synchronized NodeModification modifyChild(final PathArgument child) {
126 if(modificationType == ModificationType.UNMODIFIED) {
127 updateModificationType(ModificationType.SUBTREE_MODIFIED);
129 final NodeModification potential = childModification.get(child);
130 if (potential != null) {
133 Optional<StoreMetadataNode> currentMetadata = Optional.absent();
134 if(original.isPresent()) {
135 currentMetadata = original.get().getChild(child);
137 NodeModification newlyCreated = new NodeModification(child,currentMetadata);
138 childModification.put(child, newlyCreated);
144 * Returns all recorded direct child modification
146 * @return all recorded direct child modifications
148 public Iterable<NodeModification> getModifications() {
149 return childModification.values();
155 * Records a delete for associated node.
158 public synchronized void delete() {
161 updateModificationType(ModificationType.DELETE);
162 childModification.clear();
168 * Records a write for associated node.
172 public synchronized void write(final NormalizedNode<?, ?> value) {
175 updateModificationType(ModificationType.WRITE);
176 childModification.clear();
180 public synchronized void merge(final NormalizedNode<?, ?> data) {
183 updateModificationType(ModificationType.MERGE);
184 // FIXME: Probably merge with previous value.
189 private void checkSealed() {
190 checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
193 public synchronized void seal() {
196 for(NodeModification child : childModification.values()) {
201 private void clearSnapshot() {
202 snapshotCache = null;
205 public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
206 snapshotCache = snapshot;
210 public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
211 return Optional.fromNullable(snapshotCache);
214 public boolean hasAdditionalModifications() {
215 return !childModification.isEmpty();
219 private void updateModificationType(final ModificationType type) {
220 modificationType = type;
225 public String toString() {
226 return "NodeModification [identifier=" + identifier + ", modificationType="
227 + modificationType + ", childModification=" + childModification + "]";
230 public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
231 return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));