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