7404324b31ad6523edc92b482144b5bf0870830f
[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.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  * Implementation of Read-Write transaction which is backed by {@link DataTreeSnapshot} and executed according
25  * to {@link SnapshotBackedWriteTransaction.TransactionReadyPrototype}.
26  *
27  * @param <T> identifier type
28  */
29 @Beta
30 public final class SnapshotBackedReadWriteTransaction<T> extends
31         SnapshotBackedWriteTransaction<T> implements DOMStoreReadWriteTransaction {
32     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadWriteTransaction.class);
33
34     SnapshotBackedReadWriteTransaction(final T identifier, final boolean debug,
35             final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
36         super(identifier, debug, snapshot, readyImpl);
37     }
38
39     @SuppressWarnings("checkstyle:IllegalCatch")
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
47         try {
48             result = Optional.fromJavaUtil(readSnapshotNode(path));
49         } catch (Exception e) {
50             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
51             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
52         }
53
54         if (result == null) {
55             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
56         }
57
58         return Futures.immediateCheckedFuture(result);
59     }
60
61     @SuppressWarnings("checkstyle:IllegalCatch")
62     @Override
63     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
64         try {
65             return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
66         } catch (ReadFailedException e) {
67             return Futures.immediateFailedCheckedFuture(e);
68         }
69     }
70 }