5648846fd93792178bb57eaf5ec307e7a889e3f3
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / RequestEnvelope.java
1 /*
2  * Copyright (c) 2016 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 akka.actor.ActorRef;
11 import java.io.ObjectInput;
12
13 public final class RequestEnvelope extends Envelope<Request<?, ?>> {
14     interface SerialForm extends Envelope.SerialForm<Request<?, ?>, RequestEnvelope> {
15         @Override
16         default RequestEnvelope readExternal(final ObjectInput in, final long sessionId, final long txSequence,
17                 final Request<?, ?> message) {
18             return new RequestEnvelope(message, sessionId, txSequence);
19         }
20     }
21
22     @java.io.Serial
23     private static final long serialVersionUID = 1L;
24
25     public RequestEnvelope(final Request<?, ?> message, final long sessionId, final long txSequence) {
26         super(message, sessionId, txSequence);
27     }
28
29     @Override
30     RE createProxy() {
31         return new RE(this);
32     }
33
34     @Override
35     RequestEnvelopeProxy legacyProxy() {
36         return new RequestEnvelopeProxy(this);
37     }
38
39     /**
40      * Respond to this envelope with a {@link RequestFailure} caused by specified {@link RequestException}.
41      *
42      * @param cause Cause of this {@link RequestFailure}
43      * @param executionTimeNanos Time to execute the request, in nanoseconds
44      * @throws NullPointerException if cause is null
45      */
46     public void sendFailure(final RequestException cause, final long executionTimeNanos) {
47         sendResponse(new FailureEnvelope(getMessage().toRequestFailure(cause), getSessionId(), getTxSequence(),
48             executionTimeNanos));
49     }
50
51     /**
52      * Respond to this envelope with a {@link RequestSuccess}.
53      *
54      * @param success Successful response
55      * @throws NullPointerException if success is null
56      */
57     public void sendSuccess(final RequestSuccess<?, ?> success, final long executionTimeNanos) {
58         sendResponse(newSuccessEnvelope(success, executionTimeNanos));
59     }
60
61     /**
62      * Creates a successful ResponseEnvelope that wraps the given successful Request response message.
63      *
64      * @param success the successful Request response message
65      * @param executionTimeNanos the execution time of the request
66      * @return a {@link ResponseEnvelope} instance
67      */
68     public ResponseEnvelope<?> newSuccessEnvelope(final RequestSuccess<?, ?> success, final long executionTimeNanos) {
69         return new SuccessEnvelope(success, getSessionId(), getTxSequence(), executionTimeNanos);
70     }
71
72     private void sendResponse(final ResponseEnvelope<?> envelope) {
73         getMessage().getReplyTo().tell(envelope, ActorRef.noSender());
74     }
75 }