Bug 7449: Add message slicing/re-assembly classes
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / messaging / MessageSlice.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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 package org.opendaylight.controller.cluster.messaging;
9
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import com.google.common.base.Preconditions;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.io.Serializable;
20 import org.opendaylight.yangtools.concepts.Identifier;
21
22 /**
23  * Represents a sliced message chunk.
24  *
25  * @author Thomas Pantelis
26  */
27 public class MessageSlice implements Serializable {
28     private static final long serialVersionUID = 1L;
29
30     private final Identifier identifier;
31     private final byte[] data;
32     private final int sliceIndex;
33     private final int totalSlices;
34     private final int lastSliceHashCode;
35     private final ActorRef replyTo;
36
37     public MessageSlice(Identifier identifier, byte[] data, int sliceIndex, int totalSlices, int lastSliceHashCode,
38             final ActorRef replyTo) {
39         this.identifier = Preconditions.checkNotNull(identifier);
40         this.data = Preconditions.checkNotNull(data);
41         this.sliceIndex = sliceIndex;
42         this.totalSlices = totalSlices;
43         this.lastSliceHashCode = lastSliceHashCode;
44         this.replyTo = Preconditions.checkNotNull(replyTo);
45     }
46
47     public Identifier getIdentifier() {
48         return identifier;
49     }
50
51     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
52             + "this is OK since this class is merely a DTO and does not process the byte[] internally."
53             + "Also it would be inefficient to create a return copy as the byte[] could be large.")
54     public byte[] getData() {
55         return data;
56     }
57
58     public int getSliceIndex() {
59         return sliceIndex;
60     }
61
62     public int getTotalSlices() {
63         return totalSlices;
64     }
65
66     public int getLastSliceHashCode() {
67         return lastSliceHashCode;
68     }
69
70     public ActorRef getReplyTo() {
71         return replyTo;
72     }
73
74     @Override
75     public String toString() {
76         return "MessageSlice [identifier=" + identifier + ", data.length=" + data.length + ", sliceIndex="
77                 + sliceIndex + ", totalSlices=" + totalSlices + ", lastSliceHashCode=" + lastSliceHashCode
78                 + ", replyTo=" + replyTo + "]";
79     }
80
81     private Object writeReplace() {
82         return new Proxy(this);
83     }
84
85     private static class Proxy implements Externalizable {
86         private static final long serialVersionUID = 1L;
87
88         private MessageSlice messageSlice;
89
90         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
91         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
92         @SuppressWarnings("checkstyle:RedundantModifier")
93         public Proxy() {
94         }
95
96         Proxy(MessageSlice messageSlice) {
97             this.messageSlice = messageSlice;
98         }
99
100         @Override
101         public void writeExternal(ObjectOutput out) throws IOException {
102             out.writeObject(messageSlice.identifier);
103             out.writeInt(messageSlice.sliceIndex);
104             out.writeInt(messageSlice.totalSlices);
105             out.writeInt(messageSlice.lastSliceHashCode);
106             out.writeObject(messageSlice.data);
107             out.writeObject(Serialization.serializedActorPath(messageSlice.replyTo));
108         }
109
110         @Override
111         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
112             Identifier identifier = (Identifier) in.readObject();
113             int sliceIndex = in.readInt();
114             int totalSlices = in.readInt();
115             int lastSliceHashCode = in.readInt();
116             byte[] data = (byte[])in.readObject();
117             ActorRef replyTo = JavaSerializer.currentSystem().value().provider()
118                     .resolveActorRef((String) in.readObject());
119
120             messageSlice = new MessageSlice(identifier, data, sliceIndex, totalSlices, lastSliceHashCode, replyTo);
121         }
122
123         private Object readResolve() {
124             return messageSlice;
125         }
126     }
127 }