c3c027340cf7274d395fe3028bf2477ff7f70822
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / SchemaAwareApplyOperation.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.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataPreconditionFailedException;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
37 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
52 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
53 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
54 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
55 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
56 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
58 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
62
63 import com.google.common.base.Function;
64 import com.google.common.base.Optional;
65 import com.google.common.base.Preconditions;
66 import com.google.common.cache.CacheBuilder;
67 import com.google.common.cache.CacheLoader;
68 import com.google.common.cache.LoadingCache;
69 import com.google.common.collect.ImmutableMap;
70 import com.google.common.primitives.UnsignedLong;
71
72 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
73
74     public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
75         if (schemaNode instanceof ContainerSchemaNode) {
76             return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
77         } else if (schemaNode instanceof ListSchemaNode) {
78             return fromListSchemaNode((ListSchemaNode) schemaNode);
79         } else if (schemaNode instanceof ChoiceNode) {
80             return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
81         } else if (schemaNode instanceof LeafListSchemaNode) {
82             return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
83         } else if (schemaNode instanceof LeafSchemaNode) {
84             return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
85         }
86         throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
87     }
88
89     private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
90         List<QName> keyDefinition = schemaNode.getKeyDefinition();
91         if (keyDefinition == null || keyDefinition.isEmpty()) {
92             return new UnkeyedListModificationStrategy(schemaNode);
93         }
94         if (schemaNode.isUserOrdered()) {
95             return new OrderedMapModificationStrategy(schemaNode);
96         }
97
98         return new UnorderedMapModificationStrategy(schemaNode);
99     }
100
101     private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
102         if(schemaNode.isUserOrdered()) {
103             return new OrderedLeafSetModificationStrategy(schemaNode);
104         } else {
105             return new UnorderedLeafSetModificationStrategy(schemaNode);
106         }
107     }
108
109
110     public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
111             final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
112         AugmentationSchema augSchema = null;
113
114         allAugments:
115             for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
116                 for (DataSchemaNode child : potential.getChildNodes()) {
117                     if (identifier.getPossibleChildNames().contains(child.getQName())) {
118                         augSchema = potential;
119                         break allAugments;
120                     }
121                 }
122             }
123
124         if (augSchema != null) {
125             return new AugmentationModificationStrategy(augSchema, resolvedTree);
126         }
127         return null;
128     }
129
130     protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
131         Optional<ModificationApplyOperation> potential = getChild(child);
132         checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
133         return potential.get();
134     }
135
136     @Override
137     public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
138         if (modification.getModificationType() == ModificationType.WRITE) {
139             verifyWrittenStructure(modification.getWrittenValue());
140         }
141     }
142
143     protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
144
145     @Override
146     public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
147         switch (modification.getModificationType()) {
148         case DELETE:
149             checkDeleteApplicable(modification, current);
150         case SUBTREE_MODIFIED:
151             checkSubtreeModificationApplicable(path,modification, current);
152             return;
153         case WRITE:
154             checkWriteApplicable(path,modification, current);
155             return;
156         case MERGE:
157             checkMergeApplicable(path,modification,current);
158             return;
159         case UNMODIFIED:
160             return;
161         default:
162             throw new UnsupportedOperationException("Suplied modification type "+modification.getModificationType()+ "is not supported.");
163         }
164
165     }
166
167     protected void checkMergeApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
168         Optional<StoreMetadataNode> original = modification.getOriginal();
169         if (original.isPresent() && current.isPresent()) {
170             /*
171              * We need to do conflict detection only and only if the value of leaf changed
172              * before two transactions. If value of leaf is unchanged between two transactions
173              * it should not cause transaction to fail, since result of this merge
174              * leads to same data.
175              */
176             if(!original.get().getData().equals(current.get().getData())) {
177
178                 checkNotConflicting(path,original.get(), current.get());
179             }
180         }
181     }
182
183     protected void checkWriteApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
184         Optional<StoreMetadataNode> original = modification.getOriginal();
185         if (original.isPresent() && current.isPresent()) {
186             checkNotConflicting(path,original.get(), current.get());
187         } else if(original.isPresent()) {
188             throw new DataPreconditionFailedException(path,"Node was deleted by other transaction.");
189         }
190     }
191
192     protected static final void checkNotConflicting(final InstanceIdentifier path,final StoreMetadataNode original, final StoreMetadataNode current) throws DataPreconditionFailedException {
193         checkDataPrecondition(path, original.getNodeVersion().equals(current.getNodeVersion()),"Node was replaced by other transaction.");
194         checkDataPrecondition(path,original.getSubtreeVersion().equals(current.getSubtreeVersion()), "Node children was modified by other transaction");
195     }
196
197     protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path,final NodeModification modification,
198             final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException;
199
200     private void checkDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
201     }
202
203     @Override
204     public final Optional<StoreMetadataNode> apply(final NodeModification modification,
205             final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
206
207         switch (modification.getModificationType()) {
208         case DELETE:
209             return modification.storeSnapshot(Optional.<StoreMetadataNode> absent());
210         case SUBTREE_MODIFIED:
211             Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
212                     modification);
213             return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
214                     subtreeVersion)));
215         case MERGE:
216             if(currentMeta.isPresent()) {
217                 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(),subtreeVersion)));
218             } // Fallback to write is intentional - if node is not preexisting merge is same as write
219         case WRITE:
220             return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion)));
221         case UNMODIFIED:
222             return currentMeta;
223         default:
224             throw new IllegalArgumentException("Provided modification type is not supported.");
225         }
226     }
227
228     protected abstract StoreMetadataNode applyMerge(NodeModification modification,
229             StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
230
231     protected abstract StoreMetadataNode applyWrite(NodeModification modification,
232             Optional<StoreMetadataNode> currentMeta, UnsignedLong subtreeVersion);
233
234     protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification,
235             StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
236
237     public static abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
238
239         private final T schema;
240         private final Class<? extends NormalizedNode<?, ?>> nodeClass;
241
242         protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
243             super();
244             this.schema = schema;
245             this.nodeClass = nodeClass;
246         }
247
248         @Override
249         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
250             checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
251         }
252
253         @Override
254         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
255             throw new UnsupportedOperationException("Node " + schema.getPath()
256                     + "is leaf type node. Child nodes not allowed");
257         }
258
259         @Override
260         protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
261                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
262             throw new UnsupportedOperationException("Node " + schema.getPath()
263                     + "is leaf type node. Subtree change is not allowed.");
264         }
265
266         @Override
267         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
268                 final UnsignedLong subtreeVersion) {
269             return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
270         }
271
272         @Override
273         protected StoreMetadataNode applyWrite(final NodeModification modification,
274                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
275             return StoreMetadataNode.builder(subtreeVersion).setSubtreeVersion(subtreeVersion)
276                     .setData(modification.getWrittenValue()).build();
277         }
278
279         @Override
280         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
281                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
282             throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
283         }
284
285     }
286
287     public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
288
289         @SuppressWarnings({ "unchecked", "rawtypes" })
290         protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
291             super(schema, (Class) LeafSetEntryNode.class);
292         }
293     }
294
295     public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
296
297         @SuppressWarnings({ "unchecked", "rawtypes" })
298         protected LeafModificationStrategy(final LeafSchemaNode schema) {
299             super(schema, (Class) LeafNode.class);
300         }
301     }
302
303     public static abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
304
305         private final Class<? extends NormalizedNode<?, ?>> nodeClass;
306
307         protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
308             this.nodeClass = nodeClass;
309         }
310
311         @Override
312         public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
313             if (modification.getModificationType() == ModificationType.WRITE) {
314
315             }
316             for (NodeModification childModification : modification.getModifications()) {
317                 resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
318             }
319         }
320
321         @Override
322         protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
323                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
324             // FIXME: Implement proper write check for replacement of node container
325             //        prerequisite is to have transaction chain available for clients
326             //        otherwise this will break chained writes to same node.
327         }
328
329         @SuppressWarnings("rawtypes")
330         @Override
331         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
332             checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
333             checkArgument(writtenValue instanceof NormalizedNodeContainer);
334
335             NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
336             for (Object child : container.getValue()) {
337                 checkArgument(child instanceof NormalizedNode);
338
339                 /*
340                  * FIXME: fail-fast semantics:
341                  *
342                  * We can validate the data structure here, aborting the commit
343                  * before it ever progresses to being committed.
344                  */
345             }
346         }
347
348         @Override
349         protected StoreMetadataNode applyWrite(final NodeModification modification,
350                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
351
352             NormalizedNode<?, ?> newValue = modification.getWrittenValue();
353
354             final UnsignedLong nodeVersion;
355             if (currentMeta.isPresent()) {
356                 nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
357             } else {
358                 nodeVersion = subtreeVersion;
359             }
360
361             final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
362             if (!modification.hasAdditionalModifications()) {
363                 return newValueMeta;
364             }
365
366             @SuppressWarnings("rawtypes")
367             NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
368             StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(nodeVersion, dataBuilder) //
369                     .setSubtreeVersion(subtreeVersion);
370
371             return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
372         }
373
374         @Override
375         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
376                 final UnsignedLong subtreeVersion) {
377             // For Node Containers - merge is same as subtree change - we only replace children.
378             return applySubtreeChange(modification, currentMeta, subtreeVersion);
379         }
380
381         @Override
382         public StoreMetadataNode applySubtreeChange(final NodeModification modification,
383                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
384             // Bump subtree version to its new target
385             final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
386
387             @SuppressWarnings("rawtypes")
388             NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
389             StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(dataBuilder, currentMeta)
390                     .setIdentifier(modification.getIdentifier())
391                     .setSubtreeVersion(updatedSubtreeVersion);
392
393             return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
394         }
395
396         private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
397                 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
398
399             for (NodeModification mod : modifications) {
400                 final PathArgument id = mod.getIdentifier();
401                 final Optional<StoreMetadataNode> cm = meta.getChild(id);
402
403                 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
404                 if (result.isPresent()) {
405                     builder.add(result.get());
406                 } else {
407                     builder.remove(id);
408                 }
409             }
410
411             return builder.build();
412         }
413
414         @Override
415         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
416                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
417             checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
418             checkChildPreconditions(path,modification,current);
419
420         }
421
422         private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
423             StoreMetadataNode currentMeta = current.get();
424             for (NodeModification childMod : modification.getModifications()) {
425                 PathArgument childId = childMod.getIdentifier();
426                 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
427                 InstanceIdentifier childPath = StoreUtils.append(path, childId);
428                 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
429             }
430         }
431
432         @Override
433         protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
434                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
435             if(current.isPresent()) {
436                 checkChildPreconditions(path,modification,current);
437             }
438         }
439
440         @SuppressWarnings("rawtypes")
441         protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
442     }
443
444     public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
445
446         private final T schema;
447         private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
448                 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
449
450                     @Override
451                     public ModificationApplyOperation apply(final PathArgument identifier) {
452                         if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
453                             return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
454                         }
455
456                         DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
457                         if (child == null) {
458                             return null;
459                         }
460                         return from(child);
461                     }
462                 }));
463
464         protected DataNodeContainerModificationStrategy(final T schema,
465                 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
466             super(nodeClass);
467             this.schema = schema;
468         }
469
470         protected T getSchema() {
471             return schema;
472         }
473
474         @Override
475         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
476             try {
477                 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
478             } catch (ExecutionException e) {
479                 return Optional.absent();
480             }
481         }
482
483         @Override
484         @SuppressWarnings("rawtypes")
485         protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
486
487         @Override
488         public String toString() {
489             return getClass().getSimpleName() + " [" + schema + "]";
490         }
491
492     }
493
494     public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
495
496         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
497             super(schemaNode, ContainerNode.class);
498         }
499
500         @Override
501         @SuppressWarnings("rawtypes")
502         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
503             checkArgument(original instanceof ContainerNode);
504             return ImmutableContainerNodeBuilder.create((ContainerNode) original);
505         }
506     }
507
508     public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
509
510         public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
511             super(schemaNode, UnkeyedListEntryNode.class);
512         }
513
514         @Override
515         @SuppressWarnings("rawtypes")
516         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
517             checkArgument(original instanceof UnkeyedListEntryNode);
518             return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
519         }
520     }
521
522     public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
523
524         protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
525             super(createAugmentProxy(schema,resolved), AugmentationNode.class);
526         }
527
528         @Override
529         @SuppressWarnings("rawtypes")
530         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
531             checkArgument(original instanceof AugmentationNode);
532             return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
533         }
534     }
535
536     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
537
538         private final Map<PathArgument, ModificationApplyOperation> childNodes;
539
540         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
541             super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
542             ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
543
544             for (ChoiceCaseNode caze : schemaNode.getCases()) {
545                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
546                     SchemaAwareApplyOperation childNode = from(cazeChild);
547                     child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
548                 }
549             }
550             childNodes = child.build();
551         }
552
553         @Override
554         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
555             return Optional.fromNullable(childNodes.get(child));
556         }
557
558         @Override
559         @SuppressWarnings("rawtypes")
560         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
561             checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
562             return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
563         }
564     }
565
566     public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
567
568         protected ListEntryModificationStrategy(final ListSchemaNode schema) {
569             super(schema, MapEntryNode.class);
570         }
571
572         @Override
573         @SuppressWarnings("rawtypes")
574         protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
575             checkArgument(original instanceof MapEntryNode);
576             return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
577         }
578     }
579
580     public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
581
582         private final Optional<ModificationApplyOperation> entryStrategy;
583
584         @SuppressWarnings({ "unchecked", "rawtypes" })
585         protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
586             super((Class) LeafSetNode.class);
587             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
588         }
589
590         @SuppressWarnings("rawtypes")
591         @Override
592         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
593             checkArgument(original instanceof LeafSetNode<?>);
594             return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
595         }
596
597         @Override
598         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
599             if (identifier instanceof NodeWithValue) {
600                 return entryStrategy;
601             }
602             return Optional.absent();
603         }
604     }
605
606     public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
607
608         private final Optional<ModificationApplyOperation> entryStrategy;
609
610         @SuppressWarnings({ "unchecked", "rawtypes" })
611         protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
612             super((Class) LeafSetNode.class);
613             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
614         }
615
616         @SuppressWarnings("rawtypes")
617         @Override
618         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
619             checkArgument(original instanceof OrderedLeafSetNode<?>);
620             return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
621         }
622
623         @Override
624         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
625             if (identifier instanceof NodeWithValue) {
626                 return entryStrategy;
627             }
628             return Optional.absent();
629         }
630     }
631
632     public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
633
634         private final Optional<ModificationApplyOperation> entryStrategy;
635
636         protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
637             entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
638         }
639
640         @Override
641         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
642                 final UnsignedLong subtreeVersion) {
643             return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
644         }
645
646         @Override
647         protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
648                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
649             throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
650         }
651
652         @Override
653         protected StoreMetadataNode applyWrite(final NodeModification modification,
654                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
655             return StoreMetadataNode.createRecursively(modification.getWrittenValue(), subtreeVersion);
656         }
657
658         @Override
659         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
660             if (child instanceof NodeIdentifier) {
661                 return entryStrategy;
662             }
663             return Optional.absent();
664         }
665
666         @Override
667         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
668
669         }
670
671         @Override
672         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
673                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
674             throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
675         }
676
677     }
678
679     public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
680
681         private final Optional<ModificationApplyOperation> entryStrategy;
682
683         protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
684             super(MapNode.class);
685             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
686         }
687
688         @SuppressWarnings("rawtypes")
689         @Override
690         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
691             checkArgument(original instanceof MapNode);
692             return ImmutableMapNodeBuilder.create((MapNode) original);
693         }
694
695         @Override
696         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
697             if (identifier instanceof NodeIdentifierWithPredicates) {
698                 return entryStrategy;
699             }
700             return Optional.absent();
701         }
702
703         @Override
704         public String toString() {
705             return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
706         }
707     }
708
709     public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
710
711         private final Optional<ModificationApplyOperation> entryStrategy;
712
713         protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
714             super(OrderedMapNode.class);
715             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
716         }
717
718         @SuppressWarnings("rawtypes")
719         @Override
720         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
721             checkArgument(original instanceof OrderedMapNode);
722             return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
723         }
724
725         @Override
726         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
727             if (identifier instanceof NodeIdentifierWithPredicates) {
728                 return entryStrategy;
729             }
730             return Optional.absent();
731         }
732
733         @Override
734         public String toString() {
735             return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
736         }
737     }
738
739     public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
740         Set<DataSchemaNode> realChildSchemas = new HashSet<>();
741         for(DataSchemaNode augChild : schema.getChildNodes()) {
742             realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
743         }
744         return new AugmentationSchemaProxy(schema, realChildSchemas);
745     }
746
747     public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
748         if(!condition) {
749             throw new DataPreconditionFailedException(path, message);
750         }
751         return condition;
752     }
753
754 }