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 org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Predicate;
21 import com.google.common.primitives.UnsignedLong;
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()}.
33 public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
35 public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
37 public boolean apply(final NodeModification input) {
38 return input.getModificationType() == ModificationType.WRITE || input.getModificationType() == ModificationType.DELETE;
41 private final PathArgument identifier;
42 private ModificationType modificationType = ModificationType.UNMODIFIED;
45 private final Optional<StoreMetadataNode> original;
47 private NormalizedNode<?, ?> value;
49 private UnsignedLong subtreeVersion;
50 private Optional<StoreMetadataNode> snapshotCache;
52 private final Map<PathArgument, NodeModification> childModification;
54 private boolean sealed = false;
56 protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
57 this.identifier = identifier;
58 this.original = original;
59 childModification = new LinkedHashMap<>();
67 public NormalizedNode<?, ?> getWritenValue() {
72 public PathArgument getIdentifier() {
78 * Returns original store metadata
79 * @return original store metadata
81 public final Optional<StoreMetadataNode> getOriginal() {
86 * Returns modification type
88 * @return modification type
90 public final ModificationType getModificationType() {
91 return modificationType;
96 * Returns child modification if child was modified
98 * @return Child modification if direct child or it's subtree
103 public Optional<NodeModification> getChild(final PathArgument child) {
104 return Optional.<NodeModification> fromNullable(childModification.get(child));
109 * Returns child modification if child was modified, creates {@link NodeModification}
110 * for child otherwise.
112 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
113 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
116 * @return {@link NodeModification} for specified child, with {@link #getOriginal()}
117 * containing child metadata if child was present in original data.
119 public synchronized NodeModification modifyChild(final PathArgument child) {
122 if(modificationType == ModificationType.UNMODIFIED) {
123 updateModificationType(ModificationType.SUBTREE_MODIFIED);
125 final NodeModification potential = childModification.get(child);
126 if (potential != null) {
129 Optional<StoreMetadataNode> currentMetadata = Optional.absent();
130 if(original.isPresent()) {
131 currentMetadata = original.get().getChild(child);
133 NodeModification newlyCreated = new NodeModification(child,currentMetadata);
134 childModification.put(child, newlyCreated);
140 * Returns all recorded direct child modification
142 * @return all recorded direct child modifications
144 public Iterable<NodeModification> getModifications() {
145 return childModification.values();
151 * Records a delete for associated node.
154 public synchronized void delete() {
157 updateModificationType(ModificationType.DELETE);
158 childModification.clear();
164 * Records a write for associated node.
168 public synchronized void write(final NormalizedNode<?, ?> value) {
171 updateModificationType(ModificationType.WRITE);
172 childModification.clear();
176 private void checkSealed() {
177 checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
180 public synchronized void seal() {
183 for(NodeModification child : childModification.values()) {
188 private void clearSnapshot() {
189 snapshotCache = null;
192 public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
193 snapshotCache = snapshot;
197 public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
198 return Optional.fromNullable(snapshotCache);
201 public boolean hasAdditionalModifications() {
202 return !childModification.isEmpty();
205 public void updateModificationType(final ModificationType type) {
206 modificationType = type;
211 public String toString() {
212 return "NodeModification [identifier=" + identifier + ", modificationType="
213 + modificationType + ", childModification=" + childModification + "]";
216 public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
217 return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));