2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.dom.spi.store;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import org.opendaylight.mdsal.common.api.ReadFailedException;
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
29 * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
30 * of its calls to similar methods provided by underlying snapshot.
32 * @param <T> identifier type
35 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
37 private volatile DataTreeSnapshot stableSnapshot;
40 * Creates a new read-only transaction.
42 * @param identifier Transaction Identifier
43 * @param debug Enable transaction debugging
44 * @param snapshot Snapshot which will be modified.
46 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
47 super(identifier, debug);
48 this.stableSnapshot = Preconditions.checkNotNull(snapshot);
49 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
54 LOG.debug("Store transaction: {} : Closed", getIdentifier());
55 stableSnapshot = null;
59 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
60 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
61 checkNotNull(path, "Path must not be null.");
63 final DataTreeSnapshot snapshot = stableSnapshot;
64 if (snapshot == null) {
65 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
69 return Futures.immediateCheckedFuture(snapshot.readNode(path));
70 } catch (final Exception e) {
71 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
72 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
77 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
78 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
79 checkNotNull(path, "Path must not be null.");
82 return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
83 } catch (final ReadFailedException e) {
84 return Futures.immediateFailedCheckedFuture(e);