2 * Copyright (c) 2017 Inocybe Technologies 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.genius.utils.clustering;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.concurrent.Callable;
17 import javax.inject.Inject;
18 import org.opendaylight.genius.utils.SystemPropertyReader;
19 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
20 import org.opendaylight.mdsal.eos.binding.api.Entity;
21 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
22 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * EntityOwnershipService utilities.
29 * @author Thomas Pantelis
31 public class EntityOwnershipUtils {
32 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnershipUtils.class);
34 private final EntityOwnershipService entityOwnershipService;
37 public EntityOwnershipUtils(EntityOwnershipService entityOwnershipService) {
38 this.entityOwnershipService = Objects.requireNonNull(entityOwnershipService);
41 public EntityOwnershipService getEntityOwnershipService() {
42 return entityOwnershipService;
46 * Checks if the local node is the owner of an entity.
48 * @param entityType the entity type
49 * @param entityName the entity name
50 * @return true is the owner, false otherwise
52 public boolean isEntityOwner(String entityType, String entityName) {
53 return isEntityOwner(new Entity(entityType, entityName));
57 * Checks if the local node is the owner of an entity.
59 * @param entity the entity
60 * @return true is the owner, false otherwise
62 public boolean isEntityOwner(Entity entity) {
63 return isEntityOwner(entity, SystemPropertyReader.Cluster.getSleepTimeBetweenRetries(),
64 SystemPropertyReader.Cluster.getMaxRetries());
68 * Checks if the local node is the owner of an entity.
70 * @param entity the entity
71 * @param sleepBetweenRetries the busy wait interval in millis if the entity is not yet present
72 * @param tries the total number of busy wait tries
73 * @return true is the owner, false otherwise
75 public boolean isEntityOwner(Entity entity, long sleepBetweenRetries, int tries) {
77 Optional<EntityOwnershipState> entityState = entityOwnershipService.getOwnershipState(entity);
78 if (entityState.isPresent()) {
79 EntityOwnershipState entityOwnershipState = entityState.get();
80 return entityOwnershipState == EntityOwnershipState.IS_OWNER;
85 LOG.debug("EntityOwnershipState for {} is not yet available. {} tries left", entity, tries);
89 Thread.sleep(sleepBetweenRetries);
90 } catch (InterruptedException e) {
102 * Runs a job task if the local node is the owner of an entity.
104 * @param entityType the entity type
105 * @param entityName the entity name
106 * @param coordinator the JobCoordinator on which to run the job task
107 * @param jobDesc description of the job for logging
108 * @param job the job task
110 public void runOnlyInOwnerNode(String entityType, String entityName, JobCoordinator coordinator, String jobDesc,
112 final Entity entity = new Entity(entityType, entityName);
113 coordinator.enqueueJob(entity.toString(), () -> {
114 if (isEntityOwner(entity)) {
115 LOG.debug("Running job {} for {}", jobDesc, entity);
118 LOG.debug("runOnlyInOwnerNode: job {} was not run as I'm not the owner of {} ", jobDesc, entity);
121 return Collections.singletonList(Futures.immediateFuture(null));
126 * Runs a job task if the local node is the owner of an entity.
128 * @param entityType the entity type
129 * @param entityName the entity name
130 * @param coordinator the JobCoordinator on which to run the job
131 * @param jobKey the job key
132 * @param jobDesc description of the job for logging
133 * @param job the job task
135 public void runOnlyInOwnerNode(String entityType, String entityName, JobCoordinator coordinator,
136 String jobKey, String jobDesc, Callable<List<ListenableFuture<Void>>> job) {
137 final Entity entity = new Entity(entityType, entityName);
138 coordinator.enqueueJob(entity.toString(), () -> {
139 if (isEntityOwner(entity)) {
140 LOG.debug("Scheduling job {} for {}", jobDesc, entity);
141 coordinator.enqueueJob(jobKey, job, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
143 LOG.debug("runOnlyInOwnerNode: job {} was not run as I'm not the owner of {} ", jobDesc, entity);
146 return Collections.singletonList(Futures.immediateFuture(null));