Mechanical code cleanup (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 public class InstallSnapshot extends AbstractRaftRPC {
19     private static final long serialVersionUID = 1L;
20
21     private final String leaderId;
22     private final long lastIncludedIndex;
23     private final long lastIncludedTerm;
24     private final byte[] data;
25     private final int chunkIndex;
26     private final int totalChunks;
27     private final Optional<Integer> lastChunkHashCode;
28     private final Optional<ServerConfigurationPayload> serverConfig;
29
30     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
31             int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode, Optional<ServerConfigurationPayload> serverConfig) {
32         super(term);
33         this.leaderId = leaderId;
34         this.lastIncludedIndex = lastIncludedIndex;
35         this.lastIncludedTerm = lastIncludedTerm;
36         this.data = data;
37         this.chunkIndex = chunkIndex;
38         this.totalChunks = totalChunks;
39         this.lastChunkHashCode = lastChunkHashCode;
40         this.serverConfig = serverConfig;
41     }
42
43     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
44                            long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
45         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
46                 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
47     }
48
49     public String getLeaderId() {
50         return leaderId;
51     }
52
53     public long getLastIncludedIndex() {
54         return lastIncludedIndex;
55     }
56
57     public long getLastIncludedTerm() {
58         return lastIncludedTerm;
59     }
60
61     public byte[] getData() {
62         return data;
63     }
64
65     public int getChunkIndex() {
66         return chunkIndex;
67     }
68
69     public int getTotalChunks() {
70         return totalChunks;
71     }
72
73     public Optional<Integer> getLastChunkHashCode() {
74         return lastChunkHashCode;
75     }
76
77     public Optional<ServerConfigurationPayload> getServerConfig() {
78         return serverConfig;
79     }
80
81
82     public <T> Object toSerializable(short version) {
83         return this;
84     }
85
86     @Override
87     public String toString() {
88         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
89                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
90                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
91                 + ", serverConfig=" + serverConfig.orNull() + "]";
92     }
93
94     private Object writeReplace() {
95         return new Proxy(this);
96     }
97
98     private static class Proxy implements Externalizable {
99         private static final long serialVersionUID = 1L;
100
101         private InstallSnapshot installSnapshot;
102
103         public Proxy() {
104         }
105
106         Proxy(InstallSnapshot installSnapshot) {
107             this.installSnapshot = installSnapshot;
108         }
109
110         @Override
111         public void writeExternal(ObjectOutput out) throws IOException {
112             out.writeLong(installSnapshot.getTerm());
113             out.writeObject(installSnapshot.leaderId);
114             out.writeLong(installSnapshot.lastIncludedIndex);
115             out.writeLong(installSnapshot.lastIncludedTerm);
116             out.writeInt(installSnapshot.chunkIndex);
117             out.writeInt(installSnapshot.totalChunks);
118
119             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
120             if(installSnapshot.lastChunkHashCode.isPresent()) {
121                 out.writeInt(installSnapshot.lastChunkHashCode.get().intValue());
122             }
123
124             out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
125             if(installSnapshot.serverConfig.isPresent()) {
126                 out.writeObject(installSnapshot.serverConfig.get());
127             }
128
129             out.writeObject(installSnapshot.data);
130         }
131
132         @Override
133         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
134             long term = in.readLong();
135             String leaderId = (String) in.readObject();
136             long lastIncludedIndex = in.readLong();
137             long lastIncludedTerm = in.readLong();
138             int chunkIndex = in.readInt();
139             int totalChunks = in.readInt();
140
141             Optional<Integer> lastChunkHashCode = Optional.absent();
142             boolean chunkHashCodePresent = in.readByte() == 1;
143             if(chunkHashCodePresent) {
144                 lastChunkHashCode = Optional.of(in.readInt());
145             }
146
147             Optional<ServerConfigurationPayload> serverConfig = Optional.absent();
148             boolean serverConfigPresent = in.readByte() == 1;
149             if(serverConfigPresent) {
150                 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
151             }
152
153             byte[] data = (byte[])in.readObject();
154
155             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
156                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
157         }
158
159         private Object readResolve() {
160             return installSnapshot;
161         }
162     }
163 }