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