Bug 2690 - Yang-Data-Impl: write and then merge on same list in
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.java
index 6d20d385ee194b908f6aa015679324fb5cbd3e39..08fbcaa9a6f764ae7f46ce6e0f5c1211ce7ccbd0 100644 (file)
@@ -35,6 +35,22 @@ final class OperationWithModification {
         if (data instanceof NormalizedNodeContainer<?,?,?>) {
             @SuppressWarnings({ "rawtypes", "unchecked" })
             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
+
+            /*
+             * if there was write before on this node and it is of NormalizedNodeContainer type
+             * merge would overwrite our changes. So we create write modifications from data children to
+             * retain children created by past write operation.
+             * These writes will then be pushed down in the tree while there are merge modifications on these children
+             */
+            if (modification.getOperation().equals(LogicalOperation.WRITE)) {
+                @SuppressWarnings({ "rawtypes", "unchecked" })
+                NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
+                        (NormalizedNodeContainer) modification.getWrittenValue();
+                for (NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
+                    PathArgument childId = child.getIdentifier();
+                    forChild(childId).write(child);
+                }
+            }
             for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
                 PathArgument childId = child.getIdentifier();
                 forChild(childId).recursiveMerge(child);