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

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