Refactor TransactonContext
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / RemoteTransactionContext.java
index 20074c10289908d20839fb2fbbd43e0089a7d24c..af0c8714092ea37e82fb389b828cf415ff8bdf31 100644 (file)
@@ -10,25 +10,17 @@ package org.opendaylight.controller.cluster.datastore;
 
 import akka.actor.ActorSelection;
 import akka.dispatch.OnComplete;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.SettableFuture;
 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
-import org.opendaylight.controller.cluster.datastore.messages.DataExists;
-import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
-import org.opendaylight.controller.cluster.datastore.messages.ReadData;
-import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
-import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
-import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
+import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
 import org.opendaylight.controller.cluster.datastore.modification.Modification;
-import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import scala.concurrent.Future;
@@ -175,77 +167,22 @@ public class RemoteTransactionContext extends AbstractTransactionContext {
     }
 
     @Override
-    public void deleteData(YangInstanceIdentifier path) {
-        LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
-
-        acquireOperation();
-        batchModification(new DeleteModification(path));
-    }
-
-    @Override
-    public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
-        LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
-
-        acquireOperation();
-        batchModification(new MergeModification(path, data));
-    }
-
-    @Override
-    public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
-        LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
-
-        acquireOperation();
-        batchModification(new WriteModification(path, data));
-    }
-
-    @Override
-    public void readData(final YangInstanceIdentifier path,
-            final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
-
-        LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
-
-        // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
-        // public API contract.
+    public void executeModification(AbstractModification modification) {
+        if(LOG.isDebugEnabled()) {
+            LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass()
+                    .getSimpleName(), modification.getPath());
+        }
 
         acquireOperation();
-        sendBatchedModifications();
-
-        OnComplete<Object> onComplete = new OnComplete<Object>() {
-            @Override
-            public void onComplete(Throwable failure, Object readResponse) throws Throwable {
-                if(failure != null) {
-                    LOG.debug("Tx {} read operation failed: {}", getIdentifier(), failure);
-                    returnFuture.setException(new ReadFailedException(
-                            "Error reading data for path " + path, failure));
-
-                } else {
-                    LOG.debug("Tx {} read operation succeeded", getIdentifier(), failure);
-
-                    if (readResponse instanceof ReadDataReply) {
-                        ReadDataReply reply = (ReadDataReply) readResponse;
-                        returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
-
-                    } else 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 " + path));
-                    }
-                }
-            }
-        };
-
-        Future<Object> readFuture = executeOperationAsync(new ReadData(path));
-
-        readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
+        batchModification(modification);
     }
 
     @Override
-    public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
-
-        LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
+    public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture) {
+        if(LOG.isDebugEnabled()) {
+            LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
+                    readCmd.getPath());
+        }
 
         // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
         // public API contract.
@@ -257,27 +194,22 @@ public class RemoteTransactionContext extends AbstractTransactionContext {
             @Override
             public void onComplete(Throwable failure, Object response) throws Throwable {
                 if(failure != null) {
-                    LOG.debug("Tx {} dataExists operation failed: {}", getIdentifier(), failure);
-                    returnFuture.setException(new ReadFailedException(
-                            "Error checking data exists for path " + path, failure));
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(),
+                                failure);
+                    }
+                    returnFuture.setException(new ReadFailedException("Error checking " + readCmd.getClass().getSimpleName()
+                            + " for path " + readCmd.getPath(), failure));
                 } else {
-                    LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
-
-                    if (response instanceof DataExistsReply) {
-                        returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
-
-                    } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
-                        returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
-
-                    } else {
-                        returnFuture.setException(new ReadFailedException(
-                                "Invalid response checking exists for path " + path));
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
                     }
+                    readCmd.processResponse(response, returnFuture);
                 }
             }
         };
 
-        Future<Object> future = executeOperationAsync(new DataExists(path));
+        Future<Object> future = executeOperationAsync(readCmd);
 
         future.onComplete(onComplete, actorContext.getClientDispatcher());
     }