BUG 4615 : Add method on EOS to check if a candidate is registered locally 31/29631/1
authorMoiz Raja <moraja@cisco.com>
Wed, 11 Nov 2015 00:05:28 +0000 (16:05 -0800)
committerTom Pantelis <tpanteli@brocade.com>
Fri, 13 Nov 2015 02:29:45 +0000 (21:29 -0500)
Change-Id: Iedb2e4cf92553910cf5e1bd85978f88e10bf3c25
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/sal-common-api/src/main/java/org/opendaylight/controller/md/sal/common/api/clustering/EntityOwnershipService.java
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/DistributedEntityOwnershipServiceTest.java

index 311d1b8595565eb2390b9aef15db10644d041514..a042bf5ef2fdfd71ac2d9b82f87f641e95eca6da 100644 (file)
@@ -58,4 +58,12 @@ public interface EntityOwnershipService {
      * @return an Optional EntityOwnershipState whose instance is present if the entity is found
      */
     Optional<EntityOwnershipState> getOwnershipState(@Nonnull Entity forEntity);
      * @return an Optional EntityOwnershipState whose instance is present if the entity is found
      */
     Optional<EntityOwnershipState> getOwnershipState(@Nonnull Entity forEntity);
+
+    /**
+     * Check if a local candidate is registered for the given entity
+     *
+     * @param entity
+     * @return true if a candidate was registered locally, false otherwise
+     */
+    boolean isCandidateRegistered(@Nonnull Entity entity);
 }
 }
index f1d9b43aba35105c2098bf4910f2736302d9b73c..ec0081d38be2e0469a61aa17ef89919ced413ef5 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Collection;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
 import org.opendaylight.controller.cluster.datastore.config.Configuration;
 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
 import org.opendaylight.controller.cluster.datastore.config.Configuration;
 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
@@ -192,6 +193,11 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
         return Optional.of(new EntityOwnershipState(isOwner, hasOwner));
     }
 
         return Optional.of(new EntityOwnershipState(isOwner, hasOwner));
     }
 
+    @Override
+    public boolean isCandidateRegistered(@Nonnull Entity entity) {
+        return registeredEntities.get(entity) != null;
+    }
+
     private DataTree getLocalEntityOwnershipShardDataTree() {
         if(localEntityOwnershipShardDataTree == null) {
             try {
     private DataTree getLocalEntityOwnershipShardDataTree() {
         if(localEntityOwnershipShardDataTree == null) {
             try {
index 084c5e79db447b04bf464dddb67406ac90fa1dfa..9608b812e402fc419299e6804890ac0a26f0764b 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
 import static org.junit.Assert.assertEquals;
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -290,6 +291,30 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
         service.close();
     }
 
         service.close();
     }
 
+    @Test
+    public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
+        final TestShardBuilder shardBuilder = new TestShardBuilder();
+        DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore,
+                EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
+            @Override
+            protected EntityOwnershipShard.Builder newShardBuilder() {
+                return shardBuilder;
+            }
+        };
+
+        service.start();
+
+        final Entity test = new Entity("test-type", "test");
+
+        assertFalse(service.isCandidateRegistered(test));
+
+        service.registerCandidate(test);
+
+        assertTrue(service.isCandidateRegistered(test));
+
+        service.close();
+    }
+
     private static void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
             boolean isOwner, boolean hasOwner) {
         Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
     private static void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
             boolean isOwner, boolean hasOwner) {
         Optional<EntityOwnershipState> state = service.getOwnershipState(entity);