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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}.
28 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
29 * which delegates most of its calls to similar methods provided by underlying snapshot.
31 * @param <T> identifier type
33 * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} instead.
35 @Deprecated(forRemoval = true)
37 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
38 implements DOMStoreReadTransaction {
39 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
41 @SuppressWarnings("rawtypes")
42 private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
43 AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
46 // Guarded by stableSnapshot CAS, hence it does not need to be volatile
47 private TransactionClosePrototype<T> closeImpl;
48 private volatile DataTreeSnapshot stableSnapshot;
51 * Creates a new read-only transaction.
53 * @param identifier Transaction Identifier
54 * @param debug Enable transaction debugging
55 * @param snapshot Snapshot which will be modified.
57 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
58 final TransactionClosePrototype<T> closeImpl) {
59 super(identifier, debug);
60 this.stableSnapshot = requireNonNull(snapshot);
61 this.closeImpl = closeImpl;
62 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
67 final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
69 LOG.debug("Store transaction: {} : previously closed", getIdentifier());
73 LOG.debug("Store transaction: {} : Closed", getIdentifier());
74 if (closeImpl != null) {
75 closeImpl.transactionClosed(this);
81 @SuppressWarnings("checkstyle:IllegalCatch")
82 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
83 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
84 requireNonNull(path, "Path must not be null.");
86 final DataTreeSnapshot snapshot = stableSnapshot;
87 if (snapshot == null) {
88 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
92 return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
93 } catch (RuntimeException e) {
94 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
95 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
100 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
101 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
102 requireNonNull(path, "Path must not be null.");
105 return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
106 } catch (ReadFailedException e) {
107 return Futures.immediateFailedCheckedFuture(e);
112 * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
115 * This class is intended to be implemented by Transaction factories responsible for allocation
116 * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
117 * providing underlying logic for applying implementation.
119 * @param <T> identifier type
121 public interface TransactionClosePrototype<T> {
123 * Called when a transaction is closed. This is not invoked at most once for every transaction.
125 * @param tx Transaction which got closed.
127 void transactionClosed(SnapshotBackedReadTransaction<T> tx);