Bug 6587: Retain state when transitioning between Leader and IsolatedLeader
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / Snapshot.java
1 /*
2  * Copyright (c) 2014 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 java.io.Serializable;
11 import java.util.List;
12 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
13
14 public class Snapshot implements Serializable {
15     private static final long serialVersionUID = -8298574936724056236L;
16
17     private final byte[] state;
18     private final List<ReplicatedLogEntry> unAppliedEntries;
19     private final long lastIndex;
20     private final long lastTerm;
21     private final long lastAppliedIndex;
22     private final long lastAppliedTerm;
23     private final long electionTerm;
24     private final String electionVotedFor;
25     private final ServerConfigurationPayload serverConfig;
26
27     private Snapshot(byte[] state, List<ReplicatedLogEntry> unAppliedEntries, long lastIndex, long lastTerm,
28             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
29             ServerConfigurationPayload serverConfig) {
30         this.state = state;
31         this.unAppliedEntries = unAppliedEntries;
32         this.lastIndex = lastIndex;
33         this.lastTerm = lastTerm;
34         this.lastAppliedIndex = lastAppliedIndex;
35         this.lastAppliedTerm = lastAppliedTerm;
36         this.electionTerm = electionTerm;
37         this.electionVotedFor = electionVotedFor;
38         this.serverConfig = serverConfig;
39     }
40
41     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
42             long lastAppliedIndex, long lastAppliedTerm) {
43         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, -1, null, null);
44     }
45
46     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
47             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor) {
48         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
49                 electionTerm, electionVotedFor, null);
50     }
51
52     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
53             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
54             ServerConfigurationPayload serverConfig) {
55         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
56                 electionTerm, electionVotedFor, serverConfig);
57     }
58
59     public byte[] getState() {
60         return state;
61     }
62
63     public List<ReplicatedLogEntry> getUnAppliedEntries() {
64         return unAppliedEntries;
65     }
66
67     public long getLastTerm() {
68         return lastTerm;
69     }
70
71     public long getLastAppliedIndex() {
72         return lastAppliedIndex;
73     }
74
75     public long getLastAppliedTerm() {
76         return lastAppliedTerm;
77     }
78
79     public long getLastIndex() {
80         return this.lastIndex;
81     }
82
83     public long getElectionTerm() {
84         return electionTerm;
85     }
86
87
88     public String getElectionVotedFor() {
89         return electionVotedFor;
90     }
91
92     public ServerConfigurationPayload getServerConfiguration() {
93         return serverConfig;
94     }
95
96     @Override
97     public String toString() {
98         return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
99                 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
100                 + ", state size=" + state.length + ", electionTerm=" + electionTerm + ", electionVotedFor=" + electionVotedFor
101                 + ", ServerConfigPayload="  + serverConfig + "]";
102     }
103 }