301b54ee1ffb7a8c241ab6d26fc71630e7e80a8a
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / RequestFailure.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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.cluster.access.ABIVersion;
15 import org.opendaylight.yangtools.concepts.WritableIdentifier;
16
17 /**
18  * A failure response to a {@link Request}. Contains a {@link RequestException} detailing the cause for this failure.
19  *
20  * @author Robert Varga
21  *
22  * @param <T> Target identifier type
23  * @param <C> Message class
24  */
25 @Beta
26 public abstract class RequestFailure<T extends WritableIdentifier, C extends RequestFailure<T, C>> extends Response<T, C> {
27     private static final long serialVersionUID = 1L;
28     private final RequestException cause;
29
30     protected RequestFailure(final @Nonnull C failure, final @Nonnull ABIVersion version) {
31         super(failure, version);
32         this.cause = Preconditions.checkNotNull(failure.getCause());
33     }
34
35     protected RequestFailure(final @Nonnull T target, final @Nonnull RequestException cause) {
36         super(target);
37         this.cause = Preconditions.checkNotNull(cause);
38     }
39
40     /**
41      * Return the failure cause.
42      *
43      * @return Failure cause.
44      */
45     public final @Nonnull RequestException getCause() {
46         return cause;
47     }
48
49     /**
50      * Return an indication of whether this a hard failure. Hard failures must not be retried but need to be treated
51      * as authoritative response to a request.
52      *
53      * @return True if this represents a hard failure, false otherwise.
54      */
55     public final boolean isHardFailure() {
56         return !cause.isRetriable();
57     }
58
59     @Override
60     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
61         return super.addToStringAttributes(toStringHelper).add("cause", cause);
62     }
63
64     @Override
65     protected abstract AbstractRequestFailureProxy<T, C> externalizableProxy(@Nonnull ABIVersion version);
66 }