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