Deprecate ReadData/DataExists protobuff messages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadData.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.messages;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21
22 public class ReadData extends AbstractRead<Optional<NormalizedNode<?, ?>>> {
23     private static final long serialVersionUID = 1L;
24
25     public ReadData() {
26     }
27
28     public ReadData(final YangInstanceIdentifier path, short version) {
29         super(path, version);
30     }
31
32     @Override
33     public Object toSerializable() {
34         if(getVersion() >= DataStoreVersions.BORON_VERSION) {
35             return this;
36         } else {
37             return ShardTransactionMessages.ReadData.newBuilder().setInstanceIdentifierPathArguments(
38                     InstanceIdentifierUtils.toSerializable(getPath())).build();
39         }
40     }
41
42     @Override
43     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
44         return readDelegate.read(getPath());
45     }
46
47     @Override
48     public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
49         if(ReadDataReply.isSerializedType(readResponse)) {
50             ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
51             returnFuture.set(Optional.<NormalizedNode<?, ?>> fromNullable(reply.getNormalizedNode()));
52         } else {
53             returnFuture.setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
54         }
55     }
56
57     @Override
58     protected AbstractRead<Optional<NormalizedNode<?, ?>>> newInstance(short withVersion) {
59         return new ReadData(getPath(), withVersion);
60     }
61
62     public static ReadData fromSerializable(final Object serializable) {
63         if(serializable instanceof ReadData) {
64             return (ReadData)serializable;
65         } else {
66             ShardTransactionMessages.ReadData o = (ShardTransactionMessages.ReadData)serializable;
67             return new ReadData(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()),
68                     DataStoreVersions.LITHIUM_VERSION);
69         }
70     }
71
72     public static boolean isSerializedType(Object message) {
73         return message instanceof ReadData || message instanceof ShardTransactionMessages.ReadData;
74     }
75 }