Improve segmented journal actor metrics
[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.base.MoreObjects.ToStringHelper;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.ABIVersion;
18 import org.opendaylight.yangtools.concepts.WritableIdentifier;
19
20 /**
21  * A failure response to a {@link Request}. Contains a {@link RequestException} detailing the cause for this failure.
22  *
23  * @param <T> Target identifier type
24  * @param <C> Message class
25  */
26 public abstract class RequestFailure<T extends WritableIdentifier, C extends RequestFailure<T, C>>
27         extends Response<T, C> {
28     /**
29      * Externalizable proxy for use with {@link RequestFailure} subclasses.
30      *
31      * @param <T> Target identifier type
32      */
33     protected interface SerialForm<T extends WritableIdentifier, C extends RequestFailure<T, C>>
34             extends Message.SerialForm<T, C> {
35         @Override
36         default C readExternal(final ObjectInput in, final T target, final long sequence)
37                 throws IOException, ClassNotFoundException {
38             return createFailure(target, sequence, (RequestException) in.readObject());
39         }
40
41         @Override
42         default void writeExternal(final ObjectOutput out, final C msg) throws IOException {
43             out.writeObject(msg.getCause());
44         }
45
46         @NonNull C createFailure(@NonNull T target, long sequence, @NonNull RequestException failureCause);
47     }
48
49     @java.io.Serial
50     private static final long serialVersionUID = 1L;
51
52     private final @NonNull RequestException cause;
53
54     protected RequestFailure(final @NonNull C failure, final @NonNull ABIVersion version) {
55         super(failure, version);
56         this.cause = requireNonNull(failure.getCause());
57     }
58
59     protected RequestFailure(final @NonNull T target, final long sequence, final @NonNull RequestException cause) {
60         super(target, sequence);
61         this.cause = requireNonNull(cause);
62     }
63
64     /**
65      * Return the failure cause.
66      *
67      * @return Failure cause.
68      */
69     public final @NonNull RequestException getCause() {
70         return cause;
71     }
72
73     /**
74      * Return an indication of whether this a hard failure. Hard failures must not be retried but need to be treated
75      * as authoritative response to a request.
76      *
77      * @return True if this represents a hard failure, false otherwise.
78      */
79     public final boolean isHardFailure() {
80         return !cause.isRetriable();
81     }
82
83     @Override
84     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
85         return super.addToStringAttributes(toStringHelper).add("cause", cause);
86     }
87
88     @Override
89     protected abstract SerialForm<T, C> externalizableProxy(ABIVersion version);
90 }