04fb3b7c6c50499ad0b96b220c2685b597bdcb9d
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / NodeModification.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
15 import javax.annotation.concurrent.GuardedBy;
16
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;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Predicate;
23 import com.google.common.primitives.UnsignedLong;
24
25 /**
26  * Node Modification Node and Tree
27  *
28  * Tree which structurally resembles data tree and captures client modifications
29  * to the data store tree.
30  *
31  * This tree is lazily created and populated via {@link #modifyChild(PathArgument)}
32  * and {@link StoreMetadataNode} which represents original state {@link #getOriginal()}.
33  *
34  */
35 public class NodeModification implements StoreTreeNode<NodeModification>, Identifiable<PathArgument> {
36
37     public static final Predicate<NodeModification> IS_TERMINAL_PREDICATE = new Predicate<NodeModification>() {
38         @Override
39         public boolean apply(final NodeModification input) {
40             return input.getModificationType() == ModificationType.WRITE || input.getModificationType() == ModificationType.DELETE;
41         }
42     };
43     private final PathArgument identifier;
44     private ModificationType modificationType = ModificationType.UNMODIFIED;
45
46
47     private final Optional<StoreMetadataNode> original;
48
49     private NormalizedNode<?, ?> value;
50
51     private UnsignedLong subtreeVersion;
52     private Optional<StoreMetadataNode> snapshotCache;
53
54     private final Map<PathArgument, NodeModification> childModification;
55
56     @GuardedBy("this")
57     private boolean sealed = false;
58
59     protected NodeModification(final PathArgument identifier, final Optional<StoreMetadataNode> original) {
60         this.identifier = identifier;
61         this.original = original;
62         childModification = new LinkedHashMap<>();
63     }
64
65     /**
66      *
67      *
68      * @return
69      */
70     public NormalizedNode<?, ?> getWritenValue() {
71         return value;
72     }
73
74     @Override
75     public PathArgument getIdentifier() {
76         return identifier;
77     }
78
79     /**
80      *
81      * Returns original store metadata
82      * @return original store metadata
83      */
84     public final Optional<StoreMetadataNode> getOriginal() {
85         return original;
86     }
87
88     /**
89      * Returns modification type
90      *
91      * @return modification type
92      */
93     public final ModificationType getModificationType() {
94         return modificationType;
95     }
96
97     /**
98      *
99      * Returns child modification if child was modified
100      *
101      * @return Child modification if direct child or it's subtree
102      *  was modified.
103      *
104      */
105     @Override
106     public Optional<NodeModification> getChild(final PathArgument child) {
107         return Optional.<NodeModification> fromNullable(childModification.get(child));
108     }
109
110     /**
111      *
112      * Returns child modification if child was modified, creates {@link NodeModification}
113      * for child otherwise.
114      *
115      * If this node's {@link ModificationType} is {@link ModificationType#UNMODIFIED}
116      * changes modification type to {@link ModificationType#SUBTREE_MODIFIED}
117      *
118      * @param child
119      * @return {@link NodeModification} for specified child, with {@link #getOriginal()}
120      *  containing child metadata if child was present in original data.
121      */
122     public synchronized NodeModification modifyChild(final PathArgument child) {
123         checkSealed();
124         clearSnapshot();
125         if(modificationType == ModificationType.UNMODIFIED) {
126             updateModificationType(ModificationType.SUBTREE_MODIFIED);
127         }
128         final NodeModification potential = childModification.get(child);
129         if (potential != null) {
130             return potential;
131         }
132         Optional<StoreMetadataNode> currentMetadata = Optional.absent();
133         if(original.isPresent()) {
134             currentMetadata = original.get().getChild(child);
135         }
136         NodeModification newlyCreated = new NodeModification(child,currentMetadata);
137         childModification.put(child, newlyCreated);
138         return newlyCreated;
139     }
140
141     /**
142      *
143      * Returns all recorded direct child modification
144      *
145      * @return all recorded direct child modifications
146      */
147     public Iterable<NodeModification> getModifications() {
148         return childModification.values();
149     }
150
151
152     /**
153      *
154      * Records a delete for associated node.
155      *
156      */
157     public synchronized void delete() {
158         checkSealed();
159         clearSnapshot();
160         updateModificationType(ModificationType.DELETE);
161         childModification.clear();
162         this.value = null;
163     }
164
165     /**
166      *
167      * Records a write for associated node.
168      *
169      * @param value
170      */
171     public synchronized void write(final NormalizedNode<?, ?> value) {
172         checkSealed();
173         clearSnapshot();
174         updateModificationType(ModificationType.WRITE);
175         childModification.clear();
176         this.value = value;
177     }
178
179     @GuardedBy("this")
180     private void checkSealed() {
181         checkState(!sealed, "Node Modification is sealed. No further changes allowed.");
182     }
183
184     public synchronized void seal() {
185         sealed = true;
186         clearSnapshot();
187         for(NodeModification child : childModification.values()) {
188             child.seal();
189         }
190     }
191
192     private void clearSnapshot() {
193         snapshotCache = null;
194     }
195
196     public Optional<StoreMetadataNode> storeSnapshot(final Optional<StoreMetadataNode> snapshot) {
197         snapshotCache = snapshot;
198         return snapshot;
199     }
200
201     public Optional<Optional<StoreMetadataNode>> getSnapshotCache() {
202         return Optional.fromNullable(snapshotCache);
203     }
204
205     public boolean hasAdditionalModifications() {
206         return !childModification.isEmpty();
207     }
208
209     @GuardedBy("this")
210     private void updateModificationType(final ModificationType type) {
211         modificationType = type;
212         clearSnapshot();
213     }
214
215     @Override
216     public String toString() {
217         return "NodeModification [identifier=" + identifier + ", modificationType="
218                 + modificationType + ", childModification=" + childModification + "]";
219     }
220
221     public static NodeModification createUnmodified(final StoreMetadataNode metadataTree) {
222         return new NodeModification(metadataTree.getIdentifier(), Optional.of(metadataTree));
223     }
224
225 }