Merge "Get some basic unit testing in place for the RaftActor class"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / 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.md.sal.dom.store.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import static com.google.common.base.Preconditions.checkNotNull;
23
24 /**
25  *
26  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
27  *
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  */
32 final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction
33                                           implements DOMStoreReadTransaction {
34
35     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
36     private volatile DataTreeSnapshot stableSnapshot;
37
38     public SnapshotBackedReadTransaction(final Object identifier, final DataTreeSnapshot snapshot) {
39         super(identifier);
40         this.stableSnapshot = Preconditions.checkNotNull(snapshot);
41         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
42     }
43
44     @Override
45     public void close() {
46         LOG.debug("Store transaction: {} : Closed", getIdentifier());
47         stableSnapshot = null;
48     }
49
50     @Override
51     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
52         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
53         checkNotNull(path, "Path must not be null.");
54
55         final DataTreeSnapshot snapshot = stableSnapshot;
56         if (snapshot == null) {
57             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
58         }
59
60         try {
61             return Futures.immediateCheckedFuture(snapshot.readNode(path));
62         } catch (Exception e) {
63             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
64             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
65         }
66     }
67
68     @Override
69     public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
70         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
71         checkNotNull(path, "Path must not be null.");
72
73         try {
74             return Futures.immediateCheckedFuture(
75                 read(path).checkedGet().isPresent());
76         } catch (ReadFailedException e) {
77             return Futures.immediateFailedCheckedFuture(e);
78         }
79     }
80 }