Fix NPE when creating a producer after shard was unregistered 62/44162/1
authorTomas Cere <tcere@cisco.com>
Wed, 17 Aug 2016 11:24:54 +0000 (13:24 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 17 Aug 2016 15:32:32 +0000 (15:32 +0000)
Change-Id: I9f6728f4c885cc18d8adf8dc9d1c14e8c99977ef
Signed-off-by: Tomas Cere <tcere@cisco.com>
(cherry picked from commit f065e5c7ac0ff2bf0227b06c1ebcdf29d30414e0)

dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/ShardedDOMDataTree.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/ShardedDOMDataTreeTest.java
dom/mdsal-dom-inmemory-datastore/src/main/java/org/opendaylight/mdsal/dom/store/inmemory/InMemoryDOMDataTreeShard.java

index eb4c1df119b047a19a6c61e467eb4bb139c7ed89..2ef26a9dc34d7ffa581ec0853f4a51b9fc9951a0 100644 (file)
@@ -142,7 +142,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
             Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", subtree, producer);
 
             final DOMDataTreePrefixTableEntry<ShardRegistration<?>> possibleShardReg = shards.lookup(subtree);
-            if (possibleShardReg != null) {
+            if (possibleShardReg != null && possibleShardReg.getValue() != null) {
                 shardMap.put(subtree, possibleShardReg.getValue().getInstance());
             }
         }
index 1ffa904ef44eb78f61c58872912bfa68d7b7ef49..528234f56e3f715b311c7d7cd7ada943ee3dfc85 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import junit.framework.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -109,6 +110,38 @@ public class ShardedDOMDataTreeTest {
         dataTreeService.createProducer(Collections.singletonList(TEST_ID));
     }
 
+    @Test
+    public void testShardRegistrationClose() throws Exception {
+        rootShardReg.close();
+
+        final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
+        newRootShard.onGlobalContextUpdated(schemaContext);
+        final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
+
+        ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
+                dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
+        shardRegProducer.close();
+
+        final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1, 1);
+        innerShard.onGlobalContextUpdated(schemaContext);
+        final DOMDataTreeProducer shardRegProducer2 = dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
+        ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
+
+        innerShardReg.close();
+        // try to register the shard again
+        innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
+        final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
+        final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
+        assertNotNull(cursor);
+
+        cursor.close();
+        tx.cancel();
+        shardRegProducer2.close();
+
+        innerShardReg.close();
+        newRootShardReg.close();
+    }
+
     @Test
     public void testSingleShardWrite() throws Exception {
         final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
index b45d9abab2c9953db7fd49789ac2340f431c1330..21ead91ae91b392f925584d9d61bf1340057cd65 100644 (file)
@@ -100,6 +100,7 @@ public class InMemoryDOMDataTreeShard implements ReadableWriteableDOMDataTreeSha
     @Override
     public void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
         childShards.remove(prefix);
+        childShardsTable.remove(prefix);
     }
 
     @Override