Adopt odlparent-10.0.0/yangtools-8.0.0-SNAPSHOT
[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 java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
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.tree.api.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} and executed according
26  * to {@link SnapshotBackedWriteTransaction.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 FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
43         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
44         requireNonNull(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 FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Read failed", e));
53         }
54
55         if (result == null) {
56             return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Transaction is closed"));
57         }
58
59         return FluentFutures.immediateFluentFuture(result);
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @Override
64     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
65         return read(path).transform(Optional::isPresent, MoreExecutors.directExecutor());
66     }
67 }