d2a6a0d0824b53895e860491c7958aa50132c65d
[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 com.google.protobuf.ByteString;
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.RaftVersions;
18 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
19 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
20
21 public class InstallSnapshot extends AbstractRaftRPC implements Externalizable {
22     private static final long serialVersionUID = 1L;
23     public static final Class<InstallSnapshotMessages.InstallSnapshot> SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class;
24
25     private String leaderId;
26     private long lastIncludedIndex;
27     private long lastIncludedTerm;
28     private byte[] data;
29     private int chunkIndex;
30     private int totalChunks;
31     private Optional<Integer> lastChunkHashCode;
32     private Optional<ServerConfigurationPayload> serverConfig;
33
34     /**
35      * Empty constructor to satisfy Externalizable.
36      */
37     public InstallSnapshot() {
38     }
39
40     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
41             int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode, Optional<ServerConfigurationPayload> serverConfig) {
42         super(term);
43         this.leaderId = leaderId;
44         this.lastIncludedIndex = lastIncludedIndex;
45         this.lastIncludedTerm = lastIncludedTerm;
46         this.data = data;
47         this.chunkIndex = chunkIndex;
48         this.totalChunks = totalChunks;
49         this.lastChunkHashCode = lastChunkHashCode;
50         this.serverConfig = serverConfig;
51     }
52
53     public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
54                            long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
55         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
56                 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
57     }
58
59     public String getLeaderId() {
60         return leaderId;
61     }
62
63     public long getLastIncludedIndex() {
64         return lastIncludedIndex;
65     }
66
67     public long getLastIncludedTerm() {
68         return lastIncludedTerm;
69     }
70
71     public byte[] getData() {
72         return data;
73     }
74
75     public int getChunkIndex() {
76         return chunkIndex;
77     }
78
79     public int getTotalChunks() {
80         return totalChunks;
81     }
82
83     public Optional<Integer> getLastChunkHashCode() {
84         return lastChunkHashCode;
85     }
86
87     public Optional<ServerConfigurationPayload> getServerConfig() {
88         return serverConfig;
89     }
90
91     @Override
92     public void writeExternal(ObjectOutput out) throws IOException {
93         out.writeShort(RaftVersions.CURRENT_VERSION);
94         out.writeLong(getTerm());
95         out.writeUTF(leaderId);
96         out.writeLong(lastIncludedIndex);
97         out.writeLong(lastIncludedTerm);
98         out.writeInt(chunkIndex);
99         out.writeInt(totalChunks);
100
101         out.writeByte(lastChunkHashCode.isPresent() ? 1 : 0);
102         if(lastChunkHashCode.isPresent()) {
103             out.writeInt(lastChunkHashCode.get().intValue());
104         }
105
106         out.writeByte(serverConfig.isPresent() ? 1 : 0);
107         if(serverConfig.isPresent()) {
108             out.writeObject(serverConfig.get());
109         }
110
111         out.writeObject(data);
112     }
113
114     @Override
115     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
116         in.readShort(); // raft version - not currently used
117         setTerm(in.readLong());
118         leaderId = in.readUTF();
119         lastIncludedIndex = in.readLong();
120         lastIncludedTerm = in.readLong();
121         chunkIndex = in.readInt();
122         totalChunks = in.readInt();
123
124         lastChunkHashCode = Optional.absent();
125         boolean chunkHashCodePresent = in.readByte() == 1;
126         if(chunkHashCodePresent) {
127             lastChunkHashCode = Optional.of(in.readInt());
128         }
129
130         serverConfig = Optional.absent();
131         boolean serverConfigPresent = in.readByte() == 1;
132         if(serverConfigPresent) {
133             serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
134         }
135
136         data = (byte[])in.readObject();
137     }
138
139     public <T extends Object> Object toSerializable(short version) {
140         if(version >= RaftVersions.BORON_VERSION) {
141             return this;
142         } else {
143             InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder()
144                     .setTerm(this.getTerm())
145                     .setLeaderId(this.getLeaderId())
146                     .setChunkIndex(this.getChunkIndex())
147                     .setData(ByteString.copyFrom(getData()))
148                     .setLastIncludedIndex(this.getLastIncludedIndex())
149                     .setLastIncludedTerm(this.getLastIncludedTerm())
150                     .setTotalChunks(this.getTotalChunks());
151
152             if(lastChunkHashCode.isPresent()){
153                 builder.setLastChunkHashCode(lastChunkHashCode.get());
154             }
155             return builder.build();
156         }
157     }
158
159     @Override
160     public String toString() {
161         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
162                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
163                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
164                 + ", serverConfig=" + serverConfig.orNull() + "]";
165     }
166
167     public static InstallSnapshot fromSerializable (Object o) {
168         if(o instanceof InstallSnapshot) {
169             return (InstallSnapshot)o;
170         } else {
171             InstallSnapshotMessages.InstallSnapshot from =
172                     (InstallSnapshotMessages.InstallSnapshot) o;
173
174             Optional<Integer> lastChunkHashCode = Optional.absent();
175             if(from.hasLastChunkHashCode()){
176                 lastChunkHashCode = Optional.of(from.getLastChunkHashCode());
177             }
178
179             InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(),
180                     from.getLeaderId(), from.getLastIncludedIndex(),
181                     from.getLastIncludedTerm(), from.getData().toByteArray(),
182                     from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode,
183                     Optional.<ServerConfigurationPayload>absent());
184
185             return installSnapshot;
186         }
187     }
188
189     public static boolean isSerializedType(Object message) {
190         return message instanceof InstallSnapshot || message instanceof InstallSnapshotMessages.InstallSnapshot;
191     }
192 }