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