Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / Request.java
1 /*
2  * Copyright (c) 2016, 2017 Cisco Systems, Inc. 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.access.concepts;
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 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;
22
23 /**
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.
26  *
27  * @param <T> Target identifier type
28  * @param <C> Message type
29  */
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> {
33         @Override
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()));
38         }
39
40         @NonNull C readExternal(@NonNull ObjectInput in, @NonNull T target, long sequence, @NonNull ActorRef replyTo)
41             throws IOException;
42
43         @Override
44         default void writeExternal(final ObjectOutput out, final C msg) throws IOException {
45             out.writeObject(Serialization.serializedActorPath(msg.getReplyTo()));
46         }
47     }
48
49     @java.io.Serial
50     private static final long serialVersionUID = 1L;
51
52     private final @NonNull ActorRef replyTo;
53
54     protected Request(final @NonNull T target, final long sequence, final @NonNull ActorRef replyTo) {
55         super(target, sequence);
56         this.replyTo = requireNonNull(replyTo);
57     }
58
59     protected Request(final @NonNull C request, final @NonNull ABIVersion version) {
60         super(request, version);
61         this.replyTo = requireNonNull(request.getReplyTo());
62     }
63
64     /**
65      * Return the return address where responses to this request should be directed to.
66      *
67      * @return Original requestor
68      */
69     public final @NonNull ActorRef getReplyTo() {
70         return replyTo;
71     }
72
73     /**
74      * Return a {@link RequestFailure} for this request, caused by a {@link RequestException}.
75      *
76      * @param cause Failure cause
77      * @return {@link RequestFailure} corresponding to this request
78      */
79     public abstract @NonNull RequestFailure<T, ?> toRequestFailure(@NonNull RequestException cause);
80
81     @Override
82     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
83         return super.addToStringAttributes(toStringHelper).add("replyTo", replyTo);
84     }
85
86     @Override
87     protected abstract SerialForm<T, C> externalizableProxy(ABIVersion version);
88 }