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
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyWrapper.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyWrapper.java
new file mode 100644 (file)
index 0000000..da3a4e0
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. 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.controller.cluster.datastore.entityownership.selectionstrategy;
+
+import akka.actor.ActorRef;
+import akka.actor.Cancellable;
+import akka.actor.Scheduler;
+import com.google.common.base.Preconditions;
+import java.util.Collection;
+import java.util.concurrent.TimeUnit;
+import org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import scala.concurrent.ExecutionContextExecutor;
+import scala.concurrent.duration.FiniteDuration;
+
+/**
+ * The EntityOwnerSelectionStrategyWrapper is an EntityOwnerSelectionStrategy decorator that adds the ability to
+ * schedule an owner selection job.
+ */
+public class EntityOwnerSelectionStrategyWrapper implements EntityOwnerSelectionStrategy {
+    private final Scheduler scheduler;
+    private final ActorRef shard;
+    private final ExecutionContextExecutor dispatcher;
+    private final EntityOwnerSelectionStrategy strategy;
+
+    private Cancellable lastScheduledTask;
+
+    public EntityOwnerSelectionStrategyWrapper(Scheduler scheduler,
+                                                ActorRef shard,
+                                                ExecutionContextExecutor dispatcher,
+                                                EntityOwnerSelectionStrategy strategy) {
+        this.scheduler = Preconditions.checkNotNull(scheduler);
+        this.shard = Preconditions.checkNotNull(shard);
+        this.dispatcher = Preconditions.checkNotNull(dispatcher);
+        this.strategy = Preconditions.checkNotNull(strategy);
+    }
+
+    /**
+     * Schedule a new owner selection job. Cancelling any outstanding job if it has not been cancelled.
+     *
+     * @param entityPath
+     * @param allCandidates
+     */
+    public void scheduleOwnerSelection(YangInstanceIdentifier entityPath, Collection<String> allCandidates){
+        if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
+            lastScheduledTask.cancel();
+        }
+        lastScheduledTask = scheduler.scheduleOnce(
+                FiniteDuration.apply(strategy.selectionDelayInMillis(), TimeUnit.MILLISECONDS)
+                , shard, new SelectOwner(entityPath, allCandidates, strategy)
+                , dispatcher, shard);
+    }
+
+    @Override
+    public long selectionDelayInMillis(){
+        return strategy.selectionDelayInMillis();
+    }
+
+    @Override
+    public String newOwner(Collection<String> viableCandidates){
+        return strategy.newOwner(viableCandidates);
+    }
+}
\ No newline at end of file