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