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