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);
--- /dev/null
+/*
+ * Copyright (c) 2015 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.yangtools.yang.data.impl.schema.tree;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import com.google.common.base.Optional;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+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.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class Bug2690FixTest {
+ private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
+ private SchemaContext schemaContext;
+ private RootModificationApplyOperation rootOper;
+
+ private InMemoryDataTree inMemoryDataTree;
+
+ @Before
+ public void prepare() {
+ schemaContext = createTestContext();
+ assertNotNull("Schema context must not be null.", schemaContext);
+ rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
+ inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create();
+ inMemoryDataTree.setSchemaContext(schemaContext);
+ }
+
+ public static final InputStream getDatastoreTestInputStream() {
+ return getInputStream(ODL_DATASTORE_TEST_YANG);
+ }
+
+ private static InputStream getInputStream(final String resourceName) {
+ return TestModel.class.getResourceAsStream(ODL_DATASTORE_TEST_YANG);
+ }
+
+ public static SchemaContext createTestContext() {
+ final YangParserImpl parser = new YangParserImpl();
+ final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
+ return parser.resolveSchemaContext(modules);
+ }
+
+ @Test
+ public void testWriteMerge1() throws DataValidationFailedException {
+ final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
+ final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
+ final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
+ .withChild(fooEntryNode).build();
+ final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
+ .withChild(barEntryNode).build();
+
+ final ContainerNode cont1 = Builders.containerBuilder()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+ .withChild(mapNode1).build();
+
+ final ContainerNode cont2 = Builders.containerBuilder()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+ .withChild(mapNode2).build();
+
+ final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
+ modificationTree.write(TestModel.TEST_PATH, cont1);
+ modificationTree.merge(TestModel.TEST_PATH, cont2);
+
+ inMemoryDataTree.validate(modificationTree);
+ final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
+ inMemoryDataTree.commit(prepare);
+
+ final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
+ final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
+ final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
+ assertTrue(readNode.isPresent());
+ assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
+
+ }
+
+
+}