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