1d10ab6ea5a971dfc3e4a160949fd6f5c17fd30b
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / NormalizedNodeContainerModificationStrategy.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.checkArgument;
11
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataValidationFailedException;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.DataNodeContainerModificationStrategy.ListEntryModificationStrategy;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ValueNodeModificationStrategy.LeafSetEntryModificationStrategy;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.MutableTreeNode;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNodeFactory;
21 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
31 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
41 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
45
46 import com.google.common.base.Optional;
47 import com.google.common.collect.ImmutableMap;
48 import com.google.common.collect.Iterables;
49
50 abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
51
52     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
53
54     protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
55         this.nodeClass = nodeClass;
56     }
57
58     @Override
59     public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
60         if (modification.getType() == ModificationType.WRITE) {
61
62         }
63         for (ModifiedNode childModification : modification.getChildren()) {
64             resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
65         }
66     }
67
68     @Override
69     protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
70             final Optional<TreeNode> current) throws DataValidationFailedException {
71         // FIXME: Implement proper write check for replacement of node container
72         //        prerequisite is to have transaction chain available for clients
73         //        otherwise this will break chained writes to same node.
74     }
75
76     @SuppressWarnings("rawtypes")
77     @Override
78     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
79         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
80         checkArgument(writtenValue instanceof NormalizedNodeContainer);
81
82         NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
83         for (Object child : container.getValue()) {
84             checkArgument(child instanceof NormalizedNode);
85
86             /*
87              * FIXME: fail-fast semantics:
88              *
89              * We can validate the data structure here, aborting the commit
90              * before it ever progresses to being committed.
91              */
92         }
93     }
94
95     @Override
96     protected TreeNode applyWrite(final ModifiedNode modification,
97             final Optional<TreeNode> currentMeta, final Version version) {
98         final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
99         final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
100
101         if (Iterables.isEmpty(modification.getChildren())) {
102             return newValueMeta;
103         }
104
105         /*
106          * This is where things get interesting. The user has performed a write and
107          * then she applied some more modifications to it. So we need to make sense
108          * of that an apply the operations on top of the written value. We could have
109          * done it during the write, but this operation is potentially expensive, so
110          * we have left it out of the fast path.
111          *
112          * As it turns out, once we materialize the written data, we can share the
113          * code path with the subtree change. So let's create an unsealed TreeNode
114          * and run the common parts on it -- which end with the node being sealed.
115          */
116         final MutableTreeNode mutable = newValueMeta.mutable();
117         mutable.setSubtreeVersion(version);
118
119         @SuppressWarnings("rawtypes")
120         final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
121
122         return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
123     }
124
125     @SuppressWarnings({ "rawtypes", "unchecked" })
126     private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
127             final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
128
129         for (ModifiedNode mod : modifications) {
130             final PathArgument id = mod.getIdentifier();
131             final Optional<TreeNode> cm = meta.getChild(id);
132
133             Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
134             if (result.isPresent()) {
135                 final TreeNode tn = result.get();
136                 meta.addChild(tn);
137                 data.addChild(tn.getData());
138             } else {
139                 meta.removeChild(id);
140                 data.removeChild(id);
141             }
142         }
143
144         meta.setData(data.build());
145         return meta.seal();
146     }
147
148     @Override
149     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
150             final Version version) {
151         // For Node Containers - merge is same as subtree change - we only replace children.
152         return applySubtreeChange(modification, currentMeta, version);
153     }
154
155     @Override
156     public TreeNode applySubtreeChange(final ModifiedNode modification,
157             final TreeNode currentMeta, final Version version) {
158         final MutableTreeNode newMeta = currentMeta.mutable();
159         newMeta.setSubtreeVersion(version);
160
161         @SuppressWarnings("rawtypes")
162         NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
163
164         return mutateChildren(newMeta, dataBuilder, version, modification.getChildren());
165     }
166
167     @Override
168     protected void checkSubtreeModificationApplicable(final InstanceIdentifier path, final NodeModification modification,
169             final Optional<TreeNode> current) throws DataValidationFailedException {
170         checkConflicting(path, current.isPresent(), "Node was deleted by other transaction.");
171         checkChildPreconditions(path, modification, current);
172     }
173
174     private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
175         final TreeNode currentMeta = current.get();
176         for (NodeModification childMod : modification.getChildren()) {
177             final PathArgument childId = childMod.getIdentifier();
178             final Optional<TreeNode> childMeta = currentMeta.getChild(childId);
179
180             InstanceIdentifier childPath = path.node(childId);
181             resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
182         }
183     }
184
185     @Override
186     protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
187             final Optional<TreeNode> current) throws DataValidationFailedException {
188         if(current.isPresent()) {
189             checkChildPreconditions(path, modification,current);
190         }
191     }
192
193     @SuppressWarnings("rawtypes")
194     protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
195
196     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
197
198         private final Map<PathArgument, ModificationApplyOperation> childNodes;
199
200         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
201             super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
202             ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
203
204             for (ChoiceCaseNode caze : schemaNode.getCases()) {
205                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
206                     SchemaAwareApplyOperation childNode = from(cazeChild);
207                     child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
208                 }
209             }
210             childNodes = child.build();
211         }
212
213         @Override
214         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
215             return Optional.fromNullable(childNodes.get(child));
216         }
217
218         @Override
219         @SuppressWarnings("rawtypes")
220         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
221             checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
222             return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
223         }
224     }
225
226     public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
227
228         private final Optional<ModificationApplyOperation> entryStrategy;
229
230         @SuppressWarnings({ "unchecked", "rawtypes" })
231         protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
232             super((Class) LeafSetNode.class);
233             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
234         }
235
236         @SuppressWarnings("rawtypes")
237         @Override
238         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
239             checkArgument(original instanceof OrderedLeafSetNode<?>);
240             return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
241         }
242
243         @Override
244         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
245             if (identifier instanceof NodeWithValue) {
246                 return entryStrategy;
247             }
248             return Optional.absent();
249         }
250     }
251
252     public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
253
254         private final Optional<ModificationApplyOperation> entryStrategy;
255
256         protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
257             super(OrderedMapNode.class);
258             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
259         }
260
261         @SuppressWarnings("rawtypes")
262         @Override
263         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
264             checkArgument(original instanceof OrderedMapNode);
265             return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
266         }
267
268         @Override
269         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
270             if (identifier instanceof NodeIdentifierWithPredicates) {
271                 return entryStrategy;
272             }
273             return Optional.absent();
274         }
275
276         @Override
277         public String toString() {
278             return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
279         }
280     }
281
282     public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
283
284         private final Optional<ModificationApplyOperation> entryStrategy;
285
286         @SuppressWarnings({ "unchecked", "rawtypes" })
287         protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
288             super((Class) LeafSetNode.class);
289             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
290         }
291
292         @SuppressWarnings("rawtypes")
293         @Override
294         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
295             checkArgument(original instanceof LeafSetNode<?>);
296             return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
297         }
298
299         @Override
300         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
301             if (identifier instanceof NodeWithValue) {
302                 return entryStrategy;
303             }
304             return Optional.absent();
305         }
306     }
307
308     public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
309
310         private final Optional<ModificationApplyOperation> entryStrategy;
311
312         protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
313             super(MapNode.class);
314             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
315         }
316
317         @SuppressWarnings("rawtypes")
318         @Override
319         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
320             checkArgument(original instanceof MapNode);
321             return ImmutableMapNodeBuilder.create((MapNode) original);
322         }
323
324         @Override
325         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
326             if (identifier instanceof NodeIdentifierWithPredicates) {
327                 return entryStrategy;
328             }
329             return Optional.absent();
330         }
331
332         @Override
333         public String toString() {
334             return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
335         }
336     }
337 }