Merge "Added .gitreview"
[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 com.google.common.base.Preconditions.checkNotNull;
11
12 import org.opendaylight.mdsal.common.api.ReadFailedException;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
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  *
27  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
28  *
29  * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
30  * 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> implements DOMStoreReadTransaction {
36     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
37     private volatile DataTreeSnapshot stableSnapshot;
38
39     /**
40      * Creates a new read-only transaction.
41      *
42      * @param identifier Transaction Identifier
43      * @param debug Enable transaction debugging
44      * @param snapshot Snapshot which will be modified.
45      */
46     SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
47         super(identifier, debug);
48         this.stableSnapshot = Preconditions.checkNotNull(snapshot);
49         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
50     }
51
52     @Override
53     public void close() {
54         LOG.debug("Store transaction: {} : Closed", getIdentifier());
55         stableSnapshot = null;
56     }
57
58     @Override
59     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
60         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
61         checkNotNull(path, "Path must not be null.");
62
63         final DataTreeSnapshot snapshot = stableSnapshot;
64         if (snapshot == null) {
65             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
66         }
67
68         try {
69             return Futures.immediateCheckedFuture(snapshot.readNode(path));
70         } catch (final Exception e) {
71             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
72             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
73         }
74     }
75
76     @Override
77     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
78         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
79         checkNotNull(path, "Path must not be null.");
80
81         try {
82             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
83         } catch (final ReadFailedException e) {
84             return Futures.immediateFailedCheckedFuture(e);
85         }
86     }
87 }