Add missing copyright text
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / SnapshotBackedReadWriteTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
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.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Implementation of Read-Write transaction which is backed by {@link DataTreeSnapshot}
24  * and executed according to {@link TransactionReadyPrototype}.
25  *
26  */
27 final class SnapshotBackedReadWriteTransaction extends SnapshotBackedWriteTransaction implements DOMStoreReadWriteTransaction {
28     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadWriteTransaction.class);
29
30     /**
31      * Creates new read-write transaction.
32      *
33      * @param identifier transaction Identifier
34      * @param snapshot Snapshot which will be modified.
35      * @param readyImpl Implementation of ready method.
36      */
37     protected SnapshotBackedReadWriteTransaction(final Object identifier, final boolean debug,
38             final DataTreeSnapshot snapshot, final TransactionReadyPrototype store) {
39         super(identifier, debug, snapshot, store);
40     }
41
42     @Override
43     public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
44         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
45         checkNotNull(path, "Path must not be null.");
46
47         final Optional<NormalizedNode<?, ?>> result;
48         try {
49             result = readSnapshotNode(path);
50         } catch (Exception e) {
51             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
52             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
53         }
54
55         if (result == null) {
56             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
57         } else {
58             return Futures.immediateCheckedFuture(result);
59         }
60     }
61
62     @Override
63     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
64         try {
65             return Futures.immediateCheckedFuture(
66                 read(path).checkedGet().isPresent());
67         } catch (ReadFailedException e) {
68             return Futures.immediateFailedCheckedFuture(e);
69         }
70     }
71 }