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;
22 * Node Modification Node and Tree
24 * Tree which structurally resembles data tree and captures client modifications
25 * to the data store tree.
27 * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
28 * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
31 public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
33 private final PathArgument identifier;
34 private ModificationType modificationType = ModificationType.UNMODIFIED;
37 private final Optional<StoreMetadataNode> original;
39 private NormalizedNode<?, ?> value;
41 private StoreMetadataNode snapshotCache;
43 private final Map<PathArgument, NodeModification> childModification;
45 private boolean sealed = false;
47 protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
48 this.identifier = identifier;
49 this.original = original;
50 childModification = new LinkedHashMap<>();
58 public NormalizedNode<?, ?> getWritenValue() {
63 public PathArgument getIdentifier() {
69 * Returns original store metadata
70 * @return original store metadata
72 public final Optional<StoreMetadataNode> getOriginal() {
77 * Returns modification type
79 * @return modification type
81 public final ModificationType getModificationType() {
82 return modificationType;
87 * Returns child modification if child was modified
89 * @return Child modification if direct child or it's subtree
94 public Optional<NodeModification> getChild(final PathArgument child) {
95 return Optional.<NodeModification> fromNullable(childModification.get(child));
100 * Returns child modification if child was modified, creates {@link NodeModification}
101 * for child otherwise.
103 * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
104 * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
107 * @return {@link NodeModification} for specified child, with {@link #getOriginal()}
108 * containing child metadata if child was present in original data.
110 public synchronized NodeModification modifyChild(final PathArgument child) {
112 if(modificationType == ModificationType.UNMODIFIED) {
113 updateModificationType(ModificationType.SUBTREE_MODIFIED);
115 final NodeModification potential = childModification.get(child);
116 if (potential != null) {
119 Optional<StoreMetadataNode> currentMetadata = Optional.absent();
120 if(original.isPresent()) {
121 currentMetadata = original.get().getChild(child);
123 NodeModification newlyCreated = new NodeModification(child,currentMetadata);
124 childModification.put(child, newlyCreated);
130 * Returns all recorded direct child modification
132 * @return all recorded direct child modifications
134 public Iterable<NodeModification> getModifications() {
135 return childModification.values();
141 * Records a delete for associated node.
144 public synchronized void delete() {
146 updateModificationType(ModificationType.DELETE);
147 childModification.clear();
153 * Records a write for associated node.
157 public synchronized void write(final NormalizedNode<?, ?> value) {
159 updateModificationType(ModificationType.WRITE);
160 childModification.clear();
164 private void checkSealed() {
165 checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
168 public synchronized void seal() {
170 for(NodeModification child : childModification.values()) {
175 private void clearSnapshot() {
176 snapshotCache = null;
179 public boolean hasAdditionalModifications() {
180 return !childModification.isEmpty();
183 public void updateModificationType(final ModificationType type) {
184 modificationType = type;
189 public String toString() {
190 return "NodeModification [identifier=" + identifier + ", modificationType="
191 + modificationType + ", value=" + value + ", childModification=" + childModification + "]";
194 public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
195 return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));