Deprecate all MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / sal / core / spi / data / 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.controller.sal.core.spi.data;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
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;
24
25 /**
26  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}.
27  *
28  * <p>
29  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
30  * which delegates most of its calls to similar methods provided by underlying snapshot.
31  *
32  * @param <T> identifier type
33  *
34  * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} instead.
35  */
36 @Deprecated
37 @Beta
38 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
39         implements DOMStoreReadTransaction {
40     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
41
42     @SuppressWarnings("rawtypes")
43     private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
44             AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
45                 "stableSnapshot");
46
47     // Guarded by stableSnapshot CAS, hence it does not need to be volatile
48     private TransactionClosePrototype<T> closeImpl;
49     private volatile DataTreeSnapshot stableSnapshot;
50
51     /**
52      * Creates a new read-only transaction.
53      *
54      * @param identifier Transaction Identifier
55      * @param debug Enable transaction debugging
56      * @param snapshot Snapshot which will be modified.
57      */
58     SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
59             final TransactionClosePrototype<T> closeImpl) {
60         super(identifier, debug);
61         this.stableSnapshot = Preconditions.checkNotNull(snapshot);
62         this.closeImpl = closeImpl;
63         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
64     }
65
66     @Override
67     public void close() {
68         final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
69         if (prev == null) {
70             LOG.debug("Store transaction: {} : previously closed", getIdentifier());
71             return;
72         }
73
74         LOG.debug("Store transaction: {} : Closed", getIdentifier());
75         if (closeImpl != null) {
76             closeImpl.transactionClosed(this);
77             closeImpl = null;
78         }
79     }
80
81     @Override
82     @SuppressWarnings("checkstyle:IllegalCatch")
83     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
84         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
85         checkNotNull(path, "Path must not be null.");
86
87         final DataTreeSnapshot snapshot = stableSnapshot;
88         if (snapshot == null) {
89             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
90         }
91
92         try {
93             return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
94         } catch (RuntimeException e) {
95             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
96             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
97         }
98     }
99
100     @Override
101     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
102         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
103         checkNotNull(path, "Path must not be null.");
104
105         try {
106             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
107         } catch (ReadFailedException e) {
108             return Futures.immediateFailedCheckedFuture(e);
109         }
110     }
111
112     /**
113      * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
114      *
115      * <p>
116      * This class is intended to be implemented by Transaction factories responsible for allocation
117      * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
118      * providing underlying logic for applying implementation.
119      *
120      * @param <T> identifier type
121      */
122     public interface TransactionClosePrototype<T> {
123         /**
124          * Called when a transaction is closed. This is not invoked at most once for every transaction.
125          *
126          * @param tx Transaction which got closed.
127          */
128         void transactionClosed(SnapshotBackedReadTransaction<T> tx);
129     }
130 }