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

©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.