Migrate OSGI compendium reference
[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 java.util.Objects.requireNonNull;
11
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;
23
24 /**
25  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}.
26  *
27  * <p>
28  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
29  * which delegates most of its calls to similar methods provided by underlying snapshot.
30  *
31  * @param <T> identifier type
32  *
33  * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} instead.
34  */
35 @Deprecated(forRemoval = true)
36 @Beta
37 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
38         implements DOMStoreReadTransaction {
39     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
40
41     @SuppressWarnings("rawtypes")
42     private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
43             AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
44                 "stableSnapshot");
45
46     // Guarded by stableSnapshot CAS, hence it does not need to be volatile
47     private TransactionClosePrototype<T> closeImpl;
48     private volatile DataTreeSnapshot stableSnapshot;
49
50     /**
51      * Creates a new read-only transaction.
52      *
53      * @param identifier Transaction Identifier
54      * @param debug Enable transaction debugging
55      * @param snapshot Snapshot which will be modified.
56      */
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);
63     }
64
65     @Override
66     public void close() {
67         final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
68         if (prev == null) {
69             LOG.debug("Store transaction: {} : previously closed", getIdentifier());
70             return;
71         }
72
73         LOG.debug("Store transaction: {} : Closed", getIdentifier());
74         if (closeImpl != null) {
75             closeImpl.transactionClosed(this);
76             closeImpl = null;
77         }
78     }
79
80     @Override
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.");
85
86         final DataTreeSnapshot snapshot = stableSnapshot;
87         if (snapshot == null) {
88             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
89         }
90
91         try {
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));
96         }
97     }
98
99     @Override
100     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
101         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
102         requireNonNull(path, "Path must not be null.");
103
104         try {
105             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
106         } catch (ReadFailedException e) {
107             return Futures.immediateFailedCheckedFuture(e);
108         }
109     }
110
111     /**
112      * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
113      *
114      * <p>
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.
118      *
119      * @param <T> identifier type
120      */
121     public interface TransactionClosePrototype<T> {
122         /**
123          * Called when a transaction is closed. This is not invoked at most once for every transaction.
124          *
125          * @param tx Transaction which got closed.
126          */
127         void transactionClosed(SnapshotBackedReadTransaction<T> tx);
128     }
129 }