2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. 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.access.concepts;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.serialization.JavaSerializer;
14 import akka.serialization.Serialization;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.cluster.access.ABIVersion;
21 import org.opendaylight.yangtools.concepts.WritableIdentifier;
24 * A request message concept. Upon receipt of this message, the recipient will respond with either
25 * a {@link RequestSuccess} or a {@link RequestFailure} message.
27 * @param <T> Target identifier type
28 * @param <C> Message type
30 public abstract class Request<T extends WritableIdentifier, C extends Request<T, C>> extends Message<T, C> {
31 protected interface SerialForm<T extends WritableIdentifier, C extends Request<T, C>>
32 extends Message.SerialForm<T, C> {
34 default C readExternal(final ObjectInput in, final T target, final long sequence)
35 throws ClassNotFoundException, IOException {
36 return readExternal(in, target, sequence,
37 JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject()));
40 @NonNull C readExternal(@NonNull ObjectInput in, @NonNull T target, long sequence, @NonNull ActorRef replyTo)
44 default void writeExternal(final ObjectOutput out, final C msg) throws IOException {
45 out.writeObject(Serialization.serializedActorPath(msg.getReplyTo()));
50 private static final long serialVersionUID = 1L;
52 private final @NonNull ActorRef replyTo;
54 protected Request(final @NonNull T target, final long sequence, final @NonNull ActorRef replyTo) {
55 super(target, sequence);
56 this.replyTo = requireNonNull(replyTo);
59 protected Request(final @NonNull C request, final @NonNull ABIVersion version) {
60 super(request, version);
61 this.replyTo = requireNonNull(request.getReplyTo());
65 * Return the return address where responses to this request should be directed to.
67 * @return Original requestor
69 public final @NonNull ActorRef getReplyTo() {
74 * Return a {@link RequestFailure} for this request, caused by a {@link RequestException}.
76 * @param cause Failure cause
77 * @return {@link RequestFailure} corresponding to this request
79 public abstract @NonNull RequestFailure<T, ?> toRequestFailure(@NonNull RequestException cause);
82 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
83 return super.addToStringAttributes(toStringHelper).add("replyTo", replyTo);
87 protected abstract SerialForm<T, C> externalizableProxy(ABIVersion version);