X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=vpnservice.git;a=blobdiff_plain;f=mdsalutil%2Fmdsalutil-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fvpnservice%2Futils%2Fclustering%2FClusteringUtils.java;h=311efac7983d77d16a22f8d6d79a478b4b832966;hp=ec90e2c59fbe37125321fc3a27e2f2c0ea7f5157;hb=2ef3711b19264a9edfa95713d7bb33b79b699712;hpb=769edd5c7e8cfa0a13a2f8e442270978f649b83f diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/utils/clustering/ClusteringUtils.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/utils/clustering/ClusteringUtils.java index ec90e2c5..311efac7 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/utils/clustering/ClusteringUtils.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/utils/clustering/ClusteringUtils.java @@ -8,30 +8,86 @@ package org.opendaylight.vpnservice.utils.clustering; import com.google.common.base.Optional; +import com.google.common.collect.Lists; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; + import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; + import org.opendaylight.controller.md.sal.common.api.clustering.Entity; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState; +import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator; +import org.opendaylight.vpnservice.utils.SystemPropertyReader; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; public class ClusteringUtils { - public static boolean isNodeEntityOwner(EntityOwnershipService entityOwnershipService, String entityType, - String nodeId) { - Entity entity = new Entity(entityType, nodeId); - Optional entityState = entityOwnershipService.getOwnershipState(entity); - if (entityState.isPresent()) { - return entityState.get().isOwner(); - } - return false; + public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, + String entityType, String nodeId) { + return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId), + SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries()); } - public static boolean isNodeEntityOwner(EntityOwnershipService entityOwnershipService, String entityType, - YangInstanceIdentifier nodeId) { - Entity entity = new Entity(entityType, nodeId); - Optional entityState = entityOwnershipService.getOwnershipState(entity); - if (entityState.isPresent()) { - return entityState.get().isOwner(); + public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, + String entityType, YangInstanceIdentifier nodeId) { + return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId), + SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries()); + } + + public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, + Entity entity, long sleepBetweenRetries, int maxRetries) { + SettableFuture checkNodeEntityfuture = SettableFuture.create(); + DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance(); + CheckEntityOwnerTask checkEntityOwnerTask = new CheckEntityOwnerTask(entityOwnershipService, entity, + checkNodeEntityfuture, sleepBetweenRetries, maxRetries); + dataStoreCoordinator.enqueueJob(entityOwnershipService.toString(), checkEntityOwnerTask); + return checkNodeEntityfuture; + } + + private static class CheckEntityOwnerTask implements Callable>> { + EntityOwnershipService entityOwnershipService; + Entity entity; + SettableFuture checkNodeEntityfuture; + long sleepBetweenRetries; + int retries; + + public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity, + SettableFuture checkNodeEntityfuture, long sleepBetweenRetries, int retries) { + this.entityOwnershipService = entityOwnershipService; + this.entity = entity; + this.checkNodeEntityfuture = checkNodeEntityfuture; + this.sleepBetweenRetries = sleepBetweenRetries; + this.retries = retries; + } + + @Override + public List> call() throws Exception { + while (retries > 0) { + retries = retries - 1; + Optional entityState = entityOwnershipService.getOwnershipState(entity); + if (entityState.isPresent()) { + EntityOwnershipState entityOwnershipState = entityState.get(); + if (entityOwnershipState.hasOwner()) { + checkNodeEntityfuture.set(entityOwnershipState.isOwner()); + return getResultFuture(); + } + } + Thread.sleep(sleepBetweenRetries); + } + checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present")); + return getResultFuture(); + } + + private List> getResultFuture() { + ListenableFuture future = Futures.immediateFuture(null); + ArrayList> futureList = Lists.newArrayList(); + futureList.add(future); + return futureList; } - return false; } }