L2 Gw create changes related to ITM Tunnels creation in neutronvpn module
[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     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());
34     }
35
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());
40     }
41
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;
50     }
51
52     private static class CheckEntityOwnerTask implements Callable<List<ListenableFuture<Void>>> {
53         EntityOwnershipService entityOwnershipService;
54         Entity entity;
55         SettableFuture<Boolean> checkNodeEntityfuture;
56         long sleepBetweenRetries;
57         int retries;
58
59         public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity,
60                 SettableFuture<Boolean> checkNodeEntityfuture, long sleepBetweenRetries, int retries) {
61             this.entityOwnershipService = entityOwnershipService;
62             this.entity = entity;
63             this.checkNodeEntityfuture = checkNodeEntityfuture;
64             this.sleepBetweenRetries = sleepBetweenRetries;
65             this.retries = retries;
66         }
67
68         @Override
69         public List<ListenableFuture<Void>> call() throws Exception {
70             while (retries > 0) {
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();
78                     }
79                 }
80                 Thread.sleep(sleepBetweenRetries);
81             }
82             checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present"));
83             return getResultFuture();
84         }
85
86         private List<ListenableFuture<Void>> getResultFuture() {
87             ListenableFuture<Void> future = Futures.immediateFuture(null);
88             ArrayList<ListenableFuture<Void>> futureList = Lists.newArrayList();
89             futureList.add(future);
90             return futureList;
91         }
92     }
93 }