BUG-5280: add ClientTransactionCursor 24/49424/3
authorRobert Varga <rovarga@cisco.com>
Thu, 15 Dec 2016 11:25:59 +0000 (12:25 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 16 Dec 2016 09:44:37 +0000 (10:44 +0100)
Cursor-based access is useful for new MD-SAL APIs,
expose it from ClientTransaction.

Change-Id: Iff57c9d09935181e02ab5ac3f42c9f70d9f95424
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientTransactionCursor.java [new file with mode: 0644]

index 334ab71..f0ce2fa 100644 (file)
@@ -13,8 +13,11 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
 import com.google.common.util.concurrent.CheckedFuture;
 import java.util.Collection;
 import com.google.common.collect.Iterables;
 import com.google.common.util.concurrent.CheckedFuture;
 import java.util.Collection;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeCursor;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -52,11 +55,12 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 @Beta
 public final class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
 
 @Beta
 public final class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
 
+    private ClientTransactionCursor cursor;
+
     ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
         super(parent, transactionId);
     }
 
     ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
         super(parent, transactionId);
     }
 
-
     private AbstractProxyTransaction createProxy(final Long shard) {
         return parent().createTransactionProxy(getIdentifier(), shard);
     }
     private AbstractProxyTransaction createProxy(final Long shard) {
         return parent().createTransactionProxy(getIdentifier(), shard);
     }
@@ -65,6 +69,12 @@ public final class ClientTransaction extends AbstractClientHandle<AbstractProxyT
         return ensureProxy(path, this::createProxy);
     }
 
         return ensureProxy(path, this::createProxy);
     }
 
+    public DOMDataTreeWriteCursor openCursor() {
+        Preconditions.checkState(cursor == null, "Transaction %s has open cursor", getIdentifier());
+        cursor = new ClientTransactionCursor(this);
+        return cursor;
+    }
+
     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
         return ensureTransactionProxy(path).exists(path);
     }
     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
         return ensureTransactionProxy(path).exists(path);
     }
@@ -107,4 +117,10 @@ public final class ClientTransaction extends AbstractClientHandle<AbstractProxyT
 
         return parent().onTransactionReady(this, cohort);
     }
 
         return parent().onTransactionReady(this, cohort);
     }
+
+    void closeCursor(@Nonnull final DOMDataTreeCursor cursor) {
+        if (cursor.equals(this.cursor)) {
+            this.cursor = null;
+        }
+    }
 }
 }
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientTransactionCursor.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientTransactionCursor.java
new file mode 100644 (file)
index 0000000..bbd8534
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.databroker.actors.dds;
+
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * A {@link DOMDataTreeWriteCursor} tied to a {@link ClientTransaction}.
+ *
+ * @author Robert Varga
+ */
+final class ClientTransactionCursor implements DOMDataTreeWriteCursor {
+    private YangInstanceIdentifier current = YangInstanceIdentifier.EMPTY;
+    private final ClientTransaction parent;
+
+    ClientTransactionCursor(final ClientTransaction parent) {
+        this.parent = Preconditions.checkNotNull(parent);
+    }
+
+    @Override
+    public void enter(final PathArgument child) {
+        current = current.node(child);
+    }
+
+    @Override
+    public void enter(final PathArgument... path) {
+        enter(Arrays.asList(path));
+    }
+
+    @Override
+    public void enter(final Iterable<PathArgument> path) {
+        path.forEach(this::enter);
+    }
+
+    @Override
+    public void exit() {
+        final YangInstanceIdentifier parent = current.getParent();
+        Preconditions.checkState(parent != null);
+        current = parent;
+    }
+
+    @Override
+    public void exit(final int depth) {
+        for (int i = 0; i < depth; ++i) {
+            exit();
+        }
+    }
+
+    @Override
+    public void close() {
+        parent.closeCursor(this);
+    }
+
+    @Override
+    public void delete(final PathArgument child) {
+        parent.delete(current.node(child));
+    }
+
+    @Override
+    public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
+        parent.merge(current.node(child), data);
+    }
+
+    @Override
+    public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
+        parent.write(current.node(child), data);
+    }
+}