0f61bfb8ea9d980007f12483b8b5a83c59a6686e
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.spi.store;
9
10 import static java.util.Objects.requireNonNull;
11
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;
24
25 /**
26  * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
27  * of its calls to similar methods provided by underlying snapshot.
28  *
29  * @param <T> identifier type
30  */
31 @Beta
32 public final class SnapshotBackedReadTransaction<T> extends
33         AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction, SnapshotBackedTransaction {
34     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
35
36     @SuppressWarnings("rawtypes")
37     private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
38             AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
39                 "stableSnapshot");
40
41     // Guarded by stableSnapshot CAS, hence it does not need to be volatile
42     private TransactionClosePrototype<T> closeImpl;
43     private volatile DataTreeSnapshot stableSnapshot;
44
45     /**
46      * Creates a new read-only transaction.
47      *
48      * @param identifier Transaction Identifier
49      * @param debug Enable transaction debugging
50      * @param snapshot Snapshot which will be modified.
51      */
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);
58     }
59
60     @Override
61     public void close() {
62         final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
63         if (prev == null) {
64             LOG.debug("Store transaction: {} : previously closed", getIdentifier());
65             return;
66         }
67
68         LOG.debug("Store transaction: {} : Closed", getIdentifier());
69         if (closeImpl != null) {
70             closeImpl.transactionClosed(this);
71             closeImpl = null;
72         }
73     }
74
75     @SuppressWarnings("checkstyle:IllegalCatch")
76     @Override
77     public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
78         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
79         requireNonNull(path, "Path must not be null.");
80
81         final DataTreeSnapshot snapshot = stableSnapshot;
82         if (snapshot == null) {
83             return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Transaction is closed"));
84         }
85
86         try {
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));
91         }
92     }
93
94     @Override
95     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
96         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
97         requireNonNull(path, "Path must not be null.");
98
99         return read(path).transform(Optional::isPresent, MoreExecutors.directExecutor());
100     }
101
102     @Override
103     public Optional<DataTreeSnapshot> getSnapshot() {
104         return Optional.ofNullable(stableSnapshot);
105     }
106
107     /**
108      * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
109      *
110      * <p>
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.
114      *
115      * @param <T> identifier type
116      */
117     public interface TransactionClosePrototype<T> {
118         /**
119          * Called when a transaction is closed. This is not invoked at most once for every transaction.
120          *
121          * @param tx Transaction which got closed.
122          */
123         void transactionClosed(SnapshotBackedReadTransaction<T> tx);
124     }
125 }