Bug 4105: Integrate EntityOwnerChangeListener with EntityOwnershipShard 99/26799/1
authorTom Pantelis <tpanteli@brocade.com>
Fri, 14 Aug 2015 10:13:54 +0000 (06:13 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 10 Sep 2015 19:15:02 +0000 (15:15 -0400)
Change-Id: Ia302d503f9ff65aa48faf7d69f1405ebf5267166
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListener.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShard.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListenerTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShardTest.java

index 35100cb..82f926a 100644 (file)
@@ -44,15 +44,17 @@ import org.slf4j.LoggerFactory;
  * @author Moiz Raja
  * @author Thomas Pantelis
  */
-public class CandidateListChangeListener implements DOMDataTreeChangeListener {
+class CandidateListChangeListener implements DOMDataTreeChangeListener {
     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
 
     private final ActorRef shard;
     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
 
-    public CandidateListChangeListener(ActorRef shard, ShardDataTree shardDataTree) {
+    CandidateListChangeListener(ActorRef shard) {
         this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
+    }
 
+    void init(ShardDataTree shardDataTree) {
         shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).
                 node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME).
                         node(Candidate.QNAME).node(Candidate.QNAME).build(), this);
index 629f938..175e854 100644 (file)
@@ -45,6 +45,7 @@ import scala.concurrent.Future;
 class EntityOwnershipShard extends Shard {
     private final String localMemberName;
     private final EntityOwnershipShardCommitCoordinator commitCoordinator;
+    private final EntityOwnershipListenerSupport listenerSupport;
 
     private static DatastoreContext noPersistenceDatastoreContext(DatastoreContext datastoreContext) {
         return DatastoreContext.newBuilderFrom(datastoreContext).persistent(false).build();
@@ -55,6 +56,7 @@ class EntityOwnershipShard extends Shard {
         super(name, peerAddresses, noPersistenceDatastoreContext(datastoreContext), schemaContext);
         this.localMemberName = localMemberName;
         this.commitCoordinator = new EntityOwnershipShardCommitCoordinator(localMemberName, LOG);
+        this.listenerSupport = new EntityOwnershipListenerSupport(getContext());
     }
 
     @Override
@@ -66,7 +68,8 @@ class EntityOwnershipShard extends Shard {
     protected void onRecoveryComplete() {
         super.onRecoveryComplete();
 
-        new CandidateListChangeListener(getSelf(), getDataStore());
+        new CandidateListChangeListener(getSelf()).init(getDataStore());
+        new EntityOwnerChangeListener(localMemberName, listenerSupport).init(getDataStore());
     }
 
     @Override
@@ -87,7 +90,7 @@ class EntityOwnershipShard extends Shard {
     private void onRegisterCandidateLocal(RegisterCandidateLocal registerCandidate) {
         LOG.debug("onRegisterCandidateLocal: {}", registerCandidate);
 
-        // TODO - add the listener locally.
+        listenerSupport.addEntityOwnershipListener(registerCandidate.getEntity(), registerCandidate.getCandidate());
 
         NormalizedNode<?, ?> entityOwners = entityOwnersWithCandidate(registerCandidate.getEntity().getType(),
                 registerCandidate.getEntity().getId(), localMemberName);
index 149d5f4..aa96fc2 100644 (file)
@@ -45,7 +45,7 @@ public class CandidateListChangeListenerTest extends AbstractActorTest {
     public void testOnDataTreeChanged() throws Exception {
         JavaTestKit kit = new JavaTestKit(getSystem());
 
-        CandidateListChangeListener listener = new CandidateListChangeListener(kit.getRef(), shardDataTree);
+        new CandidateListChangeListener(kit.getRef()).init(shardDataTree);
 
         String memberName1 = "member-1";
         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
index a6bf30c..49953ac 100644 (file)
@@ -9,6 +9,8 @@ package org.opendaylight.controller.cluster.datastore.entityownership;
 
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
 import akka.actor.ActorRef;
 import akka.actor.Props;
@@ -98,6 +100,8 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
         verifyCommittedEntityCandidate(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
 
         verifyOwner(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
+
+        verify(candidate, timeout(5000)).ownershipChanged(entity, false, true);
     }
 
     @Test
@@ -127,6 +131,8 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
         verifyCommittedEntityCandidate(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
 
         verifyOwner(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
+
+        verify(candidate, timeout(5000)).ownershipChanged(entity, false, true);
     }
 
     @Test
@@ -169,6 +175,8 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
         verifyCommittedEntityCandidate(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
 
         verifyOwner(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
+
+        verify(candidate, timeout(5000)).ownershipChanged(entity, false, true);
     }
 
     @Test
@@ -207,6 +215,8 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
         verifyCommittedEntityCandidate(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
 
         verifyOwner(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
+
+        verify(candidate, timeout(5000)).ownershipChanged(entity, false, true);
     }
 
     @Test