2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.messaging;
10 import static java.util.Objects.requireNonNull;
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;
24 * The reply message for {@link MessageSlice}.
26 * @author Thomas Pantelis
28 public final class MessageSliceReply implements Serializable {
29 private static final long serialVersionUID = 1L;
31 private final Identifier identifier;
32 private final int sliceIndex;
33 private final MessageSliceException failure;
34 private final ActorRef sendTo;
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;
44 public static MessageSliceReply success(final Identifier identifier, final int sliceIndex, final ActorRef sendTo) {
45 return new MessageSliceReply(identifier, sliceIndex, null, sendTo);
48 public static MessageSliceReply failed(final Identifier identifier, final MessageSliceException failure,
49 final ActorRef sendTo) {
50 return new MessageSliceReply(identifier, -1, failure, sendTo);
53 public Identifier getIdentifier() {
57 public int getSliceIndex() {
61 public ActorRef getSendTo() {
65 public Optional<MessageSliceException> getFailure() {
66 return Optional.ofNullable(failure);
70 public String toString() {
71 return "MessageSliceReply [identifier=" + identifier + ", sliceIndex=" + sliceIndex + ", failure=" + failure
72 + ", sendTo=" + sendTo + "]";
75 private Object writeReplace() {
76 return new Proxy(this);
79 private static class Proxy implements Externalizable {
80 private static final long serialVersionUID = 1L;
82 private MessageSliceReply messageSliceReply;
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")
90 Proxy(final MessageSliceReply messageSliceReply) {
91 this.messageSliceReply = messageSliceReply;
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));
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());
110 messageSliceReply = new MessageSliceReply(identifier, sliceIndex, failure, sendTo);
113 private Object readResolve() {
114 return messageSliceReply;