Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[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
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
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.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Implementation of Read-Write transaction which is backed by {@link DataTreeSnapshot}
27  * and executed according to {@link TransactionReadyPrototype}.
28  *
29  */
30 class SnapshotBackedReadWriteTransaction extends SnapshotBackedWriteTransaction
31                                          implements DOMStoreReadWriteTransaction {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadWriteTransaction.class);
34
35     /**
36      * Creates new read-write transaction.
37      *
38      * @param identifier transaction Identifier
39      * @param snapshot Snapshot which will be modified.
40      * @param readyImpl Implementation of ready method.
41      */
42     protected SnapshotBackedReadWriteTransaction(final Object identifier, final boolean debug,
43             final DataTreeSnapshot snapshot, final TransactionReadyPrototype store) {
44         super(identifier, debug, snapshot, store);
45     }
46
47     @Override
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.");
51
52         DataTreeModification dataView = getMutatedView();
53         if(dataView == null) {
54             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
55         }
56
57         try {
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));
62         }
63     }
64
65     @Override
66     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
67         try {
68             return Futures.immediateCheckedFuture(
69                 read(path).checkedGet().isPresent());
70         } catch (ReadFailedException e) {
71             return Futures.immediateFailedCheckedFuture(e);
72         }
73     }
74 }