Follow-up to protobuff deprecation
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / DataExists.java
index 84b8df167607701ea35caf4c32fa301451744e83..08394622fbd7ec3a410914d1ff2d57c42cf909c1 100644 (file)
@@ -8,34 +8,62 @@
 
 package org.opendaylight.controller.cluster.datastore.messages;
 
+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;
 
-public class DataExists implements SerializableMessage{
+public class DataExists extends AbstractRead<Boolean> {
+    private static final long serialVersionUID = 1L;
 
-    public static final Class<ShardTransactionMessages.DataExists> SERIALIZABLE_CLASS =
-            ShardTransactionMessages.DataExists.class;
+    public DataExists() {
+    }
 
-    private final YangInstanceIdentifier path;
+    public DataExists(final YangInstanceIdentifier path, final short version) {
+        super(path, version);
+    }
 
-    public DataExists(final YangInstanceIdentifier path) {
-        this.path = path;
+    @Deprecated
+    @Override
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionMessages.DataExists.newBuilder()
+                .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.toSerializable(getPath())).build();
     }
 
-    public YangInstanceIdentifier getPath() {
-        return path;
+    @Override
+    public CheckedFuture<Boolean, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
+        return readDelegate.exists(getPath());
     }
 
-    @Override public Object toSerializable() {
-        return ShardTransactionMessages.DataExists.newBuilder()
-            .setInstanceIdentifierPathArguments(
-                InstanceIdentifierUtils.toSerializable(path)).build();
+    @Override
+    public void processResponse(Object response, SettableFuture<Boolean> returnFuture) {
+        if(DataExistsReply.isSerializedType(response)) {
+            returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
+        } else {
+            returnFuture.setException(new ReadFailedException("Invalid response checking exists for path " + getPath()));
+        }
+    }
+
+    @Override
+    protected AbstractRead<Boolean> newInstance(short withVersion) {
+        return new DataExists(getPath(), withVersion);
     }
 
     public static DataExists fromSerializable(final Object serializable){
-        ShardTransactionMessages.DataExists o = (ShardTransactionMessages.DataExists) serializable;
-        return new DataExists(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()));
+        if(serializable instanceof DataExists) {
+            return (DataExists)serializable;
+        } else {
+            ShardTransactionMessages.DataExists o = (ShardTransactionMessages.DataExists) serializable;
+            return new DataExists(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()),
+                    DataStoreVersions.LITHIUM_VERSION);
+        }
     }
 
+    public static boolean isSerializedType(Object message) {
+        return message instanceof DataExists || message instanceof ShardTransactionMessages.DataExists;
+    }
 }