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;
11 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
12 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
26 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
28 * Implementation of read-only transaction backed by {@link DataTreeSnapshot}
29 * which delegates most of its calls to similar methods provided by underlying snapshot.
32 final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction
33 implements DOMStoreReadTransaction {
35 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
36 private DataTreeSnapshot stableSnapshot;
38 public SnapshotBackedReadTransaction(final Object identifier, final DataTreeSnapshot snapshot) {
40 this.stableSnapshot = Preconditions.checkNotNull(snapshot);
41 LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
46 LOG.debug("Store transaction: {} : Closed", getIdentifier());
47 stableSnapshot = null;
51 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
52 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
53 checkNotNull(path, "Path must not be null.");
55 if(stableSnapshot == null) {
56 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
60 return Futures.immediateCheckedFuture(stableSnapshot.readNode(path));
61 } catch (Exception e) {
62 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
63 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));