eaf3aa7913a5dd03f1f29684471311498b2c0c72
[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 @Beta
35 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
36         implements DOMStoreReadTransaction {
37     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
38
39     @SuppressWarnings("rawtypes")
40     private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
41             AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
42                 "stableSnapshot");
43
44     // Guarded by stableSnapshot CAS, hence it does not need to be volatile
45     private TransactionClosePrototype<T> closeImpl;
46     private volatile DataTreeSnapshot stableSnapshot;
47
48     /**
49      * Creates a new read-only transaction.
50      *
51      * @param identifier Transaction Identifier
52      * @param debug Enable transaction debugging
53      * @param snapshot Snapshot which will be modified.
54      */
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);
61     }
62
63     @Override
64     public void close() {
65         final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
66         if (prev == null) {
67             LOG.debug("Store transaction: {} : previously closed", getIdentifier());
68             return;
69         }
70
71         LOG.debug("Store transaction: {} : Closed", getIdentifier());
72         if (closeImpl != null) {
73             closeImpl.transactionClosed(this);
74             closeImpl = null;
75         }
76     }
77
78     @Override
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.");
83
84         final DataTreeSnapshot snapshot = stableSnapshot;
85         if (snapshot == null) {
86             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
87         }
88
89         try {
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));
94         }
95     }
96
97     @Override
98     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
99         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
100         checkNotNull(path, "Path must not be null.");
101
102         try {
103             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
104         } catch (ReadFailedException e) {
105             return Futures.immediateFailedCheckedFuture(e);
106         }
107     }
108
109     /**
110      * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
111      *
112      * <p>
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.
116      *
117      * @param <T> identifier type
118      */
119     public interface TransactionClosePrototype<T> {
120         /**
121          * Called when a transaction is closed. This is not invoked at most once for every transaction.
122          *
123          * @param tx Transaction which got closed.
124          */
125         void transactionClosed(SnapshotBackedReadTransaction<T> tx);
126     }
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.