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 org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import com.google.common.util.concurrent.Futures;
26 * Implementation of Read-Write transaction which is backed by {@link DataTreeSnapshot}
27 * and executed according to {@link TransactionReadyPrototype}.
30 class SnapshotBackedReadWriteTransaction extends SnapshotBackedWriteTransaction
31 implements DOMStoreReadWriteTransaction {
33 private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadWriteTransaction.class);
36 * Creates new read-write transaction.
38 * @param identifier transaction Identifier
39 * @param snapshot Snapshot which will be modified.
40 * @param readyImpl Implementation of ready method.
42 protected SnapshotBackedReadWriteTransaction(final Object identifier, final DataTreeSnapshot snapshot,
43 final TransactionReadyPrototype store) {
44 super(identifier, snapshot, store);
48 public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
49 LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
50 checkNotNull(path, "Path must not be null.");
52 DataTreeModification dataView = getMutatedView();
53 if(dataView == null) {
54 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
58 return Futures.immediateCheckedFuture(dataView.readNode(path));
59 } catch (Exception e) {
60 LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
61 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
65 @Override public CheckedFuture<Boolean, ReadFailedException> exists(
66 YangInstanceIdentifier path) {
68 return Futures.immediateCheckedFuture(
69 read(path).checkedGet().isPresent());
70 } catch (ReadFailedException e) {
71 return Futures.immediateFailedCheckedFuture(e);