Merge "BUG 3045 : Use non-strict parsing in hello message."
[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 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  *
25  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
26  *
27  * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
28  * which delegates most of its calls to similar methods provided by underlying snapshot.
29  *
30  * <T> identifier type
31  */
32 @Beta
33 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction {
34     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
35     private volatile DataTreeSnapshot stableSnapshot;
36
37     /**
38      * Creates a new read-only transaction.
39      *
40      * @param identifier Transaction Identifier
41      * @param debug Enable transaction debugging
42      * @param snapshot Snapshot which will be modified.
43      */
44     SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
45         super(identifier, debug);
46         this.stableSnapshot = Preconditions.checkNotNull(snapshot);
47         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
48     }
49
50     @Override
51     public void close() {
52         LOG.debug("Store transaction: {} : Closed", getIdentifier());
53         stableSnapshot = null;
54     }
55
56     @Override
57     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
58         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
59         checkNotNull(path, "Path must not be null.");
60
61         final DataTreeSnapshot snapshot = stableSnapshot;
62         if (snapshot == null) {
63             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
64         }
65
66         try {
67             return Futures.immediateCheckedFuture(snapshot.readNode(path));
68         } catch (Exception e) {
69             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
70             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
71         }
72     }
73
74     @Override
75     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
76         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
77         checkNotNull(path, "Path must not be null.");
78
79         try {
80             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
81         } catch (ReadFailedException e) {
82             return Futures.immediateFailedCheckedFuture(e);
83         }
84     }
85 }

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