77678e9f2423a2e8956e33a2483618d3b0d1d4dd
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorLeadershipTransferCohort.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.raft;
9
10 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
11 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
12
13 /**
14  * A helper class that participates in raft actor leadership transfer. An instance is created upon
15  * initialization of leadership transfer.
16  * <p>
17  * NOTE: All methods on this class must be called on the actor's thread dispatcher as they modify internal state.
18  *
19  * @author Thomas Pantelis
20  */
21 public abstract class RaftActorLeadershipTransferCohort {
22     private final RaftActor raftActor;
23
24     protected RaftActorLeadershipTransferCohort(RaftActor raftActor) {
25         this.raftActor = raftActor;
26     }
27
28     /**
29      * This method is invoked to start leadership transfer.
30      */
31     public void startTransfer() {
32         RaftActorBehavior behavior = raftActor.getCurrentBehavior();
33         if(behavior instanceof Leader) {
34             ((Leader)behavior).transferLeadership(this);
35         }
36     }
37
38     /**
39      * This method is invoked to abort leadership transfer.
40      */
41     public void abortTransfer() {
42         transferComplete();
43     }
44
45     /**
46      * This method is invoked when leadership transfer is complete.
47      */
48     public abstract void transferComplete();
49 }