fe95c8e5365998a85e9559e061b507159e0d99de
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadWriteTransaction.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 com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
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-Write transaction which is backed by {@link DataTreeSnapshot}
26  * and executed according to {@link TransactionReadyPrototype}.
27  *
28  * @param <T> identifier type
29  */
30 @Beta
31 public final class SnapshotBackedReadWriteTransaction<T> extends
32         SnapshotBackedWriteTransaction<T> implements DOMStoreReadWriteTransaction {
33     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadWriteTransaction.class);
34
35     SnapshotBackedReadWriteTransaction(final T identifier, final boolean debug,
36             final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
37         super(identifier, debug, snapshot, readyImpl);
38     }
39
40     @SuppressWarnings("checkstyle:IllegalCatch")
41     @Override
42     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
43         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
44         checkNotNull(path, "Path must not be null.");
45
46         final Optional<NormalizedNode<?, ?>> result;
47
48         try {
49             result = readSnapshotNode(path);
50         } catch (Exception e) {
51             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
52             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
53         }
54
55         if (result == null) {
56             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
57         } else {
58             return Futures.immediateCheckedFuture(result);
59         }
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @Override
64     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
65         try {
66             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
67         } catch (ReadFailedException e) {
68             return Futures.immediateFailedCheckedFuture(e);
69         }
70     }
71 }