688d1e37e78829b0893cb84550b78eda668306d6
[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 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 @Beta
34 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
35         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     @SuppressWarnings("checkstyle:IllegalCatch")
60     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
61         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
62         checkNotNull(path, "Path must not be null.");
63
64         final DataTreeSnapshot snapshot = stableSnapshot;
65         if (snapshot == null) {
66             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
67         }
68
69         try {
70             return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
71         } catch (RuntimeException e) {
72             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
73             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
74         }
75     }
76
77     @Override
78     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
79         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
80         checkNotNull(path, "Path must not be null.");
81
82         try {
83             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
84         } catch (ReadFailedException e) {
85             return Futures.immediateFailedCheckedFuture(e);
86         }
87     }
88 }

©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.