Fix FindBugs warnings in sal-akk-raft
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / InstallSnapshot.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
9 package org.opendaylight.controller.cluster.raft.messages;
10
11 import com.google.common.base.Optional;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
18
19 /**
20  * Message sent from a leader to install a snapshot chunk on a follower.
21  */
22 public class InstallSnapshot extends AbstractRaftRPC {
23     private static final long serialVersionUID = 1L;
24
25     private final String leaderId;
26     private final long lastIncludedIndex;
27     private final long lastIncludedTerm;
28     private final byte[] data;
29     private final int chunkIndex;
30     private final int totalChunks;
31     private final Optional<Integer> lastChunkHashCode;
32     private final Optional<ServerConfigurationPayload> serverConfig;
33
34     @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
35             + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
36             + "Also it would be inefficient to create a copy as the byte[] could be large.")
37     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
38             int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode,
39             Optional<ServerConfigurationPayload> serverConfig) {
40         super(term);
41         this.leaderId = leaderId;
42         this.lastIncludedIndex = lastIncludedIndex;
43         this.lastIncludedTerm = lastIncludedTerm;
44         this.data = data;
45         this.chunkIndex = chunkIndex;
46         this.totalChunks = totalChunks;
47         this.lastChunkHashCode = lastChunkHashCode;
48         this.serverConfig = serverConfig;
49     }
50
51     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
52                            long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
53         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
54                 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
55     }
56
57     public String getLeaderId() {
58         return leaderId;
59     }
60
61     public long getLastIncludedIndex() {
62         return lastIncludedIndex;
63     }
64
65     public long getLastIncludedTerm() {
66         return lastIncludedTerm;
67     }
68
69     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
70             + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
71             + "Also it would be inefficient to create a return copy as the byte[] could be large.")
72     public byte[] getData() {
73         return data;
74     }
75
76     public int getChunkIndex() {
77         return chunkIndex;
78     }
79
80     public int getTotalChunks() {
81         return totalChunks;
82     }
83
84     public Optional<Integer> getLastChunkHashCode() {
85         return lastChunkHashCode;
86     }
87
88     public Optional<ServerConfigurationPayload> getServerConfig() {
89         return serverConfig;
90     }
91
92
93     public <T> Object toSerializable(short version) {
94         return this;
95     }
96
97     @Override
98     public String toString() {
99         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
100                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
101                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
102                 + ", serverConfig=" + serverConfig.orNull() + "]";
103     }
104
105     private Object writeReplace() {
106         return new Proxy(this);
107     }
108
109     private static class Proxy implements Externalizable {
110         private static final long serialVersionUID = 1L;
111
112         private InstallSnapshot installSnapshot;
113
114         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
115         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
116         @SuppressWarnings("checkstyle:RedundantModifier")
117         public Proxy() {
118         }
119
120         Proxy(InstallSnapshot installSnapshot) {
121             this.installSnapshot = installSnapshot;
122         }
123
124         @Override
125         public void writeExternal(ObjectOutput out) throws IOException {
126             out.writeLong(installSnapshot.getTerm());
127             out.writeObject(installSnapshot.leaderId);
128             out.writeLong(installSnapshot.lastIncludedIndex);
129             out.writeLong(installSnapshot.lastIncludedTerm);
130             out.writeInt(installSnapshot.chunkIndex);
131             out.writeInt(installSnapshot.totalChunks);
132
133             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
134             if (installSnapshot.lastChunkHashCode.isPresent()) {
135                 out.writeInt(installSnapshot.lastChunkHashCode.get().intValue());
136             }
137
138             out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
139             if (installSnapshot.serverConfig.isPresent()) {
140                 out.writeObject(installSnapshot.serverConfig.get());
141             }
142
143             out.writeObject(installSnapshot.data);
144         }
145
146         @Override
147         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
148             long term = in.readLong();
149             String leaderId = (String) in.readObject();
150             long lastIncludedIndex = in.readLong();
151             long lastIncludedTerm = in.readLong();
152             int chunkIndex = in.readInt();
153             int totalChunks = in.readInt();
154
155             Optional<Integer> lastChunkHashCode = Optional.absent();
156             boolean chunkHashCodePresent = in.readByte() == 1;
157             if (chunkHashCodePresent) {
158                 lastChunkHashCode = Optional.of(in.readInt());
159             }
160
161             Optional<ServerConfigurationPayload> serverConfig = Optional.absent();
162             boolean serverConfigPresent = in.readByte() == 1;
163             if (serverConfigPresent) {
164                 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
165             }
166
167             byte[] data = (byte[])in.readObject();
168
169             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
170                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
171         }
172
173         private Object readResolve() {
174             return installSnapshot;
175         }
176     }
177 }