Merge "Cosmetics: check in pom.xml files as _sort_pom_ wants them to be"
[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.StoreUtils;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
36 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
51 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
52 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
53 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
54 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
55 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
57 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
58 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
61
62 import com.google.common.base.Function;
63 import com.google.common.base.Optional;
64 import com.google.common.base.Preconditions;
65 import com.google.common.cache.CacheBuilder;
66 import com.google.common.cache.CacheLoader;
67 import com.google.common.cache.LoadingCache;
68 import com.google.common.collect.ImmutableMap;
69 import com.google.common.primitives.UnsignedLong;
70
71 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
72
73     public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
74         if (schemaNode instanceof ContainerSchemaNode) {
75             return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
76         } else if (schemaNode instanceof ListSchemaNode) {
77             return fromListSchemaNode((ListSchemaNode) schemaNode);
78         } else if (schemaNode instanceof ChoiceNode) {
79             return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
80         } else if (schemaNode instanceof LeafListSchemaNode) {
81             return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
82         } else if (schemaNode instanceof LeafSchemaNode) {
83             return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
84         }
85         throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
86     }
87
88     private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
89         List<QName> keyDefinition = schemaNode.getKeyDefinition();
90         if (keyDefinition == null || keyDefinition.isEmpty()) {
91             return new UnkeyedListModificationStrategy(schemaNode);
92         }
93         if (schemaNode.isUserOrdered()) {
94             return new OrderedMapModificationStrategy(schemaNode);
95         }
96
97         return new UnorderedMapModificationStrategy(schemaNode);
98     }
99
100     private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
101         if(schemaNode.isUserOrdered()) {
102             return new OrderedLeafSetModificationStrategy(schemaNode);
103         } else {
104             return new UnorderedLeafSetModificationStrategy(schemaNode);
105         }
106     }
107
108
109     public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
110             final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
111         AugmentationSchema augSchema = null;
112
113         allAugments:
114             for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
115                 for (DataSchemaNode child : potential.getChildNodes()) {
116                     if (identifier.getPossibleChildNames().contains(child.getQName())) {
117                         augSchema = potential;
118                         break allAugments;
119                     }
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             verifyWrittenStructure(modification.getWrittenValue());
139         }
140     }
141
142     protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
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 SchemaAwareApplyOperation {
237
238         private final T schema;
239         private final Class<? extends NormalizedNode<?, ?>> nodeClass;
240
241         protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
242             super();
243             this.schema = schema;
244             this.nodeClass = nodeClass;
245         }
246
247         @Override
248         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
249             checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
250         }
251
252         @Override
253         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
254             throw new UnsupportedOperationException("Node " + schema.getPath()
255                     + "is leaf type node. Child nodes not allowed");
256         }
257
258         @Override
259         protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
260                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
261             throw new UnsupportedOperationException("Node " + schema.getPath()
262                     + "is leaf type node. Subtree change is not allowed.");
263         }
264
265         @Override
266         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
267                 final UnsignedLong subtreeVersion) {
268             return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
269         }
270
271         @Override
272         protected StoreMetadataNode applyWrite(final NodeModification modification,
273                 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
274             UnsignedLong nodeVersion = subtreeVersion;
275             return StoreMetadataNode.builder().setNodeVersion(nodeVersion).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.from(dataBuilder) //
369                     .setNodeVersion(nodeVersion) //
370                     .setSubtreeVersion(subtreeVersion);
371
372             return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
373         }
374
375         @Override
376         protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
377                 final UnsignedLong subtreeVersion) {
378             // For Node Containers - merge is same as subtree change - we only replace children.
379             return applySubtreeChange(modification, currentMeta, subtreeVersion);
380         }
381
382         @Override
383         public StoreMetadataNode applySubtreeChange(final NodeModification modification,
384                 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
385             // Bump subtree version to its new target
386             final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
387
388             @SuppressWarnings("rawtypes")
389             NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
390             StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder, currentMeta)
391                     .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
392                     .setSubtreeVersion(updatedSubtreeVersion);
393
394             return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
395         }
396
397         private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
398                 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
399
400             for (NodeModification mod : modifications) {
401                 final PathArgument id = mod.getIdentifier();
402                 final Optional<StoreMetadataNode> cm = meta.getChild(id);
403
404                 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
405                 if (result.isPresent()) {
406                     builder.add(result.get());
407                 } else {
408                     builder.remove(id);
409                 }
410             }
411
412             return builder.build();
413         }
414
415         @Override
416         protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
417                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
418             checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
419             checkChildPreconditions(path,modification,current);
420
421         }
422
423         private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
424             StoreMetadataNode currentMeta = current.get();
425             for (NodeModification childMod : modification.getModifications()) {
426                 PathArgument childId = childMod.getIdentifier();
427                 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
428                 InstanceIdentifier childPath = StoreUtils.append(path, childId);
429                 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
430             }
431         }
432
433         @Override
434         protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
435                 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
436             if(current.isPresent()) {
437                 checkChildPreconditions(path,modification,current);
438             }
439         }
440
441         @SuppressWarnings("rawtypes")
442         protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
443     }
444
445     public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
446
447         private final T schema;
448         private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
449                 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
450
451                     @Override
452                     public ModificationApplyOperation apply(final PathArgument identifier) {
453                         if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
454                             return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
455                         }
456
457                         DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
458                         if (child == null) {
459                             return null;
460                         }
461                         return from(child);
462                     }
463                 }));
464
465         protected DataNodeContainerModificationStrategy(final T schema,
466                 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
467             super(nodeClass);
468             this.schema = schema;
469         }
470
471         protected T getSchema() {
472             return schema;
473         }
474
475         @Override
476         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
477             try {
478                 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
479             } catch (ExecutionException e) {
480                 return Optional.absent();
481             }
482         }
483
484         @Override
485         @SuppressWarnings("rawtypes")
486         protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
487
488         @Override
489         public String toString() {
490             return getClass().getSimpleName() + " [" + schema + "]";
491         }
492
493     }
494
495     public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
496
497         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
498             super(schemaNode, ContainerNode.class);
499         }
500
501         @Override
502         @SuppressWarnings("rawtypes")
503         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
504             checkArgument(original instanceof ContainerNode);
505             return ImmutableContainerNodeBuilder.create((ContainerNode) original);
506         }
507     }
508
509     public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
510
511         public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
512             super(schemaNode, UnkeyedListEntryNode.class);
513         }
514
515         @Override
516         @SuppressWarnings("rawtypes")
517         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
518             checkArgument(original instanceof UnkeyedListEntryNode);
519             return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
520         }
521     }
522
523     public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
524
525         protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
526             super(createAugmentProxy(schema,resolved), AugmentationNode.class);
527         }
528
529         @Override
530         @SuppressWarnings("rawtypes")
531         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
532             checkArgument(original instanceof AugmentationNode);
533             return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
534         }
535     }
536
537     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
538
539         private final Map<PathArgument, ModificationApplyOperation> childNodes;
540
541         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
542             super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
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.getWrittenValue(), 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 verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
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 static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
741         Set<DataSchemaNode> realChildSchemas = new HashSet<>();
742         for(DataSchemaNode augChild : schema.getChildNodes()) {
743             realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
744         }
745         return new AugmentationSchemaProxy(schema, realChildSchemas);
746     }
747
748     public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
749         if(!condition) {
750             throw new DataPreconditionFailedException(path, message);
751         }
752         return condition;
753     }
754
755 }