08923688939821cdf0b519bc924fcf9b3a95ff07
[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 @Nonnull ActorRef replyTo) {
33         super(target);
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     /**
43      * Return the return address where responses to this request should be directed to.
44      *
45      * @return Original requestor
46      */
47     public final @Nonnull ActorRef getReplyTo() {
48         return replyTo;
49     }
50
51     /**
52      * Return a {@link RequestFailure} for this request, caused by a {@link RequestException}.
53      *
54      * @param cause Failure cause
55      * @return {@link RequestFailure} corresponding to this request
56      */
57     public abstract @Nonnull RequestFailure<T, ?> toRequestFailure(final @Nonnull RequestException cause);
58
59     @Override
60     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
61         return super.addToStringAttributes(toStringHelper).add("replyTo", replyTo);
62     }
63
64     @Override
65     protected abstract AbstractRequestProxy<T, C> externalizableProxy(@Nonnull ABIVersion version);
66 }