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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
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.AbstractDOMStoreTransaction;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
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}
29 * which delegates most of its calls to similar methods provided by underlying snapshot.
32 final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction<Object>
33 implements DOMStoreReadTransaction {
35 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
36 private volatile DataTreeSnapshot stableSnapshot;
38 public SnapshotBackedReadTransaction(final Object identifier, final boolean debug, final DataTreeSnapshot snapshot) {
39 super(identifier, debug);
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 final DataTreeSnapshot snapshot = stableSnapshot;
56 if (snapshot == null) {
57 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
61 return Futures.immediateCheckedFuture(snapshot.readNode(path));
62 } catch (Exception e) {
63 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
64 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
69 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
70 LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
71 checkNotNull(path, "Path must not be null.");
74 return Futures.immediateCheckedFuture(
75 read(path).checkedGet().isPresent());
76 } catch (ReadFailedException e) {
77 return Futures.immediateFailedCheckedFuture(e);