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