BUG-648: Improve performance by reusing old data nodes
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / SchemaAwareApplyOperation.java
index 114595f75b3bed2fb4aa3150f872f6e876f30efc..4bb5aed20c72d9a8424063818cf0c84e31630b47 100644 (file)
@@ -1,23 +1,57 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.md.sal.dom.store.impl;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ExecutionException;
 
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreNodeCompositeBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -26,9 +60,13 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSet.Builder;
+import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.primitives.UnsignedLong;
 
 public abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
@@ -37,20 +75,54 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
         if (schemaNode instanceof ContainerSchemaNode) {
             return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
         } else if (schemaNode instanceof ListSchemaNode) {
-            return new ListMapModificationStrategy((ListSchemaNode) schemaNode);
+            return fromListSchemaNode((ListSchemaNode) schemaNode);
         } else if (schemaNode instanceof ChoiceNode) {
             return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
         } else if (schemaNode instanceof LeafListSchemaNode) {
-            return new LeafSetEntryModificationStrategy((LeafListSchemaNode) schemaNode);
+            return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
         } else if (schemaNode instanceof LeafSchemaNode) {
             return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
         }
         throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
     }
 
-    @Override
-    public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
-        throw new IllegalArgumentException();
+    private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
+        List<QName> keyDefinition = schemaNode.getKeyDefinition();
+        if (keyDefinition == null || keyDefinition.isEmpty()) {
+            return new UnkeyedListModificationStrategy(schemaNode);
+        }
+        if (schemaNode.isUserOrdered()) {
+            return new OrderedMapModificationStrategy(schemaNode);
+        }
+
+        return new UnorderedMapModificationStrategy(schemaNode);
+    }
+
+    private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
+        if(schemaNode.isUserOrdered()) {
+            return new OrderedLeafSetModificationStrategy(schemaNode);
+        } else {
+            return new UnorderedLeafSetModificationStrategy(schemaNode);
+        }
+    }
+
+
+    public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
+            final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
+        AugmentationSchema augSchema = null;
+        allAugments: for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
+            boolean containsAll = true;
+            for (DataSchemaNode child : potential.getChildNodes()) {
+                if (identifier.getPossibleChildNames().contains(child.getQName())) {
+                    augSchema = potential;
+                    break allAugments;
+                }
+            }
+        }
+        if (augSchema != null) {
+            return new AugmentationModificationStrategy(augSchema, resolvedTree);
+        }
+        return null;
     }
 
     protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
@@ -59,16 +131,85 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
         return potential.get();
     }
 
+    @Override
+    public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
+        if (modification.getModificationType() == ModificationType.WRITE) {
+            verifyWritenStructure(modification.getWritenValue());
+        }
+    }
+
+    protected abstract void verifyWritenStructure(NormalizedNode<?, ?> writenValue);
+
+    @Override
+    public boolean isApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+        switch (modification.getModificationType()) {
+        case DELETE:
+            return isDeleteApplicable(modification, current);
+        case SUBTREE_MODIFIED:
+            return isSubtreeModificationApplicable(modification, current);
+        case WRITE:
+            return isWriteApplicable(modification, current);
+        case MERGE:
+            return isMergeApplicable(modification,current);
+        case UNMODIFIED:
+            return true;
+        default:
+            return false;
+        }
+    }
+
+    private boolean isMergeApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+        Optional<StoreMetadataNode> original = modification.getOriginal();
+        if (original.isPresent() && current.isPresent()) {
+            return isNotConflicting(original.get(), current.get());
+        } else if (current.isPresent()) {
+            return true;
+        }
+        return true;
+    }
+
+    protected boolean isWriteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+        Optional<StoreMetadataNode> original = modification.getOriginal();
+        if (original.isPresent() && current.isPresent()) {
+            return isNotConflicting(original.get(), current.get());
+        } else if (current.isPresent()) {
+            return false;
+        }
+        return true;
+
+    }
+
+    protected final boolean isNotConflicting(final StoreMetadataNode original, final StoreMetadataNode current) {
+        return original.getNodeVersion().equals(current.getNodeVersion())
+                && original.getSubtreeVersion().equals(current.getSubtreeVersion());
+    }
+
+    protected abstract boolean isSubtreeModificationApplicable(final NodeModification modification,
+            final Optional<StoreMetadataNode> current);
+
+    private boolean isDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+        // FiXME: Add delete conflict detection.
+        return true;
+    }
+
     @Override
     public final Optional<StoreMetadataNode> apply(final NodeModification modification,
-            final Optional<StoreMetadataNode> currentMeta) {
+            final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+
         switch (modification.getModificationType()) {
         case DELETE:
-            return Optional.absent();
+            return modification.storeSnapshot(Optional.<StoreMetadataNode> absent());
         case SUBTREE_MODIFIED:
-            return Optional.of(applySubtreeChange(modification, currentMeta.get()));
+            Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
+                    modification);
+            return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
+                    subtreeVersion)));
+        case MERGE:
+            if(currentMeta.isPresent()) {
+                return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(),subtreeVersion)));
+            } // Fallback to write is intentional - if node is not preexisting merge is same as write
         case WRITE:
-            return Optional.of(applyWrite(modification, currentMeta));
+            return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion)));
         case UNMODIFIED:
             return currentMeta;
         default:
@@ -76,19 +217,30 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
         }
     }
 
+    protected abstract StoreMetadataNode applyMerge(NodeModification modification,
+            StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
+
     protected abstract StoreMetadataNode applyWrite(NodeModification modification,
-            Optional<StoreMetadataNode> currentMeta);
+            Optional<StoreMetadataNode> currentMeta, UnsignedLong subtreeVersion);
 
-    protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification, StoreMetadataNode currentMeta);
+    protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification,
+            StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
 
     public static abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends
             SchemaAwareApplyOperation {
 
         private final T schema;
+        private final Class<? extends NormalizedNode<?, ?>> nodeClass;
 
-        protected ValueNodeModificationStrategy(final T schema) {
+        protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
             super();
             this.schema = schema;
+            this.nodeClass = nodeClass;
+        }
+
+        @Override
+        protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
+            checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
         }
 
         @Override
@@ -98,119 +250,191 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
         }
 
         @Override
-        protected StoreMetadataNode applySubtreeChange(final NodeModification modification, final StoreMetadataNode currentMeta) {
+        protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
+                final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
             throw new UnsupportedOperationException("Node " + schema.getPath()
                     + "is leaf type node. Subtree change is not allowed.");
         }
 
         @Override
-        protected StoreMetadataNode applyWrite(final NodeModification modification, final Optional<StoreMetadataNode> currentMeta) {
-            return StoreMetadataNode.builder()
-            // FIXME Add .increaseNodeVersion()
+        protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
+                final UnsignedLong subtreeVersion) {
+            return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
+        }
+
+        @Override
+        protected StoreMetadataNode applyWrite(final NodeModification modification,
+                final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+            UnsignedLong nodeVersion = subtreeVersion;
+            return StoreMetadataNode.builder().setNodeVersion(nodeVersion).setSubtreeVersion(subtreeVersion)
                     .setData(modification.getWritenValue()).build();
         }
 
+        @Override
+        protected boolean isSubtreeModificationApplicable(final NodeModification modification,
+                final Optional<StoreMetadataNode> current) {
+            return false;
+        }
+
     }
 
     public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
 
+        @SuppressWarnings({ "unchecked", "rawtypes" })
         protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
-            super(schema);
+            super(schema, (Class) LeafSetEntryNode.class);
         }
     }
 
     public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
 
+        @SuppressWarnings({ "unchecked", "rawtypes" })
         protected LeafModificationStrategy(final LeafSchemaNode schema) {
-            super(schema);
+            super(schema, (Class) LeafNode.class);
         }
     }
 
     public static abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
 
+        private final Class<? extends NormalizedNode<?, ?>> nodeClass;
+
+        protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+            this.nodeClass = nodeClass;
+        }
+
+        @Override
+        public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
+            if (modification.getModificationType() == ModificationType.WRITE) {
+
+            }
+            for (NodeModification childModification : modification.getModifications()) {
+                resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
+            }
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
+            checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
+            checkArgument(writenValue instanceof NormalizedNodeContainer);
+            NormalizedNodeContainer writenCont = (NormalizedNodeContainer) writenValue;
+            for (Object child : writenCont.getValue()) {
+                checkArgument(child instanceof NormalizedNode);
+                NormalizedNode childNode = (NormalizedNode) child;
+            }
+        }
+
         @Override
-        protected StoreMetadataNode applyWrite(final NodeModification modification, final Optional<StoreMetadataNode> currentMeta) {
-            //
+        protected StoreMetadataNode applyWrite(final NodeModification modification,
+                final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+
             NormalizedNode<?, ?> newValue = modification.getWritenValue();
 
-            StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursivelly(newValue, UnsignedLong.valueOf(0));
+            final UnsignedLong nodeVersion;
+            if (currentMeta.isPresent()) {
+                nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
+            } else {
+                nodeVersion = subtreeVersion;
+            }
 
-            if(!modification.hasAdditionalModifications()) {
+            final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
+            if (!modification.hasAdditionalModifications()) {
                 return newValueMeta;
             }
-            StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(newValueMeta,
-                    createBuilder(modification.getIdentifier()));
 
-            Set<PathArgument> processedPreexisting = applyPreexistingChildren(modification, newValueMeta.getChildren(), builder);
-            applyNewChildren(modification, processedPreexisting, builder);
+            @SuppressWarnings("rawtypes")
+            NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
+            StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) //
+                    .setNodeVersion(nodeVersion) //
+                    .setSubtreeVersion(subtreeVersion);
 
-            return builder.build();
+            return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
+        }
 
+        @Override
+        protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
+                final UnsignedLong subtreeVersion) {
+            // For Node Containers - merge is same as subtree change - we only replace children.
+            return applySubtreeChange(modification, currentMeta, subtreeVersion);
         }
 
         @Override
-        @SuppressWarnings("rawtypes")
-        public StoreMetadataNode applySubtreeChange(final NodeModification modification, final StoreMetadataNode currentMeta) {
+        public StoreMetadataNode applySubtreeChange(final NodeModification modification,
+                final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
+            // Bump subtree version to its new target
+            final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
+
+            @SuppressWarnings("rawtypes")
+            NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
+            StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder, currentMeta)
+                    .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
+                    .setSubtreeVersion(updatedSubtreeVersion);
+
+            return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
+        }
 
-            StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(currentMeta,
-                    createBuilder(modification.getIdentifier()));
-            builder.setIdentifier(modification.getIdentifier());
+        private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
+                final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
 
-            // We process preexisting nodes
-            Set<PathArgument> processedPreexisting = applyPreexistingChildren(modification,
-                    currentMeta.getChildren(), builder);
-            applyNewChildren(modification, processedPreexisting, builder);
-            return builder.build();
-        }
+            for (NodeModification mod : modifications) {
+                final PathArgument id = mod.getIdentifier();
+                final Optional<StoreMetadataNode> cm = meta.getChild(id);
 
-        private void applyNewChildren(final NodeModification modification, final Set<PathArgument> ignore,
-                final StoreNodeCompositeBuilder builder) {
-            for (NodeModification childModification : modification.getModifications()) {
-                PathArgument childIdentifier = childModification.getIdentifier();
-                // We skip allready processed modifications
-                if (ignore.contains(childIdentifier)) {
-                    continue;
-                }
-                Optional<StoreMetadataNode> childResult = resolveChildOperation(childIdentifier) //
-                        .apply(childModification, Optional.<StoreMetadataNode> absent());
-                if (childResult.isPresent()) {
-                    builder.add(childResult.get());
+                Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
+                if (result.isPresent()) {
+                    builder.add(result.get());
+                } else {
+                    builder.remove(id);
                 }
             }
+
+            return builder.build();
         }
 
-        private Set<PathArgument> applyPreexistingChildren(final NodeModification modification,
-                final Iterable<StoreMetadataNode> children, final StoreNodeCompositeBuilder nodeBuilder) {
-            Builder<PathArgument> processedModifications = ImmutableSet.<PathArgument> builder();
-            for (StoreMetadataNode childMeta : children) {
-                PathArgument childIdentifier = childMeta.getIdentifier();
-                // We retrieve Child modification metadata
-                Optional<NodeModification> childModification = modification.getChild(childIdentifier);
-                // Node is modified
-                if (childModification.isPresent()) {
-                    processedModifications.add(childIdentifier);
-                    Optional<StoreMetadataNode> change = resolveChildOperation(childIdentifier) //
-                            .apply(childModification.get(), Optional.of(childMeta));
-                } else {
-                    // Child is unmodified - reuse existing metadata and data
-                    // snapshot
-                    nodeBuilder.add(childMeta);
-                }
+        @Override
+        protected boolean isSubtreeModificationApplicable(final NodeModification modification,
+                final Optional<StoreMetadataNode> current) {
+            if (false == current.isPresent()) {
+                return false;
+            }
+            boolean result = true;
+            StoreMetadataNode currentMeta = current.get();
+            for (NodeModification childMod : modification.getModifications()) {
+                PathArgument childId = childMod.getIdentifier();
+                Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
+                result &= resolveChildOperation(childId).isApplicable(childMod, childMeta);
             }
-            return processedModifications.build();
+            return result;
         }
 
         @SuppressWarnings("rawtypes")
-        protected abstract NormalizedNodeContainerBuilder createBuilder(PathArgument identifier);
+        protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
     }
 
     public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends
             NormalizedNodeContainerModificationStrategy {
 
         private final T schema;
-
-        protected DataNodeContainerModificationStrategy(final T schema) {
-            super();
+        private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
+                .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
+
+                    @Override
+                    public ModificationApplyOperation apply(final PathArgument identifier) {
+                        if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
+                            return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
+                        }
+
+                        DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
+                        if (child == null) {
+                            return null;
+                        }
+                        return from(child);
+                    }
+                }));
+
+        protected DataNodeContainerModificationStrategy(final T schema,
+                final Class<? extends NormalizedNode<?, ?>> nodeClass) {
+            super(nodeClass);
             this.schema = schema;
         }
 
@@ -220,16 +444,16 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
 
         @Override
         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
-            DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
-            if (child == null || child.isAugmenting()) {
+            try {
+                return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
+            } catch (ExecutionException e) {
                 return Optional.absent();
             }
-            return Optional.<ModificationApplyOperation> of(from(child));
         }
 
         @Override
         @SuppressWarnings("rawtypes")
-        protected abstract DataContainerNodeBuilder createBuilder(PathArgument identifier);
+        protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
 
         @Override
         public String toString() {
@@ -242,61 +466,136 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
             DataNodeContainerModificationStrategy<ContainerSchemaNode> {
 
         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
-            super(schemaNode);
+            super(schemaNode, ContainerNode.class);
         }
 
         @Override
         @SuppressWarnings("rawtypes")
-        protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
-            // TODO Auto-generated method stub
-            checkArgument(identifier instanceof NodeIdentifier);
-            return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof ContainerNode);
+            return ImmutableContainerNodeBuilder.create((ContainerNode) original);
         }
+    }
 
+    public static class UnkeyedListItemModificationStrategy extends
+            DataNodeContainerModificationStrategy<ListSchemaNode> {
+
+        public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
+            super(schemaNode, UnkeyedListEntryNode.class);
+        }
+
+        @Override
+        @SuppressWarnings("rawtypes")
+        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof UnkeyedListEntryNode);
+            return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
+        }
+    }
+
+    public static class AugmentationModificationStrategy extends
+            DataNodeContainerModificationStrategy<AugmentationSchema> {
+
+        protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
+            super(createAugmentProxy(schema,resolved), AugmentationNode.class);
+            // FIXME: Use resolved children instead of unresolved.
+
+        }
+
+        @Override
+        @SuppressWarnings("rawtypes")
+        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof AugmentationNode);
+            return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
+        }
     }
 
     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
 
         private final ChoiceNode schema;
+        private final Map<PathArgument, ModificationApplyOperation> childNodes;
 
         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
+            super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
             this.schema = schemaNode;
+            ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
+
+            for (ChoiceCaseNode caze : schemaNode.getCases()) {
+                for (DataSchemaNode cazeChild : caze.getChildNodes()) {
+                    SchemaAwareApplyOperation childNode = from(cazeChild);
+                    child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
+                }
+            }
+            childNodes = child.build();
         }
 
         @Override
-        @SuppressWarnings("rawtypes")
-        protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
-            checkArgument(identifier instanceof NodeIdentifier);
-            return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+        public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+            return Optional.fromNullable(childNodes.get(child));
         }
 
+        @Override
+        @SuppressWarnings("rawtypes")
+        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
+            return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
+        }
     }
 
     public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
 
         protected ListEntryModificationStrategy(final ListSchemaNode schema) {
-            super(schema);
+            super(schema, MapEntryNode.class);
         }
 
         @Override
         @SuppressWarnings("rawtypes")
-        protected final DataContainerNodeBuilder createBuilder(final PathArgument identifier) {
-            return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier((NodeIdentifierWithPredicates) identifier);
+        protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof MapEntryNode);
+            return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
         }
+    }
+
+    public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+
+        private final Optional<ModificationApplyOperation> entryStrategy;
 
+        @SuppressWarnings({ "unchecked", "rawtypes" })
+        protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
+            super((Class) LeafSetNode.class);
+            entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof LeafSetNode<?>);
+            return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
+        }
+
+        @Override
+        public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+            if (identifier instanceof NodeWithValue) {
+                return entryStrategy;
+            }
+            return Optional.absent();
+        }
     }
 
-    public static class LeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+    public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
 
         private final Optional<ModificationApplyOperation> entryStrategy;
 
-        protected LeafSetModificationStrategy(final LeafListSchemaNode schema) {
+        @SuppressWarnings({ "unchecked", "rawtypes" })
+        protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
+            super((Class) LeafSetNode.class);
             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
         }
 
+        @SuppressWarnings("rawtypes")
         @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) {
-            return ImmutableLeafSetNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+        protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof OrderedLeafSetNode<?>);
+            return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
         }
 
         @Override
@@ -306,20 +605,99 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
             }
             return Optional.absent();
         }
+    }
+
+    public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
 
+        private final Optional<ModificationApplyOperation> entryStrategy;
+
+        protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
+            entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
+        }
+
+        @Override
+        protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
+                final UnsignedLong subtreeVersion) {
+            return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
+        }
+
+        @Override
+        protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
+                final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
+            throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
+        }
+
+        @Override
+        protected StoreMetadataNode applyWrite(final NodeModification modification,
+                final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
+            return StoreMetadataNode.createRecursively(modification.getWritenValue(), subtreeVersion);
+        }
+
+        @Override
+        public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+            if (child instanceof NodeIdentifier) {
+                return entryStrategy;
+            }
+            return Optional.absent();
+        }
+
+        @Override
+        protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
+
+        }
+
+        @Override
+        protected boolean isSubtreeModificationApplicable(final NodeModification modification,
+                final Optional<StoreMetadataNode> current) {
+            return false;
+        }
+
+    }
+
+    public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+
+        private final Optional<ModificationApplyOperation> entryStrategy;
+
+        protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
+            super(MapNode.class);
+            entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof MapNode);
+            return ImmutableMapNodeBuilder.create((MapNode) original);
+        }
+
+        @Override
+        public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+            if (identifier instanceof NodeIdentifierWithPredicates) {
+                return entryStrategy;
+            }
+            return Optional.absent();
+        }
+
+        @Override
+        public String toString() {
+            return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
+        }
     }
 
-    public static class ListMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
+    public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
 
         private final Optional<ModificationApplyOperation> entryStrategy;
 
-        protected ListMapModificationStrategy(final ListSchemaNode schema) {
+        protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
+            super(OrderedMapNode.class);
             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
         }
 
+        @SuppressWarnings("rawtypes")
         @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) {
-            return ImmutableMapNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier);
+        protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
+            checkArgument(original instanceof OrderedMapNode);
+            return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
         }
 
         @Override
@@ -332,7 +710,7 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
 
         @Override
         public String toString() {
-            return "ListMapModificationStrategy [entry=" + entryStrategy + "]";
+            return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
         }
     }
 
@@ -340,4 +718,12 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper
 
     }
 
+    public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
+        Set<DataSchemaNode> realChildSchemas = new HashSet<>();
+        for(DataSchemaNode augChild : schema.getChildNodes()) {
+            realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
+        }
+        return new AugmentationSchemaProxy(schema, realChildSchemas);
+    }
+
 }