BUG-5280: introduce request/response Envelope
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / AbstractRequestFailureProxy.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 java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.concepts.WritableIdentifier;
16
17 /**
18  * Abstract Externalizable proxy for use with {@link RequestFailure} subclasses.
19  *
20  * @author Robert Varga
21  *
22  * @param <T> Target identifier type
23  */
24 @Beta
25 public abstract class AbstractRequestFailureProxy<T extends WritableIdentifier, C extends RequestFailure<T, C>>
26         extends AbstractResponseProxy<T, C> {
27     private static final long serialVersionUID = 1L;
28     private RequestException cause;
29
30     protected AbstractRequestFailureProxy() {
31         // For Externalizable
32     }
33
34     protected AbstractRequestFailureProxy(final @Nonnull C failure) {
35         super(failure);
36         this.cause = failure.getCause();
37     }
38
39     @Override
40     public void writeExternal(final ObjectOutput out) throws IOException {
41         super.writeExternal(out);
42         out.writeObject(cause);
43     }
44
45     @Override
46     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
47         super.readExternal(in);
48         cause = (RequestException) in.readObject();
49     }
50
51     @Override
52     final C createResponse(final T target) {
53         return createFailure(target, cause);
54     }
55
56     protected abstract @Nonnull C createFailure(@Nonnull T target, @Nonnull RequestException cause);
57 }