X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-inmemory-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2FSnapshotBackedReadTransaction.java;h=2caa76d49dff02a34ad98232ccdc7db139f07f2c;hp=39d6483c525ed648ea8c7f922ce8c57ee0064af0;hb=315a10ec8b79abec3f4d718359ebb4202bffcb11;hpb=de3e413b633b7555ae8f3fe2ec163dbb7dda5da8 diff --git a/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SnapshotBackedReadTransaction.java b/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SnapshotBackedReadTransaction.java index 39d6483c52..2caa76d49d 100644 --- a/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SnapshotBackedReadTransaction.java +++ b/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SnapshotBackedReadTransaction.java @@ -7,20 +7,19 @@ */ package org.opendaylight.controller.md.sal.dom.store.impl; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; - -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; +import static com.google.common.base.Preconditions.checkNotNull; /** * @@ -30,10 +29,11 @@ import com.google.common.util.concurrent.ListenableFuture; * which delegates most of its calls to similar methods provided by underlying snapshot. * */ -final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements -DOMStoreReadTransaction { +final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction + implements DOMStoreReadTransaction { + private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class); - private DataTreeSnapshot stableSnapshot; + private volatile DataTreeSnapshot stableSnapshot; public SnapshotBackedReadTransaction(final Object identifier, final DataTreeSnapshot snapshot) { super(identifier); @@ -48,9 +48,33 @@ DOMStoreReadTransaction { } @Override - public ListenableFuture>> read(final YangInstanceIdentifier path) { + public CheckedFuture>, ReadFailedException> read(final YangInstanceIdentifier path) { + LOG.debug("Tx: {} Read: {}", getIdentifier(), path); checkNotNull(path, "Path must not be null."); - checkState(stableSnapshot != null, "Transaction is closed"); - return Futures.immediateFuture(stableSnapshot.readNode(path)); + + final DataTreeSnapshot snapshot = stableSnapshot; + if (snapshot == null) { + return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed")); + } + + try { + return Futures.immediateCheckedFuture(snapshot.readNode(path)); + } catch (Exception e) { + LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e); + return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e)); + } + } + + @Override + public CheckedFuture exists(YangInstanceIdentifier path) { + LOG.debug("Tx: {} Exists: {}", getIdentifier(), path); + checkNotNull(path, "Path must not be null."); + + try { + return Futures.immediateCheckedFuture( + read(path).checkedGet().isPresent()); + } catch (ReadFailedException e) { + return Futures.immediateFailedCheckedFuture(e); + } } -} \ No newline at end of file +}