Deprecate ReadData/DataExists protobuff messages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadData.java
index 2f56a9740b2d4872a116dbccc31ceeb33e6425f1..c2709d757fdacc83ea6932337dd27ee64f7ab46f 100644 (file)
@@ -8,16 +8,68 @@
 
 package org.opendaylight.controller.cluster.datastore.messages;
 
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
+import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public class ReadData {
-  private final InstanceIdentifier path;
+public class ReadData extends AbstractRead<Optional<NormalizedNode<?, ?>>> {
+    private static final long serialVersionUID = 1L;
 
-  public ReadData(InstanceIdentifier path) {
-    this.path = path;
-  }
+    public ReadData() {
+    }
 
-  public InstanceIdentifier getPath() {
-    return path;
-  }
+    public ReadData(final YangInstanceIdentifier path, short version) {
+        super(path, version);
+    }
+
+    @Override
+    public Object toSerializable() {
+        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
+            return this;
+        } else {
+            return ShardTransactionMessages.ReadData.newBuilder().setInstanceIdentifierPathArguments(
+                    InstanceIdentifierUtils.toSerializable(getPath())).build();
+        }
+    }
+
+    @Override
+    public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
+        return readDelegate.read(getPath());
+    }
+
+    @Override
+    public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
+        if(ReadDataReply.isSerializedType(readResponse)) {
+            ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
+            returnFuture.set(Optional.<NormalizedNode<?, ?>> fromNullable(reply.getNormalizedNode()));
+        } else {
+            returnFuture.setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
+        }
+    }
+
+    @Override
+    protected AbstractRead<Optional<NormalizedNode<?, ?>>> newInstance(short withVersion) {
+        return new ReadData(getPath(), withVersion);
+    }
+
+    public static ReadData fromSerializable(final Object serializable) {
+        if(serializable instanceof ReadData) {
+            return (ReadData)serializable;
+        } else {
+            ShardTransactionMessages.ReadData o = (ShardTransactionMessages.ReadData)serializable;
+            return new ReadData(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()),
+                    DataStoreVersions.LITHIUM_VERSION);
+        }
+    }
+
+    public static boolean isSerializedType(Object message) {
+        return message instanceof ReadData || message instanceof ShardTransactionMessages.ReadData;
+    }
 }