Convert CDS implementation to use msdal APIs
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / ClientBackedReadTransaction.java
1 /*
2  * Copyright (c) 2016 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.cluster.databroker;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
14 import javax.annotation.Nullable;
15 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 /**
22  * An implementation of {@link DOMStoreReadTransaction} backed by a {@link ClientSnapshot}. Used for standalone
23  * transactions.
24  *
25  * @author Robert Varga
26  */
27 final class ClientBackedReadTransaction extends ClientBackedTransaction<ClientSnapshot>
28         implements DOMStoreReadTransaction {
29     private static final AtomicReferenceFieldUpdater<ClientBackedReadTransaction, ClientBackedTransactionChain>
30         PARENT_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ClientBackedReadTransaction.class,
31             ClientBackedTransactionChain.class, "parent");
32
33     @SuppressWarnings("unused")
34     private volatile ClientBackedTransactionChain parent;
35
36     ClientBackedReadTransaction(final ClientSnapshot delegate, @Nullable final ClientBackedTransactionChain parent,
37         @Nullable final Throwable allocationContext) {
38         super(delegate, allocationContext);
39         this.parent = parent;
40     }
41
42     @Override
43     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
44         return Futures.makeChecked(delegate().read(path), ReadFailedException.MAPPER);
45     }
46
47     @Override
48     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
49         return Futures.makeChecked(delegate().exists(path), ReadFailedException.MAPPER);
50     }
51
52     @Override
53     public void close() {
54         super.close();
55
56         final ClientBackedTransactionChain local = PARENT_UPDATER.getAndSet(this, null);
57         if (local != null) {
58             local.snapshotClosed(delegate());
59         }
60     }
61 }