2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.spi.store;
10 import static java.util.Objects.requireNonNull;
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.api.schema.tree.DataTreeSnapshot;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
26 * of its calls to similar methods provided by underlying snapshot.
28 * @param <T> identifier type
31 public final class SnapshotBackedReadTransaction<T> extends
32 AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction, SnapshotBackedTransaction {
34 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
35 private volatile DataTreeSnapshot stableSnapshot;
38 * Creates a new read-only transaction.
40 * @param identifier Transaction Identifier
41 * @param debug Enable transaction debugging
42 * @param snapshot Snapshot which will be modified.
44 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
45 super(identifier, debug);
46 this.stableSnapshot = requireNonNull(snapshot);
47 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
52 LOG.debug("Store transaction: {} : Closed", getIdentifier());
53 stableSnapshot = null;
56 @SuppressWarnings("checkstyle:IllegalCatch")
58 public FluentFuture<Optional<NormalizedNode<?,?>>> read(final YangInstanceIdentifier path) {
59 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
60 requireNonNull(path, "Path must not be null.");
62 final DataTreeSnapshot snapshot = stableSnapshot;
63 if (snapshot == null) {
64 return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Transaction is closed"));
68 return FluentFutures.immediateFluentFuture(snapshot.readNode(path));
69 } catch (Exception e) {
70 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
71 return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Read failed", e));
76 public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
77 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
78 requireNonNull(path, "Path must not be null.");
80 return read(path).transform(Optional::isPresent, MoreExecutors.directExecutor());
84 public java.util.Optional<DataTreeSnapshot> getSnapshot() {
85 return java.util.Optional.ofNullable(stableSnapshot);