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