Report ExactDataObjectStep from DataObjectModification
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DOMDataBrokerTransactionChainImpl.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.mdsal.dom.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Map;
19 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
20 import java.util.concurrent.atomic.AtomicLong;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.common.api.CommitInfo;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
29 import org.opendaylight.yangtools.yang.common.Empty;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * NormalizedNode implementation of {@link org.opendaylight.mdsal.common.api.TransactionChain} which is backed
35  * by several {@link DOMStoreTransactionChain} differentiated by provided
36  * {@link org.opendaylight.mdsal.common.api.LogicalDatastoreType} type.
37  *
38  */
39 final class DOMDataBrokerTransactionChainImpl extends AbstractDOMForwardedTransactionFactory<DOMStoreTransactionChain>
40         implements DOMTransactionChain {
41     private enum State {
42         RUNNING,
43         CLOSING,
44         CLOSED,
45         FAILED,
46     }
47
48     private static final AtomicIntegerFieldUpdater<DOMDataBrokerTransactionChainImpl> COUNTER_UPDATER =
49             AtomicIntegerFieldUpdater.newUpdater(DOMDataBrokerTransactionChainImpl.class, "counter");
50     private static final AtomicReferenceFieldUpdater<DOMDataBrokerTransactionChainImpl, State> STATE_UPDATER =
51             AtomicReferenceFieldUpdater.newUpdater(DOMDataBrokerTransactionChainImpl.class, State.class, "state");
52     private static final Logger LOG = LoggerFactory.getLogger(DOMDataBrokerTransactionChainImpl.class);
53
54     private final @NonNull SettableFuture<Empty> future = SettableFuture.create();
55     private final AtomicLong txNum = new AtomicLong();
56     private final AbstractDOMDataBroker broker;
57     private final long chainId;
58
59     private volatile State state = State.RUNNING;
60     private volatile int counter = 0;
61
62     DOMDataBrokerTransactionChainImpl(final long chainId,
63             final Map<LogicalDatastoreType, DOMStoreTransactionChain> chains, final AbstractDOMDataBroker broker) {
64         super(chains);
65         this.chainId = chainId;
66         this.broker = requireNonNull(broker);
67     }
68
69     private void checkNotFailed() {
70         Preconditions.checkState(state != State.FAILED, "Transaction chain has failed");
71     }
72
73     @Override
74     public ListenableFuture<Empty> future() {
75         return future;
76     }
77
78     @Override
79     protected Object newTransactionIdentifier() {
80         return "DOM-CHAIN-" + chainId + "-" + txNum.getAndIncrement();
81     }
82
83     @Override
84     protected FluentFuture<? extends CommitInfo> commit(final DOMDataTreeWriteTransaction transaction,
85             final DOMStoreThreePhaseCommitCohort cohort) {
86         checkNotFailed();
87         checkNotClosed();
88
89         final var ret = broker.commit(transaction, cohort);
90         COUNTER_UPDATER.incrementAndGet(this);
91
92         ret.addCallback(new FutureCallback<CommitInfo>() {
93             @Override
94             public void onSuccess(final CommitInfo result) {
95                 transactionCompleted();
96             }
97
98             @Override
99             public void onFailure(final Throwable throwable) {
100                 transactionFailed(transaction, throwable);
101             }
102         }, MoreExecutors.directExecutor());
103
104         return ret;
105     }
106
107     @Override
108     public void close() {
109         final boolean success = STATE_UPDATER.compareAndSet(this, State.RUNNING, State.CLOSING);
110         if (!success) {
111             LOG.debug("Chain {} is no longer running", this);
112             return;
113         }
114
115         super.close();
116         for (DOMStoreTransactionChain subChain : getTxFactories().values()) {
117             subChain.close();
118         }
119
120         if (counter == 0) {
121             finishClose();
122         }
123     }
124
125     private void finishClose() {
126         state = State.CLOSED;
127         future.set(Empty.value());
128     }
129
130     private void transactionCompleted() {
131         if (COUNTER_UPDATER.decrementAndGet(this) == 0 && state == State.CLOSING) {
132             finishClose();
133         }
134     }
135
136     private void transactionFailed(final DOMDataTreeWriteTransaction tx, final Throwable cause) {
137         state = State.FAILED;
138         LOG.debug("Transaction chain {} failed.", this, cause);
139         future.setException(cause);
140     }
141 }