Add RaftActorLeadershipTransferCohort and implement transfer in Leader
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorLeadershipTransferCohort.java
diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java
new file mode 100644 (file)
index 0000000..77678e9
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2015 Brocade Communications 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.raft;
+
+import org.opendaylight.controller.cluster.raft.behaviors.Leader;
+import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
+
+/**
+ * A helper class that participates in raft actor leadership transfer. An instance is created upon
+ * initialization of leadership transfer.
+ * <p>
+ * NOTE: All methods on this class must be called on the actor's thread dispatcher as they modify internal state.
+ *
+ * @author Thomas Pantelis
+ */
+public abstract class RaftActorLeadershipTransferCohort {
+    private final RaftActor raftActor;
+
+    protected RaftActorLeadershipTransferCohort(RaftActor raftActor) {
+        this.raftActor = raftActor;
+    }
+
+    /**
+     * This method is invoked to start leadership transfer.
+     */
+    public void startTransfer() {
+        RaftActorBehavior behavior = raftActor.getCurrentBehavior();
+        if(behavior instanceof Leader) {
+            ((Leader)behavior).transferLeadership(this);
+        }
+    }
+
+    /**
+     * This method is invoked to abort leadership transfer.
+     */
+    public void abortTransfer() {
+        transferComplete();
+    }
+
+    /**
+     * This method is invoked when leadership transfer is complete.
+     */
+    public abstract void transferComplete();
+}