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