Fix followerDistributedDataStore tear down
[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.util.concurrent.FluentFuture;
11 import java.util.Optional;
12 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
15 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * An implementation of {@link DOMStoreReadTransaction} backed by a {@link ClientSnapshot}. Used for standalone
21  * transactions.
22  *
23  * @author Robert Varga
24  */
25 final class ClientBackedReadTransaction extends ClientBackedTransaction<ClientSnapshot>
26         implements DOMStoreReadTransaction {
27     private static final AtomicReferenceFieldUpdater<ClientBackedReadTransaction, ClientBackedTransactionChain>
28         PARENT_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ClientBackedReadTransaction.class,
29             ClientBackedTransactionChain.class, "parent");
30
31     @SuppressWarnings("unused")
32     private volatile ClientBackedTransactionChain parent;
33
34     ClientBackedReadTransaction(final ClientSnapshot delegate, final @Nullable ClientBackedTransactionChain parent,
35             final @Nullable Throwable allocationContext) {
36         super(delegate, allocationContext);
37         this.parent = parent;
38     }
39
40     @Override
41     public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) {
42         return delegate().read(path);
43     }
44
45     @Override
46     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
47         return delegate().exists(path);
48     }
49
50     @Override
51     public void close() {
52         super.close();
53
54         final ClientBackedTransactionChain local = PARENT_UPDATER.getAndSet(this, null);
55         if (local != null) {
56             local.snapshotClosed(delegate());
57         }
58     }
59 }