d0d6324b135ee70b28c3effe630e77f585d3d769
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / Request.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 com.google.common.annotations.Beta;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.cluster.access.ABIVersion;
16 import org.opendaylight.yangtools.concepts.WritableIdentifier;
17
18 /**
19  * A request message concept. Upon receipt of this message, the recipient will respond with either
20  * a {@link RequestSuccess} or a {@link RequestFailure} message.
21  *
22  * @author Robert Varga
23  *
24  * @param <T> Target identifier type
25  * @param <C> Message type
26  */
27 @Beta
28 public abstract class Request<T extends WritableIdentifier, C extends Request<T, C>> extends Message<T, C> {
29     private static final long serialVersionUID = 1L;
30     private final ActorRef replyTo;
31
32     protected Request(final @Nonnull T target, final long sequence, final long retry, final @Nonnull ActorRef replyTo) {
33         super(target, sequence, retry);
34         this.replyTo = Preconditions.checkNotNull(replyTo);
35     }
36
37     protected Request(final @Nonnull C request, final @Nonnull ABIVersion version) {
38         super(request, version);
39         this.replyTo = Preconditions.checkNotNull(request.getReplyTo());
40     }
41
42     protected Request(final C request, final long retry) {
43         super(request, retry);
44         this.replyTo = Preconditions.checkNotNull(request.getReplyTo());
45     }
46
47     /**
48      * Return the return address where responses to this request should be directed to.
49      *
50      * @return Original requestor
51      */
52     public final @Nonnull ActorRef getReplyTo() {
53         return replyTo;
54     }
55
56     /**
57      * Return a {@link RequestFailure} for this request, caused by a {@link RequestException}.
58      *
59      * @param cause Failure cause
60      * @return {@link RequestFailure} corresponding to this request
61      */
62     public abstract @Nonnull RequestFailure<T, ?> toRequestFailure(final @Nonnull RequestException cause);
63
64     @Override
65     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
66         return super.addToStringAttributes(toStringHelper).add("replyTo", replyTo);
67     }
68
69     @Override
70     protected abstract AbstractRequestProxy<T, C> externalizableProxy(@Nonnull ABIVersion version);
71 }