2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.vpnservice.utils.clustering;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.Callable;
22 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
24 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
25 import org.opendaylight.vpnservice.utils.SystemPropertyReader;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 public class ClusteringUtils {
30 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
31 String entityType, String nodeId) {
32 return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId),
33 SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries());
36 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
37 String entityType, YangInstanceIdentifier nodeId) {
38 return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId),
39 SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries());
42 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
43 Entity entity, long sleepBetweenRetries, int maxRetries) {
44 SettableFuture<Boolean> checkNodeEntityfuture = SettableFuture.create();
45 DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
46 CheckEntityOwnerTask checkEntityOwnerTask = new CheckEntityOwnerTask(entityOwnershipService, entity,
47 checkNodeEntityfuture, sleepBetweenRetries, maxRetries);
48 dataStoreCoordinator.enqueueJob(entityOwnershipService.toString(), checkEntityOwnerTask);
49 return checkNodeEntityfuture;
52 private static class CheckEntityOwnerTask implements Callable<List<ListenableFuture<Void>>> {
53 EntityOwnershipService entityOwnershipService;
55 SettableFuture<Boolean> checkNodeEntityfuture;
56 long sleepBetweenRetries;
59 public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity,
60 SettableFuture<Boolean> checkNodeEntityfuture, long sleepBetweenRetries, int retries) {
61 this.entityOwnershipService = entityOwnershipService;
63 this.checkNodeEntityfuture = checkNodeEntityfuture;
64 this.sleepBetweenRetries = sleepBetweenRetries;
65 this.retries = retries;
69 public List<ListenableFuture<Void>> call() throws Exception {
71 retries = retries - 1;
72 Optional<EntityOwnershipState> entityState = entityOwnershipService.getOwnershipState(entity);
73 if (entityState.isPresent()) {
74 EntityOwnershipState entityOwnershipState = entityState.get();
75 if (entityOwnershipState.hasOwner()) {
76 checkNodeEntityfuture.set(entityOwnershipState.isOwner());
77 return getResultFuture();
80 Thread.sleep(sleepBetweenRetries);
82 checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present"));
83 return getResultFuture();
86 private List<ListenableFuture<Void>> getResultFuture() {
87 ListenableFuture<Void> future = Futures.immediateFuture(null);
88 ArrayList<ListenableFuture<Void>> futureList = Lists.newArrayList();
89 futureList.add(future);