2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static java.util.Objects.requireNonNull;
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;
21 * A failure response to a {@link Request}. Contains a {@link RequestException} detailing the cause for this failure.
23 * @param <T> Target identifier type
24 * @param <C> Message class
26 public abstract class RequestFailure<T extends WritableIdentifier, C extends RequestFailure<T, C>>
27 extends Response<T, C> {
29 * Externalizable proxy for use with {@link RequestFailure} subclasses.
31 * @param <T> Target identifier type
33 protected interface SerialForm<T extends WritableIdentifier, C extends RequestFailure<T, C>>
34 extends Message.SerialForm<T, C> {
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());
42 default void writeExternal(final ObjectOutput out, final C msg) throws IOException {
43 out.writeObject(msg.getCause());
46 @NonNull C createFailure(@NonNull T target, long sequence, @NonNull RequestException failureCause);
50 private static final long serialVersionUID = 1L;
52 private final @NonNull RequestException cause;
54 protected RequestFailure(final @NonNull C failure, final @NonNull ABIVersion version) {
55 super(failure, version);
56 this.cause = requireNonNull(failure.getCause());
59 protected RequestFailure(final @NonNull T target, final long sequence, final @NonNull RequestException cause) {
60 super(target, sequence);
61 this.cause = requireNonNull(cause);
65 * Return the failure cause.
67 * @return Failure cause.
69 public final @NonNull RequestException getCause() {
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.
77 * @return True if this represents a hard failure, false otherwise.
79 public final boolean isHardFailure() {
80 return !cause.isRetriable();
84 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
85 return super.addToStringAttributes(toStringHelper).add("cause", cause);
89 protected abstract SerialForm<T, C> externalizableProxy(ABIVersion version);