Fix modernization issues
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / messaging / MessageSliceReply.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 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 java.util.Optional;
21 import org.opendaylight.yangtools.concepts.Identifier;
22
23 /**
24  * The reply message for {@link MessageSlice}.
25  *
26  * @author Thomas Pantelis
27  */
28 public final class MessageSliceReply implements Serializable {
29     private static final long serialVersionUID = 1L;
30
31     private final Identifier identifier;
32     private final int sliceIndex;
33     private final MessageSliceException failure;
34     private final ActorRef sendTo;
35
36     private MessageSliceReply(final Identifier identifier, final int sliceIndex, final MessageSliceException failure,
37             final ActorRef sendTo) {
38         this.identifier = requireNonNull(identifier);
39         this.sliceIndex = sliceIndex;
40         this.sendTo = requireNonNull(sendTo);
41         this.failure = failure;
42     }
43
44     public static MessageSliceReply success(final Identifier identifier, final int sliceIndex, final ActorRef sendTo) {
45         return new MessageSliceReply(identifier, sliceIndex, null, sendTo);
46     }
47
48     public static MessageSliceReply failed(final Identifier identifier, final MessageSliceException failure,
49             final ActorRef sendTo) {
50         return new MessageSliceReply(identifier, -1, failure, sendTo);
51     }
52
53     public Identifier getIdentifier() {
54         return identifier;
55     }
56
57     public int getSliceIndex() {
58         return sliceIndex;
59     }
60
61     public ActorRef getSendTo() {
62         return sendTo;
63     }
64
65     public Optional<MessageSliceException> getFailure() {
66         return Optional.ofNullable(failure);
67     }
68
69     @Override
70     public String toString() {
71         return "MessageSliceReply [identifier=" + identifier + ", sliceIndex=" + sliceIndex + ", failure=" + failure
72                 + ", sendTo=" + sendTo + "]";
73     }
74
75     private Object writeReplace() {
76         return new Proxy(this);
77     }
78
79     private static class Proxy implements Externalizable {
80         private static final long serialVersionUID = 1L;
81
82         private MessageSliceReply messageSliceReply;
83
84         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
85         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
86         @SuppressWarnings("checkstyle:RedundantModifier")
87         public Proxy() {
88         }
89
90         Proxy(final MessageSliceReply messageSliceReply) {
91             this.messageSliceReply = messageSliceReply;
92         }
93
94         @Override
95         public void writeExternal(final ObjectOutput out) throws IOException {
96             out.writeObject(messageSliceReply.identifier);
97             out.writeInt(messageSliceReply.sliceIndex);
98             out.writeObject(messageSliceReply.failure);
99             out.writeObject(Serialization.serializedActorPath(messageSliceReply.sendTo));
100         }
101
102         @Override
103         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
104             final Identifier identifier = (Identifier) in.readObject();
105             final int sliceIndex = in.readInt();
106             final MessageSliceException failure = (MessageSliceException) in.readObject();
107             ActorRef sendTo = JavaSerializer.currentSystem().value().provider()
108                     .resolveActorRef((String) in.readObject());
109
110             messageSliceReply = new MessageSliceReply(identifier, sliceIndex, failure, sendTo);
111         }
112
113         private Object readResolve() {
114             return messageSliceReply;
115         }
116     }
117 }