Cleanup warnings
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / DOMBrokerTransactionChain.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.databroker;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 import java.util.concurrent.atomic.AtomicLong;
20 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 final class DOMBrokerTransactionChain extends AbstractDOMTransactionFactory<DOMStoreTransactionChain>
32         implements DOMTransactionChain {
33     private enum State {
34         RUNNING,
35         CLOSING,
36         CLOSED,
37         FAILED,
38     }
39
40     private static final AtomicIntegerFieldUpdater<DOMBrokerTransactionChain> COUNTER_UPDATER =
41             AtomicIntegerFieldUpdater.newUpdater(DOMBrokerTransactionChain.class, "counter");
42     private static final AtomicReferenceFieldUpdater<DOMBrokerTransactionChain, State> STATE_UPDATER =
43             AtomicReferenceFieldUpdater.newUpdater(DOMBrokerTransactionChain.class, State.class, "state");
44     private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerTransactionChain.class);
45     private final AtomicLong txNum = new AtomicLong();
46     private final AbstractDOMBroker broker;
47     private final TransactionChainListener listener;
48     private final long chainId;
49
50     private volatile State state = State.RUNNING;
51     private volatile int counter = 0;
52
53     /**
54      * Constructs an instance.
55      *
56      * @param chainId
57      *            ID of transaction chain
58      * @param chains
59      *            Backing {@link DOMStoreTransactionChain}s.
60      * @param listener
61      *            Listener, which listens on transaction chain events.
62      * @throws NullPointerException
63      *             If any of arguments is null.
64      */
65     DOMBrokerTransactionChain(final long chainId, final Map<LogicalDatastoreType, DOMStoreTransactionChain> chains,
66             final AbstractDOMBroker broker, final TransactionChainListener listener) {
67         super(chains);
68         this.chainId = chainId;
69         this.broker = Preconditions.checkNotNull(broker);
70         this.listener = Preconditions.checkNotNull(listener);
71     }
72
73     private void checkNotFailed() {
74         Preconditions.checkState(state != State.FAILED, "Transaction chain has failed");
75     }
76
77     @Override
78     protected Object newTransactionIdentifier() {
79         return "DOM-CHAIN-" + chainId + "-" + txNum.getAndIncrement();
80     }
81
82     @Override
83     public CheckedFuture<Void, TransactionCommitFailedException> submit(
84             final DOMDataWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
85         checkNotFailed();
86         checkNotClosed();
87
88         final CheckedFuture<Void, TransactionCommitFailedException> ret = broker.submit(transaction, cohorts);
89
90         COUNTER_UPDATER.incrementAndGet(this);
91         Futures.addCallback(ret, new FutureCallback<Void>() {
92             @Override
93             public void onSuccess(final Void result) {
94                 transactionCompleted();
95             }
96
97             @Override
98             public void onFailure(final Throwable failure) {
99                 transactionFailed(transaction, failure);
100             }
101         }, MoreExecutors.directExecutor());
102
103         return ret;
104     }
105
106     @Override
107     public void close() {
108         final boolean success = STATE_UPDATER.compareAndSet(this, State.RUNNING, State.CLOSING);
109         if (!success) {
110             LOG.debug("Chain {} is no longer running", this);
111             return;
112         }
113
114         super.close();
115         for (DOMStoreTransactionChain subChain : getTxFactories().values()) {
116             subChain.close();
117         }
118
119         if (counter == 0) {
120             finishClose();
121         }
122     }
123
124     private void finishClose() {
125         state = State.CLOSED;
126         listener.onTransactionChainSuccessful(this);
127     }
128
129     private void transactionCompleted() {
130         if (COUNTER_UPDATER.decrementAndGet(this) == 0 && state == State.CLOSING) {
131             finishClose();
132         }
133     }
134
135     private void transactionFailed(final DOMDataWriteTransaction tx, final Throwable cause) {
136         state = State.FAILED;
137         LOG.debug("Transaction chain {}¬†failed.", this, cause);
138         listener.onTransactionChainFailed(this, tx, cause);
139     }
140 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.