/* * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.vpnservice.utils.clustering; import com.google.common.base.Optional; import com.google.common.collect.Lists; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import org.opendaylight.controller.md.sal.common.api.clustering.Entity; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState; import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator; import org.opendaylight.vpnservice.utils.SystemPropertyReader; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; public class ClusteringUtils { static DataStoreJobCoordinator dataStoreJobCoordinator; static DataStoreJobCoordinator getDataStoreJobCoordinator() { if (dataStoreJobCoordinator == null) { dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance(); } return dataStoreJobCoordinator; } public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) { dataStoreJobCoordinator = ds; } public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, String entityType, String nodeId) { return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId), SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries()); } public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, String entityType, YangInstanceIdentifier nodeId) { return checkNodeEntityOwner(entityOwnershipService, new Entity(entityType, nodeId), SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(), SystemPropertyReader.Cluster.getMaxRetries()); } public static ListenableFuture checkNodeEntityOwner(EntityOwnershipService entityOwnershipService, Entity entity, long sleepBetweenRetries, int maxRetries) { SettableFuture checkNodeEntityfuture = SettableFuture.create(); CheckEntityOwnerTask checkEntityOwnerTask = new CheckEntityOwnerTask(entityOwnershipService, entity, checkNodeEntityfuture, sleepBetweenRetries, maxRetries); getDataStoreJobCoordinator().enqueueJob(entityOwnershipService.toString(), checkEntityOwnerTask); return checkNodeEntityfuture; } private static class CheckEntityOwnerTask implements Callable>> { EntityOwnershipService entityOwnershipService; Entity entity; SettableFuture checkNodeEntityfuture; long sleepBetweenRetries; int retries; public CheckEntityOwnerTask(EntityOwnershipService entityOwnershipService, Entity entity, SettableFuture checkNodeEntityfuture, long sleepBetweenRetries, int retries) { this.entityOwnershipService = entityOwnershipService; this.entity = entity; this.checkNodeEntityfuture = checkNodeEntityfuture; this.sleepBetweenRetries = sleepBetweenRetries; this.retries = retries; } @Override public List> call() throws Exception { while (retries > 0) { retries = retries - 1; Optional entityState = entityOwnershipService.getOwnershipState(entity); if (entityState.isPresent()) { EntityOwnershipState entityOwnershipState = entityState.get(); if (entityOwnershipState.hasOwner()) { checkNodeEntityfuture.set(entityOwnershipState.isOwner()); return getResultFuture(); } } Thread.sleep(sleepBetweenRetries); } checkNodeEntityfuture.setException(new EntityOwnerNotPresentException("Entity Owner Not Present")); return getResultFuture(); } private List> getResultFuture() { ListenableFuture future = Futures.immediateFuture(null); ArrayList> futureList = Lists.newArrayList(); futureList.add(future); return futureList; } } }