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.controller.sal.core.spi.data;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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;
26 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
28 * Implementation of read-only transaction backed by {@link DataTreeSnapshot} which delegates most
29 * of its calls to similar methods provided by underlying snapshot.
31 * @param <T> identifier type
34 public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction {
35 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
36 private volatile DataTreeSnapshot stableSnapshot;
39 * Creates a new read-only transaction.
41 * @param identifier Transaction Identifier
42 * @param debug Enable transaction debugging
43 * @param snapshot Snapshot which will be modified.
45 SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
46 super(identifier, debug);
47 this.stableSnapshot = Preconditions.checkNotNull(snapshot);
48 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
53 LOG.debug("Store transaction: {} : Closed", getIdentifier());
54 stableSnapshot = null;
58 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
59 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
60 checkNotNull(path, "Path must not be null.");
62 final DataTreeSnapshot snapshot = stableSnapshot;
63 if (snapshot == null) {
64 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
68 return Futures.immediateCheckedFuture(snapshot.readNode(path));
69 } catch (final Exception e) {
70 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
71 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
76 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
77 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
78 checkNotNull(path, "Path must not be null.");
81 return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
82 } catch (final ReadFailedException e) {
83 return Futures.immediateFailedCheckedFuture(e);