2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import org.opendaylight.controller.cluster.DataPersistenceProvider;
11 import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm;
12 import org.slf4j.Logger;
15 * Implementation of ElectionTerm for the RaftActor.
17 class ElectionTermImpl implements ElectionTerm {
18 private long currentTerm = 0;
19 private String votedFor = null;
21 private final DataPersistenceProvider persistence;
23 private final Logger log;
24 private final String logId;
26 ElectionTermImpl(DataPersistenceProvider persistence, String logId, Logger log) {
27 this.persistence = persistence;
33 public long getCurrentTerm() {
38 public String getVotedFor() {
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;
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());