Fix FindBugs warnings in sal-akk-raft
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Serializable;
12 import java.util.List;
13 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
14
15 /**
16  * Represents a snapshot of the raft data.
17  *
18  * @author Moiz Raja
19  * @author Thomas Pantelis
20  */
21 public class Snapshot implements Serializable {
22     private static final long serialVersionUID = -8298574936724056236L;
23
24     private final byte[] state;
25     private final List<ReplicatedLogEntry> unAppliedEntries;
26     private final long lastIndex;
27     private final long lastTerm;
28     private final long lastAppliedIndex;
29     private final long lastAppliedTerm;
30     private final long electionTerm;
31     private final String electionVotedFor;
32     private final ServerConfigurationPayload serverConfig;
33
34     private Snapshot(byte[] state, List<ReplicatedLogEntry> unAppliedEntries, long lastIndex, long lastTerm,
35             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
36             ServerConfigurationPayload serverConfig) {
37         this.state = state;
38         this.unAppliedEntries = unAppliedEntries;
39         this.lastIndex = lastIndex;
40         this.lastTerm = lastTerm;
41         this.lastAppliedIndex = lastAppliedIndex;
42         this.lastAppliedTerm = lastAppliedTerm;
43         this.electionTerm = electionTerm;
44         this.electionVotedFor = electionVotedFor;
45         this.serverConfig = serverConfig;
46     }
47
48     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
49             long lastAppliedIndex, long lastAppliedTerm) {
50         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, -1, null, null);
51     }
52
53     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
54             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor) {
55         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
56                 electionTerm, electionVotedFor, null);
57     }
58
59     public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
60             long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
61             ServerConfigurationPayload serverConfig) {
62         return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
63                 electionTerm, electionVotedFor, serverConfig);
64     }
65
66     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
67             + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
68             + "Also it would be inefficient to create a return copy as the byte[] could be large.")
69     public byte[] getState() {
70         return state;
71     }
72
73     public List<ReplicatedLogEntry> getUnAppliedEntries() {
74         return unAppliedEntries;
75     }
76
77     public long getLastTerm() {
78         return lastTerm;
79     }
80
81     public long getLastAppliedIndex() {
82         return lastAppliedIndex;
83     }
84
85     public long getLastAppliedTerm() {
86         return lastAppliedTerm;
87     }
88
89     public long getLastIndex() {
90         return this.lastIndex;
91     }
92
93     public long getElectionTerm() {
94         return electionTerm;
95     }
96
97
98     public String getElectionVotedFor() {
99         return electionVotedFor;
100     }
101
102     public ServerConfigurationPayload getServerConfiguration() {
103         return serverConfig;
104     }
105
106     @Override
107     public String toString() {
108         return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
109                 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
110                 + ", state size=" + state.length + ", electionTerm=" + electionTerm + ", electionVotedFor="
111                 + electionVotedFor + ", ServerConfigPayload="  + serverConfig + "]";
112     }
113 }