Deprecate ReadData/DataExists protobuff messages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / AbstractRead.java
1 /*
2  * Copyright (c) 2015 Huawei, 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
9 package org.opendaylight.controller.cluster.datastore.messages;
10
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 /**
22  * Abstract base class for ReadData and DataExists messages.
23  *
24  * @author gwu
25  *
26  */
27 public abstract class AbstractRead<T> extends VersionedExternalizableMessage {
28     private static final long serialVersionUID = 1L;
29
30     private YangInstanceIdentifier path;
31
32     protected AbstractRead() {
33     }
34
35     public AbstractRead(final YangInstanceIdentifier path, final short version) {
36         super(version);
37         this.path = path;
38     }
39
40     public YangInstanceIdentifier getPath() {
41         return path;
42     }
43
44     @Override
45     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
46         super.readExternal(in);
47         path = SerializationUtils.deserializePath(in);
48     }
49
50     @Override
51     public void writeExternal(ObjectOutput out) throws IOException {
52         super.writeExternal(out);
53         SerializationUtils.serializePath(path, out);
54     }
55
56     public AbstractRead<T> asVersion(short version) {
57         return version == getVersion() ? this : newInstance(version);
58     }
59
60     public abstract CheckedFuture<T, ReadFailedException> apply(DOMStoreReadTransaction readDelegate);
61
62     public abstract void processResponse(Object reponse, SettableFuture<T> promise);
63
64     protected abstract AbstractRead<T> newInstance(short withVersion);
65 }