Notify listeners on applySnapshot 28/45028/1
authorTom Pantelis <tpanteli@brocade.com>
Thu, 1 Sep 2016 16:14:07 +0000 (12:14 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 1 Sep 2016 19:06:35 +0000 (15:06 -0400)
When a snapshot is applied from the leader, we weren't notifying data change listeners.
We should.

Change-Id: If721c2ce7e6f27aa01f7babc0a0ad3c4468840c1
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java

index a990b50..bf3b200 100644 (file)
@@ -208,7 +208,10 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
 
         final DataTreeModification unwrapped = unwrap(mod);
         dataTree.validate(unwrapped);
-        dataTree.commit(dataTree.prepare(unwrapped));
+        DataTreeCandidateTip candidate = dataTree.prepare(unwrapped);
+        dataTree.commit(candidate);
+        notifyListeners(candidate);
+
         LOG.debug("{}: state snapshot applied in %s", logContext, elapsed);
     }
 
index 5d27224..9ae74bb 100644 (file)
@@ -10,28 +10,42 @@ package org.opendaylight.controller.cluster.datastore;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.reset;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCanCommit;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCommit;
 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediatePreCommit;
 import com.google.common.base.Optional;
 import com.google.common.base.Ticker;
+import com.google.common.collect.Maps;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
@@ -136,6 +150,61 @@ public class ShardDataTreeTest extends AbstractTest {
         assertEquals(expected, actual);
     }
 
+    @Test
+    public void testListenerNotifiedOnApplySnapshot() throws Exception {
+        final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+
+        DOMDataTreeChangeListener listener = mock(DOMDataTreeChangeListener.class);
+        shardDataTree.registerTreeChangeListener(CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME), listener);
+
+        addCar(shardDataTree, "optima");
+
+        verifyOnDataTreeChanged(listener, dtc -> {
+            assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+            assertEquals("getRootPath", CarsModel.newCarPath("optima"), dtc.getRootPath());
+        });
+
+        addCar(shardDataTree, "sportage");
+
+        verifyOnDataTreeChanged(listener, dtc -> {
+            assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
+            assertEquals("getRootPath", CarsModel.newCarPath("sportage"), dtc.getRootPath());
+        });
+
+        ShardDataTree newDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
+        addCar(newDataTree, "optima");
+        addCar(newDataTree, "murano");
+
+        shardDataTree.applySnapshot(newDataTree.takeStateSnapshot());
+
+        Map<YangInstanceIdentifier, ModificationType> expChanges = Maps.newHashMap();
+        expChanges.put(CarsModel.newCarPath("optima"), ModificationType.WRITE);
+        expChanges.put(CarsModel.newCarPath("murano"), ModificationType.WRITE);
+        expChanges.put(CarsModel.newCarPath("sportage"), ModificationType.DELETE);
+        verifyOnDataTreeChanged(listener, dtc -> {
+            ModificationType expType = expChanges.remove(dtc.getRootPath());
+            assertNotNull("Got unexpected change for " + dtc.getRootPath(), expType);
+            assertEquals("getModificationType", expType, dtc.getRootNode().getModificationType());
+        });
+
+        if(!expChanges.isEmpty()) {
+            fail("Missing change notifications: " + expChanges);
+        }
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static void verifyOnDataTreeChanged(DOMDataTreeChangeListener listener, Consumer<DataTreeCandidate> callback) {
+        ArgumentCaptor<Collection> changes = ArgumentCaptor.forClass(Collection.class);
+        verify(listener, atLeastOnce()).onDataTreeChanged(changes.capture());
+        for(Collection list: changes.getAllValues()) {
+            for(Object dtc: list) {
+                callback.accept((DataTreeCandidate)dtc);
+            }
+        }
+
+        reset(listener);
+    }
+
     private static NormalizedNode<?, ?> getCars(final ShardDataTree shardDataTree) {
         final ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId());
         final DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
@@ -148,10 +217,14 @@ public class ShardDataTreeTest extends AbstractTest {
     }
 
     private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree) throws ExecutionException, InterruptedException {
+        return addCar(shardDataTree, "altima");
+    }
+
+    private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree, String name) throws ExecutionException, InterruptedException {
         return doTransaction(shardDataTree, snapshot -> {
                 snapshot.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
                 snapshot.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
-                snapshot.write(CarsModel.newCarPath("altima"), CarsModel.newCarEntry("altima", new BigInteger("100")));
+                snapshot.write(CarsModel.newCarPath(name), CarsModel.newCarEntry(name, new BigInteger("100")));
             });
     }
 
@@ -196,4 +269,4 @@ public class ShardDataTreeTest extends AbstractTest {
         return candidate;
     }
 
-}
\ No newline at end of file
+}

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.