f83ea1a2de36f3b326bf3dc1b7404a2e80348def
[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.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 final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<PathArgument>, NodeModification {
35
36     public static final Predicate<ModifiedNode> IS_TERMINAL_PREDICATE = new Predicate<ModifiedNode>() {
37         @Override
38         public boolean apply(final ModifiedNode input) {
39             switch (input.getType()) {
40             case DELETE:
41             case MERGE:
42             case WRITE:
43                 return true;
44             case SUBTREE_MODIFIED:
45             case UNMODIFIED:
46                 return false;
47             }
48
49             throw new IllegalArgumentException(String.format("Unhandled modification type %s", input.getType()));
50         }
51     };
52
53     private final Map<PathArgument, ModifiedNode> children = new LinkedHashMap<>();
54     private final Optional<TreeNode> original;
55     private final PathArgument identifier;
56     private ModificationType modificationType = ModificationType.UNMODIFIED;
57     private Optional<TreeNode> snapshotCache;
58     private NormalizedNode<?, ?> value;
59
60     private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original) {
61         this.identifier = identifier;
62         this.original = original;
63     }
64
65     /**
66      *
67      *
68      * @return
69      */
70     public NormalizedNode<?, ?> getWrittenValue() {
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     @Override
85     public Optional<TreeNode> getOriginal() {
86         return original;
87     }
88
89     /**
90      * Returns modification type
91      *
92      * @return modification type
93      */
94     @Override
95     public ModificationType getType() {
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<ModifiedNode> getChild(final PathArgument child) {
109         return Optional.<ModifiedNode> fromNullable(children.get(child));
110     }
111
112     /**
113      *
114      * Returns child modification if child was modified, creates {@link ModifiedNode}
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 ModifiedNode} for specified child, with {@link #getOriginal()}
122      *         containing child metadata if child was present in original data.
123      */
124     public ModifiedNode modifyChild(final PathArgument child) {
125         clearSnapshot();
126         if (modificationType == ModificationType.UNMODIFIED) {
127             updateModificationType(ModificationType.SUBTREE_MODIFIED);
128         }
129         final ModifiedNode potential = children.get(child);
130         if (potential != null) {
131             return potential;
132         }
133
134         final Optional<TreeNode> currentMetadata;
135         if (original.isPresent()) {
136             final TreeNode orig = original.get();
137             currentMetadata = orig.getChild(child);
138         } else {
139             currentMetadata = Optional.absent();
140         }
141
142         ModifiedNode newlyCreated = new ModifiedNode(child, currentMetadata);
143         children.put(child, newlyCreated);
144         return newlyCreated;
145     }
146
147     /**
148      *
149      * Returns all recorded direct child modification
150      *
151      * @return all recorded direct child modifications
152      */
153     @Override
154     public Iterable<ModifiedNode> getChildren() {
155         return children.values();
156     }
157
158     /**
159      *
160      * Records a delete for associated node.
161      *
162      */
163     public void delete() {
164         clearSnapshot();
165         updateModificationType(ModificationType.DELETE);
166         children.clear();
167         this.value = null;
168     }
169
170     /**
171      *
172      * Records a write for associated node.
173      *
174      * @param value
175      */
176     public void write(final NormalizedNode<?, ?> value) {
177         clearSnapshot();
178         updateModificationType(ModificationType.WRITE);
179         children.clear();
180         this.value = value;
181     }
182
183     public void merge(final NormalizedNode<?, ?> data) {
184         clearSnapshot();
185         updateModificationType(ModificationType.MERGE);
186         // FIXME: Probably merge with previous value.
187         this.value = data;
188     }
189
190     void seal() {
191         clearSnapshot();
192         for (ModifiedNode child : children.values()) {
193             child.seal();
194         }
195     }
196
197     private void clearSnapshot() {
198         snapshotCache = null;
199     }
200
201     public Optional<TreeNode> storeSnapshot(final Optional<TreeNode> snapshot) {
202         snapshotCache = snapshot;
203         return snapshot;
204     }
205
206     public Optional<Optional<TreeNode>> getSnapshotCache() {
207         return Optional.fromNullable(snapshotCache);
208     }
209
210     @GuardedBy("this")
211     private void updateModificationType(final ModificationType type) {
212         modificationType = type;
213         clearSnapshot();
214     }
215
216     @Override
217     public String toString() {
218         return "NodeModification [identifier=" + identifier + ", modificationType="
219                 + modificationType + ", childModification=" + children + "]";
220     }
221
222     public static ModifiedNode createUnmodified(final TreeNode metadataTree) {
223         return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree));
224     }
225 }