Further Guava Optional cleanups
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ElectionTermImpl.java
index e75e0c5bb61f2e35881242df7206c2bb0a4e1704..e123a64874e84b925fa456f145b28ab508b547ff 100644 (file)
@@ -1,47 +1,55 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco 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 java.util.concurrent.atomic.AtomicLong;
-
-public class ElectionTermImpl implements ElectionTerm{
-    /**
-     * Identifier of the actor whose election term information this is
-     */
-    private final String id;
-
-    private AtomicLong currentTerm;
+import org.opendaylight.controller.cluster.DataPersistenceProvider;
+import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm;
+import org.slf4j.Logger;
 
-    private String votedFor;
+/**
+ * Implementation of ElectionTerm for the RaftActor.
+ */
+class ElectionTermImpl implements ElectionTerm {
+    private long currentTerm = 0;
+    private String votedFor = null;
 
-    public ElectionTermImpl(String id) {
-        this.id = id;
+    private final DataPersistenceProvider persistence;
 
-        // TODO: Read currentTerm from some persistent state
-        currentTerm = new AtomicLong(0);
+    private final Logger log;
+    private final String logId;
 
-        // TODO: Read votedFor from some file
-        votedFor = "";
+    ElectionTermImpl(DataPersistenceProvider persistence, String logId, Logger log) {
+        this.persistence = persistence;
+        this.logId = logId;
+        this.log = log;
     }
 
-    public AtomicLong getCurrentTerm() {
+    @Override
+    public long getCurrentTerm() {
         return currentTerm;
     }
 
+    @Override
     public String getVotedFor() {
         return votedFor;
     }
 
-    public void update(long currentTerm, String votedFor){
-        this.currentTerm.set(currentTerm);
-        this.votedFor = votedFor;
+    @Override
+    public void update(long newTerm, String newVotedFor) {
+        log.debug("{}: Set currentTerm={}, votedFor={}", logId, newTerm, newVotedFor);
+        this.currentTerm = newTerm;
+        this.votedFor = newVotedFor;
+    }
 
-        // TODO : Write to some persistent state
+    @Override
+    public void updateAndPersist(long newTerm, String newVotedFor) {
+        update(newTerm, newVotedFor);
+        // FIXME : Maybe first persist then update the state
+        persistence.persist(new UpdateElectionTerm(this.currentTerm, this.votedFor), NoopProcedure.instance());
     }
 }