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 static DataStoreJobCoordinator dataStoreJobCoordinator;
32 static DataStoreJobCoordinator getDataStoreJobCoordinator() {
33 if (dataStoreJobCoordinator == null) {
34 dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
36 return dataStoreJobCoordinator;
38 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
39 dataStoreJobCoordinator = ds;
42 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
43 String entityType, String nodeId) {
44 return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId),
45 SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries());
48 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
49 String entityType, YangInstanceIdentifier nodeId) {
50 return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId),
51 SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries());
54 public static ListenableFuture<Boolean> checkNodeEntityOwner(EntityOwnershipService entityOwnershipService,
55 Entity entity, long sleepBetweenRetries, int maxRetries) {
56 SettableFuture<Boolean> checkNodeEntityfuture = SettableFuture.create();
57 CheckEntityOwnerTask checkEntityOwnerTask = new CheckEntityOwnerTask(entityOwnershipService, entity,
58 checkNodeEntityfuture, sleepBetweenRetries, maxRetries);
59 getDataStoreJobCoordinator().enqueueJob(entityOwnershipService.toString(), checkEntityOwnerTask);
60 return checkNodeEntityfuture;
63 private static class CheckEntityOwnerTask implements Callable<List<ListenableFuture<Void>>> {
64 EntityOwnershipService entityOwnershipService;
66 SettableFuture<Boolean> checkNodeEntityfuture;
67 long sleepBetweenRetries;
70 public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity,
71 SettableFuture<Boolean> checkNodeEntityfuture, long sleepBetweenRetries, int retries) {
72 this.entityOwnershipService = entityOwnershipService;
74 this.checkNodeEntityfuture = checkNodeEntityfuture;
75 this.sleepBetweenRetries = sleepBetweenRetries;
76 this.retries = retries;
80 public List<ListenableFuture<Void>> call() throws Exception {
82 retries = retries - 1;
83 Optional<EntityOwnershipState> entityState = entityOwnershipService.getOwnershipState(entity);
84 if (entityState.isPresent()) {
85 EntityOwnershipState entityOwnershipState = entityState.get();
86 if (entityOwnershipState.hasOwner()) {
87 checkNodeEntityfuture.set(entityOwnershipState.isOwner());
88 return getResultFuture();
91 Thread.sleep(sleepBetweenRetries);
93 checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present"));
94 return getResultFuture();
97 private List<ListenableFuture<Void>> getResultFuture() {
98 ListenableFuture<Void> future = Futures.immediateFuture(null);
99 ArrayList<ListenableFuture<Void>> futureList = Lists.newArrayList();
100 futureList.add(future);