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 com.google.protobuf.ByteString;
13 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
15 public class InstallSnapshot extends AbstractRaftRPC {
17 public static final Class<InstallSnapshotMessages.InstallSnapshot> SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
18 private static final long serialVersionUID = 1L;
20 private final String leaderId;
21 private final long lastIncludedIndex;
22 private final long lastIncludedTerm;
23 private final ByteString data;
24 private final int chunkIndex;
25 private final int totalChunks;
26 private final Optional<Integer> lastChunkHashCode;
28 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
29 long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode) {
31 this.leaderId = leaderId;
32 this.lastIncludedIndex = lastIncludedIndex;
33 this.lastIncludedTerm = lastIncludedTerm;
35 this.chunkIndex = chunkIndex;
36 this.totalChunks = totalChunks;
37 this.lastChunkHashCode = lastChunkHashCode;
40 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
41 long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks) {
42 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, Optional.<Integer>absent());
46 public String getLeaderId() {
50 public long getLastIncludedIndex() {
51 return lastIncludedIndex;
54 public long getLastIncludedTerm() {
55 return lastIncludedTerm;
58 public ByteString getData() {
62 public int getChunkIndex() {
66 public int getTotalChunks() {
70 public Optional<Integer> getLastChunkHashCode() {
71 return lastChunkHashCode;
74 public <T extends Object> Object toSerializable(){
75 InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder()
76 .setTerm(this.getTerm())
77 .setLeaderId(this.getLeaderId())
78 .setChunkIndex(this.getChunkIndex())
79 .setData(this.getData())
80 .setLastIncludedIndex(this.getLastIncludedIndex())
81 .setLastIncludedTerm(this.getLastIncludedTerm())
82 .setTotalChunks(this.getTotalChunks());
84 if(lastChunkHashCode.isPresent()){
85 builder.setLastChunkHashCode(lastChunkHashCode.get());
87 return builder.build();
90 public static InstallSnapshot fromSerializable (Object o) {
91 InstallSnapshotMessages.InstallSnapshot from =
92 (InstallSnapshotMessages.InstallSnapshot) o;
94 Optional<Integer> lastChunkHashCode = Optional.absent();
95 if(from.hasLastChunkHashCode()){
96 lastChunkHashCode = Optional.of(from.getLastChunkHashCode());
99 InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
100 from.getLeaderId(), from.getLastIncludedIndex(),
101 from.getLastIncludedTerm(), from.getData(),
102 from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode);
104 return installSnapshot;
108 public String toString() {
109 StringBuilder builder = new StringBuilder();
110 builder.append("InstallSnapshot [term=").append(term).append(", leaderId=").append(leaderId)
111 .append(", lastIncludedIndex=").append(lastIncludedIndex).append(", lastIncludedTerm=")
112 .append(lastIncludedTerm).append(", data=").append(data).append(", chunkIndex=").append(chunkIndex)
113 .append(", totalChunks=").append(totalChunks).append(", lastChunkHashCode=").append(lastChunkHashCode)
115 return builder.toString();