2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.raft.messages;
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;
18 public class InstallSnapshot extends AbstractRaftRPC {
19 private static final long serialVersionUID = 1L;
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;
30 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
31 int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode, Optional<ServerConfigurationPayload> serverConfig) {
33 this.leaderId = leaderId;
34 this.lastIncludedIndex = lastIncludedIndex;
35 this.lastIncludedTerm = lastIncludedTerm;
37 this.chunkIndex = chunkIndex;
38 this.totalChunks = totalChunks;
39 this.lastChunkHashCode = lastChunkHashCode;
40 this.serverConfig = serverConfig;
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());
49 public String getLeaderId() {
53 public long getLastIncludedIndex() {
54 return lastIncludedIndex;
57 public long getLastIncludedTerm() {
58 return lastIncludedTerm;
61 public byte[] getData() {
65 public int getChunkIndex() {
69 public int getTotalChunks() {
73 public Optional<Integer> getLastChunkHashCode() {
74 return lastChunkHashCode;
77 public Optional<ServerConfigurationPayload> getServerConfig() {
82 public <T extends Object> Object toSerializable(short version) {
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() + "]";
94 private Object writeReplace() {
95 return new Proxy(this);
98 private static class Proxy implements Externalizable {
99 private static final long serialVersionUID = 1L;
101 private InstallSnapshot installSnapshot;
106 Proxy(InstallSnapshot installSnapshot) {
107 this.installSnapshot = installSnapshot;
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);
119 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
120 if(installSnapshot.lastChunkHashCode.isPresent()) {
121 out.writeInt(installSnapshot.lastChunkHashCode.get().intValue());
124 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
125 if(installSnapshot.serverConfig.isPresent()) {
126 out.writeObject(installSnapshot.serverConfig.get());
129 out.writeObject(installSnapshot.data);
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();
141 Optional<Integer> lastChunkHashCode = Optional.absent();
142 boolean chunkHashCodePresent = in.readByte() == 1;
143 if(chunkHashCodePresent) {
144 lastChunkHashCode = Optional.of(in.readInt());
147 Optional<ServerConfigurationPayload> serverConfig = Optional.absent();
148 boolean serverConfigPresent = in.readByte() == 1;
149 if(serverConfigPresent) {
150 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
153 byte[] data = (byte[])in.readObject();
155 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
156 chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
159 private Object readResolve() {
160 return installSnapshot;