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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Optional;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import org.opendaylight.mdsal.common.api.ReadFailedException;
18 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
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;
26 * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
27 * of its calls to similar methods provided by underlying snapshot.
29 * @param <T> identifier type
32 public final class SnapshotBackedReadTransaction<T> extends
33 AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction, SnapshotBackedTransaction {
34 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
36 @SuppressWarnings("rawtypes")
37 private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
38 AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
41 // Guarded by stableSnapshot CAS, hence it does not need to be volatile
42 private TransactionClosePrototype<T> closeImpl;
43 private volatile DataTreeSnapshot stableSnapshot;
46 * Creates a new read-only transaction.
48 * @param identifier Transaction Identifier
49 * @param debug Enable transaction debugging
50 * @param snapshot Snapshot which will be modified.
52 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
53 final TransactionClosePrototype<T> closeImpl) {
54 super(identifier, debug);
55 this.stableSnapshot = requireNonNull(snapshot);
56 this.closeImpl = closeImpl;
57 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
62 final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
64 LOG.debug("Store transaction: {} : previously closed", getIdentifier());
68 LOG.debug("Store transaction: {} : Closed", getIdentifier());
69 if (closeImpl != null) {
70 closeImpl.transactionClosed(this);
75 @SuppressWarnings("checkstyle:IllegalCatch")
77 public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
78 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
79 requireNonNull(path, "Path must not be null.");
81 final DataTreeSnapshot snapshot = stableSnapshot;
82 if (snapshot == null) {
83 return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Transaction is closed"));
87 return FluentFutures.immediateFluentFuture(snapshot.readNode(path));
88 } catch (Exception e) {
89 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
90 return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Read failed", e));
95 public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
96 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
97 requireNonNull(path, "Path must not be null.");
99 return read(path).transform(Optional::isPresent, MoreExecutors.directExecutor());
103 public Optional<DataTreeSnapshot> getSnapshot() {
104 return Optional.ofNullable(stableSnapshot);
108 * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
111 * This class is intended to be implemented by Transaction factories responsible for allocation
112 * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
113 * providing underlying logic for applying implementation.
115 * @param <T> identifier type
117 public interface TransactionClosePrototype<T> {
119 * Called when a transaction is closed. This is not invoked at most once for every transaction.
121 * @param tx Transaction which got closed.
123 void transactionClosed(SnapshotBackedReadTransaction<T> tx);