Add Delayed Owner selection base on strategy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyWrapper.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Cancellable;
13 import akka.actor.Scheduler;
14 import com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import scala.concurrent.ExecutionContextExecutor;
20 import scala.concurrent.duration.FiniteDuration;
21
22 /**
23  * The EntityOwnerSelectionStrategyWrapper is an EntityOwnerSelectionStrategy decorator that adds the ability to
24  * schedule an owner selection job.
25  */
26 public class EntityOwnerSelectionStrategyWrapper implements EntityOwnerSelectionStrategy {
27     private final Scheduler scheduler;
28     private final ActorRef shard;
29     private final ExecutionContextExecutor dispatcher;
30     private final EntityOwnerSelectionStrategy strategy;
31
32     private Cancellable lastScheduledTask;
33
34     public EntityOwnerSelectionStrategyWrapper(Scheduler scheduler,
35                                                 ActorRef shard,
36                                                 ExecutionContextExecutor dispatcher,
37                                                 EntityOwnerSelectionStrategy strategy) {
38         this.scheduler = Preconditions.checkNotNull(scheduler);
39         this.shard = Preconditions.checkNotNull(shard);
40         this.dispatcher = Preconditions.checkNotNull(dispatcher);
41         this.strategy = Preconditions.checkNotNull(strategy);
42     }
43
44     /**
45      * Schedule a new owner selection job. Cancelling any outstanding job if it has not been cancelled.
46      *
47      * @param entityPath
48      * @param allCandidates
49      */
50     public void scheduleOwnerSelection(YangInstanceIdentifier entityPath, Collection<String> allCandidates){
51         if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
52             lastScheduledTask.cancel();
53         }
54         lastScheduledTask = scheduler.scheduleOnce(
55                 FiniteDuration.apply(strategy.selectionDelayInMillis(), TimeUnit.MILLISECONDS)
56                 , shard, new SelectOwner(entityPath, allCandidates, strategy)
57                 , dispatcher, shard);
58     }
59
60     @Override
61     public long selectionDelayInMillis(){
62         return strategy.selectionDelayInMillis();
63     }
64
65     @Override
66     public String newOwner(Collection<String> viableCandidates){
67         return strategy.newOwner(viableCandidates);
68     }
69 }