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.md.sal.dom.store.impl;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
29 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
30 * which delegates most of its calls to similar methods provided by underlying snapshot.
33 final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction
34 implements DOMStoreReadTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
37 private volatile DataTreeSnapshot stableSnapshot;
39 public SnapshotBackedReadTransaction(final Object identifier, final boolean debug, final DataTreeSnapshot snapshot) {
40 super(identifier, debug);
41 this.stableSnapshot = Preconditions.checkNotNull(snapshot);
42 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
47 LOG.debug("Store transaction: {} : Closed", getIdentifier());
48 stableSnapshot = null;
52 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
53 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
54 checkNotNull(path, "Path must not be null.");
56 final DataTreeSnapshot snapshot = stableSnapshot;
57 if (snapshot == null) {
58 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
62 return Futures.immediateCheckedFuture(snapshot.readNode(path));
63 } catch (Exception e) {
64 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
65 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
70 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
71 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
72 checkNotNull(path, "Path must not be null.");
75 return Futures.immediateCheckedFuture(
76 read(path).checkedGet().isPresent());
77 } catch (ReadFailedException e) {
78 return Futures.immediateFailedCheckedFuture(e);