8e2e3d507d1752fe470d0da778e4d62c11ba2fa5
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ElectionTermImpl.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 package org.opendaylight.controller.cluster.raft;
9
10 import org.opendaylight.controller.cluster.DataPersistenceProvider;
11 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
12 import org.slf4j.Logger;
13
14 /**
15  * Implementation of ElectionTerm for the RaftActor.
16  */
17 class ElectionTermImpl implements ElectionTerm {
18     /**
19      * Identifier of the actor whose election term information this is
20      */
21     private long currentTerm = 0;
22     private String votedFor = null;
23
24     private final DataPersistenceProvider persistence;
25
26     private final Logger log;
27     private final String logId;
28
29     ElectionTermImpl(DataPersistenceProvider persistence, String logId, Logger log) {
30         this.persistence = persistence;
31         this.logId = logId;
32         this.log = log;
33     }
34
35     @Override
36     public long getCurrentTerm() {
37         return currentTerm;
38     }
39
40     @Override
41     public String getVotedFor() {
42         return votedFor;
43     }
44
45     @Override public void update(long currentTerm, String votedFor) {
46         if(log.isDebugEnabled()) {
47             log.debug("{}: Set currentTerm={}, votedFor={}", logId, currentTerm, votedFor);
48         }
49         this.currentTerm = currentTerm;
50         this.votedFor = votedFor;
51     }
52
53     @Override
54     public void updateAndPersist(long currentTerm, String votedFor){
55         update(currentTerm, votedFor);
56         // FIXME : Maybe first persist then update the state
57         persistence.persist(new UpdateElectionTerm(this.currentTerm, this.votedFor), NoopProcedure.instance());
58     }
59 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.