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;
+ @Override
+ public Object toSerializable() {
+ if(getVersion() >= DataStoreVersions.BORON_VERSION) {
+ return this;
+ } else {
+ 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;
+ }
}