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