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