Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / utils / clustering / ClusteringUtils.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.utils.clustering;
9
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;
15
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.Callable;
21
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;
27
28 public class ClusteringUtils {
29
30     static DataStoreJobCoordinator dataStoreJobCoordinator;
31
32     static DataStoreJobCoordinator getDataStoreJobCoordinator() {
33         if (dataStoreJobCoordinator == null) {
34             dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
35         }
36         return dataStoreJobCoordinator;
37     }
38     public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
39         dataStoreJobCoordinator = ds;
40     }
41
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());
46     }
47
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());
52     }
53
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;
61     }
62
63     private static class CheckEntityOwnerTask implements Callable<List<ListenableFuture<Void>>> {
64         EntityOwnershipService entityOwnershipService;
65         Entity entity;
66         SettableFuture<Boolean> checkNodeEntityfuture;
67         long sleepBetweenRetries;
68         int retries;
69
70         public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity,
71                 SettableFuture<Boolean> checkNodeEntityfuture, long sleepBetweenRetries, int retries) {
72             this.entityOwnershipService = entityOwnershipService;
73             this.entity = entity;
74             this.checkNodeEntityfuture = checkNodeEntityfuture;
75             this.sleepBetweenRetries = sleepBetweenRetries;
76             this.retries = retries;
77         }
78
79         @Override
80         public List<ListenableFuture<Void>> call() throws Exception {
81             while (retries > 0) {
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();
89                     }
90                 }
91                 Thread.sleep(sleepBetweenRetries);
92             }
93             checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present"));
94             return getResultFuture();
95         }
96
97         private List<ListenableFuture<Void>> getResultFuture() {
98             ListenableFuture<Void> future = Futures.immediateFuture(null);
99             ArrayList<ListenableFuture<Void>> futureList = Lists.newArrayList();
100             futureList.add(future);
101             return futureList;
102         }
103     }
104 }