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