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.controller.sal.core.spi.data;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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}.
29 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
30 * which delegates most of its calls to similar methods provided by underlying snapshot.
32 * @param <T> identifier type
35 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
36 implements DOMStoreReadTransaction {
37 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
39 @SuppressWarnings("rawtypes")
40 private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
41 AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
44 // Guarded by stableSnapshot CAS, hence it does not need to be volatile
45 private TransactionClosePrototype<T> closeImpl;
46 private volatile DataTreeSnapshot stableSnapshot;
49 * Creates a new read-only transaction.
51 * @param identifier Transaction Identifier
52 * @param debug Enable transaction debugging
53 * @param snapshot Snapshot which will be modified.
55 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
56 final TransactionClosePrototype<T> closeImpl) {
57 super(identifier, debug);
58 this.stableSnapshot = Preconditions.checkNotNull(snapshot);
59 this.closeImpl = closeImpl;
60 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
65 final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
67 LOG.debug("Store transaction: {} : previously closed", getIdentifier());
71 LOG.debug("Store transaction: {} : Closed", getIdentifier());
72 if (closeImpl != null) {
73 closeImpl.transactionClosed(this);
79 @SuppressWarnings("checkstyle:IllegalCatch")
80 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
81 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
82 checkNotNull(path, "Path must not be null.");
84 final DataTreeSnapshot snapshot = stableSnapshot;
85 if (snapshot == null) {
86 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
90 return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
91 } catch (RuntimeException e) {
92 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
93 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
98 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
99 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
100 checkNotNull(path, "Path must not be null.");
103 return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
104 } catch (ReadFailedException e) {
105 return Futures.immediateFailedCheckedFuture(e);
110 * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
113 * This class is intended to be implemented by Transaction factories responsible for allocation
114 * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
115 * providing underlying logic for applying implementation.
117 * @param <T> identifier type
119 public interface TransactionClosePrototype<T> {
121 * Called when a transaction is closed. This is not invoked at most once for every transaction.
123 * @param tx Transaction which got closed.
125 void transactionClosed(SnapshotBackedReadTransaction<T> tx);