Fixup checkstyle
[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.persisted.UpdateElectionTerm;
12 import org.slf4j.Logger;
13
14 /**
15  * Implementation of ElectionTerm for the RaftActor.
16  */
17 class ElectionTermImpl implements ElectionTerm {
18     private long currentTerm = 0;
19     private String votedFor = null;
20
21     private final DataPersistenceProvider persistence;
22
23     private final Logger log;
24     private final String logId;
25
26     ElectionTermImpl(DataPersistenceProvider persistence, String logId, Logger log) {
27         this.persistence = persistence;
28         this.logId = logId;
29         this.log = log;
30     }
31
32     @Override
33     public long getCurrentTerm() {
34         return currentTerm;
35     }
36
37     @Override
38     public String getVotedFor() {
39         return votedFor;
40     }
41
42     @Override
43     public void update(long newTerm, String newVotedFor) {
44         log.debug("{}: Set currentTerm={}, votedFor={}", logId, newTerm, newVotedFor);
45         this.currentTerm = newTerm;
46         this.votedFor = newVotedFor;
47     }
48
49     @Override
50     public void updateAndPersist(long newTerm, String newVotedFor) {
51         update(newTerm, newVotedFor);
52         // FIXME : Maybe first persist then update the state
53         persistence.persist(new UpdateElectionTerm(this.currentTerm, this.votedFor), NoopProcedure.instance());
54     }
55 }