Improve segmented journal actor metrics
[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
12 public final class RequestEnvelope extends Envelope<Request<?, ?>> {
13     @java.io.Serial
14     private static final long serialVersionUID = 1L;
15
16     public RequestEnvelope(final Request<?, ?> message, final long sessionId, final long txSequence) {
17         super(message, sessionId, txSequence);
18     }
19
20     @Override
21     RE createProxy() {
22         return new RE(this);
23     }
24
25     /**
26      * Respond to this envelope with a {@link RequestFailure} caused by specified {@link RequestException}.
27      *
28      * @param cause Cause of this {@link RequestFailure}
29      * @param executionTimeNanos Time to execute the request, in nanoseconds
30      * @throws NullPointerException if cause is null
31      */
32     public void sendFailure(final RequestException cause, final long executionTimeNanos) {
33         sendResponse(new FailureEnvelope(getMessage().toRequestFailure(cause), getSessionId(), getTxSequence(),
34             executionTimeNanos));
35     }
36
37     /**
38      * Respond to this envelope with a {@link RequestSuccess}.
39      *
40      * @param success Successful response
41      * @throws NullPointerException if success is null
42      */
43     public void sendSuccess(final RequestSuccess<?, ?> success, final long executionTimeNanos) {
44         sendResponse(newSuccessEnvelope(success, executionTimeNanos));
45     }
46
47     /**
48      * Creates a successful ResponseEnvelope that wraps the given successful Request response message.
49      *
50      * @param success the successful Request response message
51      * @param executionTimeNanos the execution time of the request
52      * @return a {@link ResponseEnvelope} instance
53      */
54     public ResponseEnvelope<?> newSuccessEnvelope(final RequestSuccess<?, ?> success, final long executionTimeNanos) {
55         return new SuccessEnvelope(success, getSessionId(), getTxSequence(), executionTimeNanos);
56     }
57
58     private void sendResponse(final ResponseEnvelope<?> envelope) {
59         getMessage().getReplyTo().tell(envelope, ActorRef.noSender());
60     }
61 }