BUG-509: Refactor listener walking
[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             UnsignedLong nodeVersion = subtreeVersion;
276             return StoreMetadataNode.builder().setNodeVersion(nodeVersion).setSubtreeVersion(subtreeVersion)
277                     .setData(modification.getWrittenValue()).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 verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
333             checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
334             checkArgument(writtenValue instanceof NormalizedNodeContainer);
335
336             NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
337             for (Object child : container.getValue()) {
338                 checkArgument(child instanceof NormalizedNode);
339
340                 /*
341                  * FIXME: fail-fast semantics:
342                  *
343                  * We can validate the data structure here, aborting the commit
344                  * before it ever progresses to being committed.
345                  */
346             }
347         }
348
349         @Override
350         protected StoreMetadataNode applyWrite(final NodeModification modification,
351                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
352
353             NormalizedNode<?, ?> newValue = modification.getWrittenValue();
354
355             final UnsignedLong nodeVersion;
356             if (currentMeta.isPresent()) {
357                 nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
358             } else {
359                 nodeVersion = subtreeVersion;
360             }
361
362             final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
363             if (!modification.hasAdditionalModifications()) {
364                 return newValueMeta;
365             }
366
367             @SuppressWarnings("rawtypes")
368             NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
369             StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) //
370                     .setNodeVersion(nodeVersion) //
371                     .setSubtreeVersion(subtreeVersion);
372
373             return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
374         }
375
376         @Override
377         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
378                 final UnsignedLong subtreeVersion) {
379             // For Node Containers - merge is same as subtree change - we only replace children.
380             return applySubtreeChange(modification, currentMeta, subtreeVersion);
381         }
382
383         @Override
384         public StoreMetadataNode applySubtreeChange(final NodeModification modification,
385                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
386             // Bump subtree version to its new target
387             final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
388
389             @SuppressWarnings("rawtypes")
390             NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
391             StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder, currentMeta)
392                     .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
393                     .setSubtreeVersion(updatedSubtreeVersion);
394
395             return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
396         }
397
398         private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
399                 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
400
401             for (NodeModification mod : modifications) {
402                 final PathArgument id = mod.getIdentifier();
403                 final Optional<StoreMetadataNode> cm = meta.getChild(id);
404
405                 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
406                 if (result.isPresent()) {
407                     builder.add(result.get());
408                 } else {
409                     builder.remove(id);
410                 }
411             }
412
413             return builder.build();
414         }
415
416         @Override
417         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
418                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
419             checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
420             checkChildPreconditions(path,modification,current);
421
422         }
423
424         private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
425             StoreMetadataNode currentMeta = current.get();
426             for (NodeModification childMod : modification.getModifications()) {
427                 PathArgument childId = childMod.getIdentifier();
428                 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
429                 InstanceIdentifier childPath = StoreUtils.append(path, childId);
430                 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
431             }
432         }
433
434         @Override
435         protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
436                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
437             if(current.isPresent()) {
438                 checkChildPreconditions(path,modification,current);
439             }
440         }
441
442         @SuppressWarnings("rawtypes")
443         protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
444     }
445
446     public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
447
448         private final T schema;
449         private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
450                 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
451
452                     @Override
453                     public ModificationApplyOperation apply(final PathArgument identifier) {
454                         if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
455                             return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
456                         }
457
458                         DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
459                         if (child == null) {
460                             return null;
461                         }
462                         return from(child);
463                     }
464                 }));
465
466         protected DataNodeContainerModificationStrategy(final T schema,
467                 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
468             super(nodeClass);
469             this.schema = schema;
470         }
471
472         protected T getSchema() {
473             return schema;
474         }
475
476         @Override
477         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
478             try {
479                 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
480             } catch (ExecutionException e) {
481                 return Optional.absent();
482             }
483         }
484
485         @Override
486         @SuppressWarnings("rawtypes")
487         protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
488
489         @Override
490         public String toString() {
491             return getClass().getSimpleName() + " [" + schema + "]";
492         }
493
494     }
495
496     public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
497
498         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
499             super(schemaNode, ContainerNode.class);
500         }
501
502         @Override
503         @SuppressWarnings("rawtypes")
504         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
505             checkArgument(original instanceof ContainerNode);
506             return ImmutableContainerNodeBuilder.create((ContainerNode) original);
507         }
508     }
509
510     public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
511
512         public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
513             super(schemaNode, UnkeyedListEntryNode.class);
514         }
515
516         @Override
517         @SuppressWarnings("rawtypes")
518         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
519             checkArgument(original instanceof UnkeyedListEntryNode);
520             return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
521         }
522     }
523
524     public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
525
526         protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
527             super(createAugmentProxy(schema,resolved), AugmentationNode.class);
528         }
529
530         @Override
531         @SuppressWarnings("rawtypes")
532         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
533             checkArgument(original instanceof AugmentationNode);
534             return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
535         }
536     }
537
538     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
539
540         private final Map<PathArgument, ModificationApplyOperation> childNodes;
541
542         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
543             super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
544             ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
545
546             for (ChoiceCaseNode caze : schemaNode.getCases()) {
547                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
548                     SchemaAwareApplyOperation childNode = from(cazeChild);
549                     child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
550                 }
551             }
552             childNodes = child.build();
553         }
554
555         @Override
556         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
557             return Optional.fromNullable(childNodes.get(child));
558         }
559
560         @Override
561         @SuppressWarnings("rawtypes")
562         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
563             checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
564             return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
565         }
566     }
567
568     public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
569
570         protected ListEntryModificationStrategy(final ListSchemaNode schema) {
571             super(schema, MapEntryNode.class);
572         }
573
574         @Override
575         @SuppressWarnings("rawtypes")
576         protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
577             checkArgument(original instanceof MapEntryNode);
578             return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
579         }
580     }
581
582     public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
583
584         private final Optional<ModificationApplyOperation> entryStrategy;
585
586         @SuppressWarnings({ "unchecked", "rawtypes" })
587         protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
588             super((Class) LeafSetNode.class);
589             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
590         }
591
592         @SuppressWarnings("rawtypes")
593         @Override
594         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
595             checkArgument(original instanceof LeafSetNode<?>);
596             return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
597         }
598
599         @Override
600         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
601             if (identifier instanceof NodeWithValue) {
602                 return entryStrategy;
603             }
604             return Optional.absent();
605         }
606     }
607
608     public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
609
610         private final Optional<ModificationApplyOperation> entryStrategy;
611
612         @SuppressWarnings({ "unchecked", "rawtypes" })
613         protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
614             super((Class) LeafSetNode.class);
615             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
616         }
617
618         @SuppressWarnings("rawtypes")
619         @Override
620         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
621             checkArgument(original instanceof OrderedLeafSetNode<?>);
622             return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
623         }
624
625         @Override
626         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
627             if (identifier instanceof NodeWithValue) {
628                 return entryStrategy;
629             }
630             return Optional.absent();
631         }
632     }
633
634     public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
635
636         private final Optional<ModificationApplyOperation> entryStrategy;
637
638         protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
639             entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
640         }
641
642         @Override
643         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
644                 final UnsignedLong subtreeVersion) {
645             return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
646         }
647
648         @Override
649         protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
650                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
651             throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
652         }
653
654         @Override
655         protected StoreMetadataNode applyWrite(final NodeModification modification,
656                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
657             return StoreMetadataNode.createRecursively(modification.getWrittenValue(), subtreeVersion);
658         }
659
660         @Override
661         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
662             if (child instanceof NodeIdentifier) {
663                 return entryStrategy;
664             }
665             return Optional.absent();
666         }
667
668         @Override
669         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
670
671         }
672
673         @Override
674         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
675                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
676             throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
677         }
678
679     }
680
681     public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
682
683         private final Optional<ModificationApplyOperation> entryStrategy;
684
685         protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
686             super(MapNode.class);
687             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
688         }
689
690         @SuppressWarnings("rawtypes")
691         @Override
692         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
693             checkArgument(original instanceof MapNode);
694             return ImmutableMapNodeBuilder.create((MapNode) original);
695         }
696
697         @Override
698         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
699             if (identifier instanceof NodeIdentifierWithPredicates) {
700                 return entryStrategy;
701             }
702             return Optional.absent();
703         }
704
705         @Override
706         public String toString() {
707             return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
708         }
709     }
710
711     public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
712
713         private final Optional<ModificationApplyOperation> entryStrategy;
714
715         protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
716             super(OrderedMapNode.class);
717             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
718         }
719
720         @SuppressWarnings("rawtypes")
721         @Override
722         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
723             checkArgument(original instanceof OrderedMapNode);
724             return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
725         }
726
727         @Override
728         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
729             if (identifier instanceof NodeIdentifierWithPredicates) {
730                 return entryStrategy;
731             }
732             return Optional.absent();
733         }
734
735         @Override
736         public String toString() {
737             return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
738         }
739     }
740
741     public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
742         Set<DataSchemaNode> realChildSchemas = new HashSet<>();
743         for(DataSchemaNode augChild : schema.getChildNodes()) {
744             realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
745         }
746         return new AugmentationSchemaProxy(schema, realChildSchemas);
747     }
748
749     public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
750         if(!condition) {
751             throw new DataPreconditionFailedException(path, message);
752         }
753         return condition;
754     }
755
756 }