BUG-5280: Close client history after all histories are closed
[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.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.sal.core.spi.data.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         super(delegate);
38         this.parent = parent;
39     }
40
41     @Override
42     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
43         return Futures.makeChecked(delegate().read(path), ReadFailedException.MAPPER);
44     }
45
46     @Override
47     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
48         return Futures.makeChecked(delegate().exists(path), ReadFailedException.MAPPER);
49     }
50
51     @Override
52     public void close() {
53         super.close();
54
55         final ClientBackedTransactionChain local = PARENT_UPDATER.getAndSet(this, null);
56         if (local != null) {
57             local.snapshotClosed(delegate());
58         }
59     }
60 }