Introduce ReadTransaction.exists() 67/77767/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Nov 2018 09:11:44 +0000 (10:11 +0100)
committerStephen Kitt <skitt@redhat.com>
Wed, 14 Nov 2018 15:47:21 +0000 (15:47 +0000)
This method exists in the DOM API, but for some reason it was left
out if the Binding API. Introduce it as default methods riding on
the read() method and override them in all known implementations,
so the request is efficiently routed.

JIRA: MDSAL-389
Change-Id: Id56332d6f720396305ac68e40554e62fd17c34fb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/ForwardingReadOnlyTransaction.java
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/ForwardingReadWriteTransaction.java
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/md/sal/binding/api/ReadTransaction.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/AbstractForwardedTransaction.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMReadTransactionAdapter.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMReadWriteTransactionAdapter.java

index a8fad51..a71c08a 100644 (file)
@@ -24,7 +24,7 @@ public class ForwardingReadOnlyTransaction extends ForwardingObject implements R
 
     private final ReadOnlyTransaction delegate;
 
-    protected ForwardingReadOnlyTransaction(ReadOnlyTransaction delegate) {
+    protected ForwardingReadOnlyTransaction(final ReadOnlyTransaction delegate) {
         this.delegate = delegate;
     }
 
@@ -34,11 +34,17 @@ public class ForwardingReadOnlyTransaction extends ForwardingObject implements R
     }
 
     @Override
-    public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(LogicalDatastoreType store,
-            InstanceIdentifier<T> path) {
+    public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(final LogicalDatastoreType store,
+            final InstanceIdentifier<T> path) {
         return delegate.read(store, path);
     }
 
+    @Override
+    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        return delegate.exists(store, path);
+    }
+
     @Override
     public Object getIdentifier() {
         return delegate.getIdentifier();
index 641e77b..3e8dfe7 100644 (file)
@@ -26,7 +26,7 @@ public class ForwardingReadWriteTransaction extends ForwardingObject implements
 
     private final ReadWriteTransaction delegate;
 
-    protected ForwardingReadWriteTransaction(ReadWriteTransaction delegate) {
+    protected ForwardingReadWriteTransaction(final ReadWriteTransaction delegate) {
         this.delegate = delegate;
     }
 
@@ -36,22 +36,29 @@ public class ForwardingReadWriteTransaction extends ForwardingObject implements
     }
 
     @Override
-    public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
+    public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
+            final T data) {
         delegate.put(store, path, data);
     }
 
     @Override
-    public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
-            boolean createMissingParents) {
+    public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
+            final T data, final boolean createMissingParents) {
         delegate.put(store, path, data, createMissingParents);
     }
 
     @Override
-    public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(LogicalDatastoreType store,
-            InstanceIdentifier<T> path) {
+    public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(final LogicalDatastoreType store,
+            final InstanceIdentifier<T> path) {
         return delegate.read(store, path);
     }
 
+    @Override
+    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        return delegate.exists(store, path);
+    }
+
     @Override
     public Object getIdentifier() {
         return delegate.getIdentifier();
@@ -63,13 +70,14 @@ public class ForwardingReadWriteTransaction extends ForwardingObject implements
     }
 
     @Override
-    public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
+    public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
+            final T data) {
         delegate.merge(store, path, data);
     }
 
     @Override
-    public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
-            boolean createMissingParents) {
+    public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
+            final T data, final boolean createMissingParents) {
         delegate.merge(store, path, data, createMissingParents);
     }
 
@@ -79,7 +87,7 @@ public class ForwardingReadWriteTransaction extends ForwardingObject implements
     }
 
     @Override
-    public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
+    public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
         delegate.delete(store, path);
     }
 }
index 21bcf23..01f5254 100644 (file)
@@ -9,6 +9,9 @@ package org.opendaylight.controller.md.sal.binding.api;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncReadTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
@@ -45,6 +48,38 @@ public interface ReadTransaction extends AsyncReadTransaction<InstanceIdentifier
      *         {@link ReadFailedException} or an exception derived from ReadFailedException.</li>
      *         </ul>
      */
-    <T extends DataObject> CheckedFuture<Optional<T>,ReadFailedException> read(
+    <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(
             LogicalDatastoreType store, InstanceIdentifier<T> path);
+
+    /**
+     * Checks if data is available in the logical data store located at provided path.
+     *
+     * <p>
+     * Note: a successful result from this method makes no guarantee that a subsequent call to {@link #read}
+     * will succeed. It is possible that the data resides in a data store on a remote node and, if that
+     * node goes down or a network failure occurs, a subsequent read would fail. Another scenario is if
+     * the data is deleted in between the calls to <code>exists</code> and <code>read</code>
+     *
+     * <p>
+     * Default implementation delegates to {@link #read(LogicalDatastoreType, InstanceIdentifier)}, implementations
+     * are advised to provide a more efficient override.
+     *
+     * @param store
+     *            Logical data store from which read should occur.
+     * @param path
+     *            Path which uniquely identifies subtree which client want to
+     *            check existence of
+     * @return a CheckFuture containing the result of the check.
+     *         <ul>
+     *         <li>If the data at the supplied path exists, the Future returns a Boolean
+     *         whose value is true, false otherwise</li>
+     *         <li>If checking for the data fails, the Future will fail with a
+     *         {@link ReadFailedException} or an exception derived from ReadFailedException.</li>
+     *         </ul>
+     */
+    default CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        return MappingCheckedFuture.create(Futures.transform(read(store, path), Optional::isPresent,
+            MoreExecutors.directExecutor()), ReadFailedException.MAPPER);
+    }
 }
index fe8c758..24b7ff0 100644 (file)
@@ -57,7 +57,7 @@ abstract class AbstractForwardedTransaction<T extends AsyncTransaction<YangInsta
         return codec;
     }
 
-    protected final <D extends DataObject> CheckedFuture<Optional<D>,ReadFailedException> doRead(
+    protected final <D extends DataObject> CheckedFuture<Optional<D>, ReadFailedException> doRead(
             final DOMDataReadTransaction readTx, final LogicalDatastoreType store,
             final InstanceIdentifier<D> path) {
         Preconditions.checkArgument(!path.isWildcarded(), "Invalid read of wildcarded path %s", path);
@@ -68,4 +68,13 @@ abstract class AbstractForwardedTransaction<T extends AsyncTransaction<YangInsta
                 MoreExecutors.directExecutor()),
             ReadFailedException.MAPPER);
     }
+
+    protected final CheckedFuture<Boolean, ReadFailedException> doExists(
+            final DOMDataReadTransaction readTx, final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        Preconditions.checkArgument(!path.isWildcarded(), "Invalid read of wildcarded path %s", path);
+
+        return MappingCheckedFuture.create(readTx.exists(store, codec.toYangInstanceIdentifierBlocking(path)),
+            ReadFailedException.MAPPER);
+    }
 }
index 1d69322..7a8414c 100644 (file)
@@ -25,11 +25,17 @@ class BindingDOMReadTransactionAdapter extends AbstractForwardedTransaction<DOMD
     }
 
     @Override
-    public <T extends DataObject> CheckedFuture<Optional<T>,ReadFailedException> read(
+    public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(
             final LogicalDatastoreType store, final InstanceIdentifier<T> path) {
         return doRead(getDelegate(),store, path);
     }
 
+    @Override
+    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        return doExists(getDelegate(), store, path);
+    }
+
     @Override
     public void close() {
         getDelegate().close();
index bebed4d..d3764fb 100644 (file)
@@ -29,4 +29,10 @@ class BindingDOMReadWriteTransactionAdapter extends
             final LogicalDatastoreType store, final InstanceIdentifier<T> path) {
         return doRead(getDelegate(), store, path);
     }
+
+    @Override
+    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+            final InstanceIdentifier<?> path) {
+        return doExists(getDelegate(), store, path);
+    }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.