When no candidates are present for an entity do not return EntityOwnershipState 93/33493/4
authorMoiz Raja <moraja@cisco.com>
Fri, 8 Jan 2016 04:18:01 +0000 (20:18 -0800)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 4 Feb 2016 19:33:22 +0000 (19:33 +0000)
Change-Id: I22c0100755a1fca50c638ff4b435e04bdd0f76ff
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipService.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipIntegrationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java

index 0277271..a2a63f4 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NODE_ID;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
 import akka.actor.ActorRef;
@@ -44,6 +45,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controll
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 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.tree.DataTree;
 import org.slf4j.Logger;
@@ -173,19 +175,26 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
         Preconditions.checkNotNull(forEntity, "forEntity cannot be null");
 
         DataTree dataTree = getLocalEntityOwnershipShardDataTree();
-        if(dataTree == null) {
+        if (dataTree == null) {
             return Optional.absent();
         }
 
         Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(
                 entityPath(forEntity.getType(), forEntity.getId()));
-        if(!entityNode.isPresent()) {
+        if (!entityNode.isPresent()) {
+            return Optional.absent();
+        }
+
+        // Check if there are any candidates, if there are none we do not really have ownership state
+        final MapEntryNode entity = (MapEntryNode) entityNode.get();
+        final Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates = entity.getChild(CANDIDATE_NODE_ID);
+        final boolean hasCandidates = optionalCandidates.isPresent() && ((MapNode) optionalCandidates.get()).getValue().size() > 0;
+        if(!hasCandidates){
             return Optional.absent();
         }
 
         String localMemberName = datastore.getActorContext().getCurrentMemberName();
-        Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = ((MapEntryNode)entityNode.get()).
-                getChild(ENTITY_OWNER_NODE_ID);
+        Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID);
         String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
         boolean hasOwner = !Strings.isNullOrEmpty(owner);
         boolean isOwner = hasOwner && localMemberName.equals(owner);
index bad8647..c8f4bb7 100644 (file)
@@ -400,7 +400,8 @@ public class DistributedEntityOwnershipIntegrationTest {
         boolean passed = false;
         for(int i=0;i<100;i++) {
             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
-            if(!leaderEntityOwnershipService.getOwnershipState(ENTITY1).get().hasOwner() &&
+            if(!leaderEntityOwnershipService.getOwnershipState(ENTITY1).isPresent() ||
+                    !leaderEntityOwnershipService.getOwnershipState(ENTITY1).get().hasOwner() &&
                     !follower1EntityOwnershipService.getOwnershipState(ENTITY1).get().hasOwner() &&
                     !follower2EntityOwnershipService.getOwnershipState(ENTITY1).get().hasOwner() &&
                     leaderChangeCaptor.getAllValues().size() > 0 && !leaderChangeCaptor.getValue().hasOwner() &&
index 63e5769..8cbac4b 100644 (file)
@@ -17,7 +17,9 @@ import static org.mockito.Mockito.mock;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
@@ -268,10 +270,12 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
         shardBuilder.setDataTree(shardDataTree.getDataTree());
 
         Entity entity1 = new Entity(ENTITY_TYPE, "one");
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-1"), shardDataTree);
         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
                 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
         verifyGetOwnershipState(service, entity1, true, true);
 
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-2"), shardDataTree);
         writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
                 shardDataTree);
         verifyGetOwnershipState(service, entity1, false, true);
@@ -284,10 +288,14 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
         Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
         assertEquals("getOwnershipState present", false, state.isPresent());
 
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getId(), "member-1"), shardDataTree);
         writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
                 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
         verifyGetOwnershipState(service, entity2, false, false);
 
+        deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getId()), "member-1"), shardDataTree);
+        Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
+        assertEquals("getOwnershipState present", false, state2.isPresent());
         service.close();
     }