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 / legacy / sharded / adapter / ShardedDOMDataBrokerDelegatingTransactionChain.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
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Maps;
15 import java.util.Map;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
23 import org.opendaylight.mdsal.common.api.TransactionChain;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28
29 class ShardedDOMDataBrokerDelegatingTransactionChain implements DOMTransactionChain, org.opendaylight.mdsal.common
30         .api.TransactionChainListener {
31     private final org.opendaylight.mdsal.dom.api.DOMTransactionChain txChainDelegate;
32     private final SchemaContext schemaContext;
33     private final TransactionChainListener txChainListener;
34     private final Object txChainIdentifier;
35     private final AtomicLong txNum = new AtomicLong();
36
37     private final Map<Object, AsyncTransaction<?, ?>> transactionMap;
38
39     ShardedDOMDataBrokerDelegatingTransactionChain(final Object txChainIdentifier,
40                                                           final SchemaContext schemaContext,
41                                                           final org.opendaylight.mdsal.dom.api.DOMDataBroker
42                                                                   brokerDelegate,
43                                                           final TransactionChainListener txChainListener) {
44         checkNotNull(brokerDelegate);
45         this.schemaContext = checkNotNull(schemaContext);
46         this.txChainIdentifier = checkNotNull(txChainIdentifier);
47         this.txChainListener = checkNotNull(txChainListener);
48         this.txChainDelegate = brokerDelegate.createTransactionChain(this);
49         transactionMap = Maps.newHashMap();
50     }
51
52     @Override
53     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
54         final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
55         final DOMDataReadOnlyTransaction readTx = new ShardedDOMDataBrokerDelegatingReadTransaction(
56                 newTransactionIdentifier(), readTxDelegate);
57         transactionMap.put(readTxDelegate.getIdentifier(), readTx);
58
59         return readTx;
60     }
61
62     @Override
63     public DOMDataReadWriteTransaction newReadWriteTransaction() {
64         final Object readWriteTxId = newTransactionIdentifier();
65         final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
66         final DOMDataReadOnlyTransaction readTx = new ShardedDOMDataBrokerDelegatingReadTransaction(readWriteTxId,
67                                                                                                     readTxDelegate);
68
69         final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
70         final DOMDataWriteTransaction writeTx = new ShardedDOMDataBrokerDelegatingWriteTransaction(readWriteTxId,
71                                                                                                    writeTxDelegate);
72
73         final DOMDataReadWriteTransaction readWriteTx = new ShardedDOMDataBrokerDelegatingReadWriteTransaction(
74                 readWriteTxId, schemaContext, readTx, writeTx);
75         transactionMap.put(readTxDelegate.getIdentifier(), readWriteTx);
76         transactionMap.put(writeTxDelegate.getIdentifier(), readWriteTx);
77
78         return readWriteTx;
79     }
80
81     @Override
82     public DOMDataWriteTransaction newWriteOnlyTransaction() {
83         final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
84         final DOMDataWriteTransaction writeTx = new ShardedDOMDataBrokerDelegatingWriteTransaction(
85                 newTransactionIdentifier(), writeTxDelegate);
86         transactionMap.put(writeTxDelegate.getIdentifier(), writeTx);
87
88         return writeTx;
89     }
90
91     @Override
92     public void close() {
93         txChainDelegate.close();
94     }
95
96     @Override
97     public void onTransactionChainFailed(final TransactionChain<?, ?> transactionChain,
98                                          final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?>
99                                                  asyncTransaction,
100                                          final Throwable throwable) {
101         txChainListener
102                 .onTransactionChainFailed(this, transactionFromDelegate(asyncTransaction.getIdentifier()), throwable);
103     }
104
105     @Override
106     public void onTransactionChainSuccessful(final TransactionChain<?, ?> transactionChain) {
107         txChainListener.onTransactionChainSuccessful(this);
108     }
109
110     private AsyncTransaction<?, ?> transactionFromDelegate(final Object delegateId) {
111         Preconditions.checkState(transactionMap.containsKey(delegateId),
112                                  "Delegate transaction {} is not present in transaction chain history", delegateId);
113         return transactionMap.get(delegateId);
114     }
115
116     private Object newTransactionIdentifier() {
117         return "DOM-CHAIN-" + txChainIdentifier + "-" + txNum.getAndIncrement();
118     }
119 }

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