Fix logic ensuring parent presence 84/88984/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Apr 2020 13:01:59 +0000 (15:01 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Apr 2020 14:23:51 +0000 (16:23 +0200)
When we are asked to create intermediate nodes for a path, whose
parent is the conceptual root (i.e. its parent is empty), we should
not attempt to merge it, as it just does not make sense.

JIRA: MDSAL-534
Change-Id: Ia5f6ef2857903d8e88030f4c1f3867b8ed8f5a0e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit ed271c6322092bccf8ff9500b1bbb0fb62683419)

binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/BindingDOMWriteTransactionAdapter.java
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/WriteTransactionTest.java

index 121ad0381acf3c190c3b117188fa09e2f8884a2f..f7f079b5c2a6c565990be10e0feb3ee907d563e5 100644 (file)
@@ -86,7 +86,7 @@ class BindingDOMWriteTransactionAdapter<T extends DOMDataTreeWriteTransaction> e
     private void ensureParentsByMerge(final LogicalDatastoreType store, final YangInstanceIdentifier domPath,
             final InstanceIdentifier<?> path) {
         final YangInstanceIdentifier parentPath = domPath.getParent();
-        if (parentPath != null) {
+        if (parentPath != null && !parentPath.isEmpty()) {
             final NormalizedNode<?, ?> parentNode = getCodec().instanceIdentifierToNode(parentPath);
             getDelegate().merge(store, YangInstanceIdentifier.create(parentNode.getIdentifier()), parentNode);
         }
index d742d325bed2d380c339cf429686fddab6600769..55f26a098e22978b89d928357899758c2d47dd3d 100644 (file)
@@ -49,6 +49,13 @@ public class WriteTransactionTest extends AbstractDataBrokerTest {
         assertTrue("List node must exists after commit",listNode.isPresent());
     }
 
+    @Test
+    public void testPutCreateParentsSuperfluous() throws InterruptedException, ExecutionException {
+        final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
+        writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
+        writeTx.commit().get();
+    }
+
     @Test
     public void testMergeCreateParentsSuccess() throws InterruptedException, ExecutionException {
         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
@@ -61,4 +68,11 @@ public class WriteTransactionTest extends AbstractDataBrokerTest {
         final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
         assertTrue("List node must exists after commit",listNode.isPresent());
     }
+
+    @Test
+    public void testMergeCreateParentsSuperfluous() throws InterruptedException, ExecutionException {
+        final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
+        writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
+        writeTx.commit().get();
+    }
 }