8a836bffb9962b760f1bd45362b8555fc9c5f3f8
[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 /**
15  * Represents a snapshot of the raft data.
16  *
17  * @author Moiz Raja
18  * @author Thomas Pantelis
19  */
20 public class Snapshot implements Serializable {
21     private static final long serialVersionUID = -8298574936724056236L;
22
23     private final byte[] state;
24     private final List<ReplicatedLogEntry> unAppliedEntries;
25     private final long lastIndex;
26     private final long lastTerm;
27     private final long lastAppliedIndex;
28     private final long lastAppliedTerm;
29     private final long electionTerm;
30     private final String electionVotedFor;
31     private final ServerConfigurationPayload serverConfig;
32
33     private Snapshot(byte[] state, List<ReplicatedLogEntry> unAppliedEntries, long lastIndex, long lastTerm,
34             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
35             ServerConfigurationPayload serverConfig) {
36         this.state = state;
37         this.unAppliedEntries = unAppliedEntries;
38         this.lastIndex = lastIndex;
39         this.lastTerm = lastTerm;
40         this.lastAppliedIndex = lastAppliedIndex;
41         this.lastAppliedTerm = lastAppliedTerm;
42         this.electionTerm = electionTerm;
43         this.electionVotedFor = electionVotedFor;
44         this.serverConfig = serverConfig;
45     }
46
47     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
48             long lastAppliedIndex, long lastAppliedTerm) {
49         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, -1, null, 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         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
55                 electionTerm, electionVotedFor, null);
56     }
57
58     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
59             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
60             ServerConfigurationPayload serverConfig) {
61         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
62                 electionTerm, electionVotedFor, serverConfig);
63     }
64
65     public byte[] getState() {
66         return state;
67     }
68
69     public List<ReplicatedLogEntry> getUnAppliedEntries() {
70         return unAppliedEntries;
71     }
72
73     public long getLastTerm() {
74         return lastTerm;
75     }
76
77     public long getLastAppliedIndex() {
78         return lastAppliedIndex;
79     }
80
81     public long getLastAppliedTerm() {
82         return lastAppliedTerm;
83     }
84
85     public long getLastIndex() {
86         return this.lastIndex;
87     }
88
89     public long getElectionTerm() {
90         return electionTerm;
91     }
92
93
94     public String getElectionVotedFor() {
95         return electionVotedFor;
96     }
97
98     public ServerConfigurationPayload getServerConfiguration() {
99         return serverConfig;
100     }
101
102     @Override
103     public String toString() {
104         return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
105                 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
106                 + ", state size=" + state.length + ", electionTerm=" + electionTerm + ", electionVotedFor=" + electionVotedFor
107                 + ", ServerConfigPayload="  + serverConfig + "]";
108     }
109 }