BUG 4615 : Add method on EOS to check if a candidate is registered locally 25/29525/1
authorMoiz Raja <moraja@cisco.com>
Wed, 11 Nov 2015 00:05:28 +0000 (16:05 -0800)
committerMoiz Raja <moraja@cisco.com>
Wed, 11 Nov 2015 00:05:28 +0000 (16:05 -0800)
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);
+
+    /**
+     * 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 c94c7623c060a4745c70eee19028bd3e39fc5d78..a54cd99466d8abd01134aeb1307ae706dd2b44c8 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 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;
@@ -192,6 +193,11 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
         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 {
index 2ca3caf5e2fef3ec5bb0da226e9fc3cd36e053f3..cac7b650c85c4d5e11f4d80bf7061af305922537 100644 (file)
@@ -8,6 +8,7 @@
 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;
@@ -281,6 +282,27 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
         service.close();
     }
 
+    @Test
+    public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
+        final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+        DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
+            @Override
+            protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
+                return shardPropsCreator;
+            }
+        };
+
+        service.start();
+
+        final Entity test = new Entity("test-type", "test");
+
+        assertFalse(service.isCandidateRegistered(test));
+
+        service.registerCandidate(test);
+
+        assertTrue(service.isCandidateRegistered(test));
+    }
+
     private void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
             boolean isOwner, boolean hasOwner) {
         Optional<EntityOwnershipState> state = service.getOwnershipState(entity);