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.elan.utils;
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;
21 import java.util.List;
22 import java.util.concurrent.Callable;
24 public class ElanClusterUtils {
25 private static final Logger logger = LoggerFactory.getLogger(ElanClusterUtils.class);
27 private static EntityOwnershipService eos;
29 static DataStoreJobCoordinator dataStoreJobCoordinator;
31 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
32 dataStoreJobCoordinator = ds;
35 public static void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
36 eos = entityOwnershipService;
39 public static void runOnlyInLeaderNode(Runnable job) {
40 runOnlyInLeaderNode(job, "");
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>() {
49 public void onSuccess(Boolean isOwner) {
53 logger.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
57 public void onFailure(Throwable error) {
58 logger.error("Failed to identity cluster owner ", error);
63 public static void runOnlyInLeaderNode(String jobKey, Callable<List<ListenableFuture<Void>>> dataStoreJob) {
64 runOnlyInLeaderNode(jobKey, "", dataStoreJob);
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>() {
74 public void onSuccess(Boolean isOwner) {
76 logger.trace("scheduling job {} ", jobDescription);
77 dataStoreJobCoordinator.enqueueJob(jobKey, dataStoreJob,
78 SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
80 logger.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
84 public void onFailure(Throwable error) {
85 logger.error("Failed to identity cluster owner for job "+jobDescription, error);