Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[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 static com.google.common.base.Preconditions.checkNotNull;
11
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
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
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}
30  * which delegates most of its calls to similar methods provided by underlying snapshot.
31  *
32  */
33 final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction
34                                           implements DOMStoreReadTransaction {
35
36     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
37     private volatile DataTreeSnapshot stableSnapshot;
38
39     public SnapshotBackedReadTransaction(final Object identifier, final boolean debug, final DataTreeSnapshot snapshot) {
40         super(identifier, debug);
41         this.stableSnapshot = Preconditions.checkNotNull(snapshot);
42         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
43     }
44
45     @Override
46     public void close() {
47         LOG.debug("Store transaction: {} : Closed", getIdentifier());
48         stableSnapshot = null;
49     }
50
51     @Override
52     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
53         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
54         checkNotNull(path, "Path must not be null.");
55
56         final DataTreeSnapshot snapshot = stableSnapshot;
57         if (snapshot == null) {
58             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
59         }
60
61         try {
62             return Futures.immediateCheckedFuture(snapshot.readNode(path));
63         } catch (Exception e) {
64             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
65             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
66         }
67     }
68
69     @Override
70     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
71         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
72         checkNotNull(path, "Path must not be null.");
73
74         try {
75             return Futures.immediateCheckedFuture(
76                 read(path).checkedGet().isPresent());
77         } catch (ReadFailedException e) {
78             return Futures.immediateFailedCheckedFuture(e);
79         }
80     }
81 }