ee4f4d667379a62dec3d4566d636fb58cb86f13c
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / utils / ElanClusterUtils.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.elan.utils;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
14 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
15 import org.opendaylight.vpnservice.utils.SystemPropertyReader;
16 import org.opendaylight.vpnservice.utils.clustering.ClusteringUtils;
17 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundConstants;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.util.List;
22 import java.util.concurrent.Callable;
23
24 public class ElanClusterUtils {
25     private static final Logger logger = LoggerFactory.getLogger(ElanClusterUtils.class);
26
27     private static EntityOwnershipService eos;
28
29     static DataStoreJobCoordinator dataStoreJobCoordinator;
30
31     public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
32         dataStoreJobCoordinator = ds;
33     }
34
35     public static void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
36         eos = entityOwnershipService;
37     }
38
39     public static void runOnlyInLeaderNode(Runnable job) {
40         runOnlyInLeaderNode(job, "");
41     }
42
43     public static void runOnlyInLeaderNode(final Runnable job, final String jobDescription) {
44         ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
45                 eos, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
46                 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
47         Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
48             @Override
49             public void onSuccess(Boolean isOwner) {
50                 if (isOwner) {
51                     job.run();
52                 } else {
53                     logger.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
54                 }
55             }
56             @Override
57             public void onFailure(Throwable error) {
58                 logger.error("Failed to identity cluster owner ", error);
59             }
60         });
61     }
62
63     public static void runOnlyInLeaderNode(String jobKey, Callable<List<ListenableFuture<Void>>> dataStoreJob) {
64         runOnlyInLeaderNode(jobKey, "", dataStoreJob);
65     }
66
67     public static void runOnlyInLeaderNode(final String jobKey, final String jobDescription,
68                                            final Callable<List<ListenableFuture<Void>>> dataStoreJob) {
69         ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
70                 eos, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
71                 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
72         Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
73             @Override
74             public void onSuccess(Boolean isOwner) {
75                 if (isOwner) {
76                     logger.trace("scheduling job {} ", jobDescription);
77                     dataStoreJobCoordinator.enqueueJob(jobKey, dataStoreJob,
78                             SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
79                 } else {
80                     logger.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
81                 }
82             }
83             @Override
84             public void onFailure(Throwable error) {
85                 logger.error("Failed to identity cluster owner for job "+jobDescription, error);
86             }
87         });
88     }
89 }