1bbf8f53e528cb4150019edcb2855d7659673bf7
[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,
30         org.opendaylight.mdsal.common.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     public ShardedDOMDataBrokerDelegatingTransactionChain(final Object txChainIdentifier, final SchemaContext schemaContext,
40                                                           final org.opendaylight.mdsal.dom.api.DOMDataBroker brokerDelegate,
41                                                           final TransactionChainListener txChainListener) {
42         checkNotNull(brokerDelegate);
43         this.schemaContext = checkNotNull(schemaContext);
44         this.txChainIdentifier = checkNotNull(txChainIdentifier);
45         this.txChainListener = checkNotNull(txChainListener);
46         this.txChainDelegate = brokerDelegate.createTransactionChain(this);
47         transactionMap = Maps.newHashMap();
48     }
49
50     @Override
51     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
52         final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
53         final DOMDataReadOnlyTransaction readTx =
54                 new ShardedDOMDataBrokerDelegatingReadTransaction(newTransactionIdentifier(), readTxDelegate);
55         transactionMap.put(readTxDelegate.getIdentifier(), readTx);
56
57         return readTx;
58     }
59
60     @Override
61     public DOMDataReadWriteTransaction newReadWriteTransaction() {
62         final Object readWriteTxId = newTransactionIdentifier();
63         final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
64         final DOMDataReadOnlyTransaction readTx =
65                 new ShardedDOMDataBrokerDelegatingReadTransaction(readWriteTxId, readTxDelegate);
66
67         final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
68         final DOMDataWriteTransaction writeTx =
69                 new ShardedDOMDataBrokerDelegatingWriteTransaction(readWriteTxId, writeTxDelegate);
70
71         final DOMDataReadWriteTransaction readWriteTx =
72                 new ShardedDOMDataBrokerDelegatingReadWriteTransaction(readWriteTxId, schemaContext,
73                         readTx, writeTx);
74         transactionMap.put(readTxDelegate.getIdentifier(), readWriteTx);
75         transactionMap.put(writeTxDelegate.getIdentifier(), readWriteTx);
76
77         return readWriteTx;
78     }
79
80     @Override
81     public DOMDataWriteTransaction newWriteOnlyTransaction() {
82         final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
83         final DOMDataWriteTransaction writeTx =
84                 new ShardedDOMDataBrokerDelegatingWriteTransaction(newTransactionIdentifier(), writeTxDelegate);
85         transactionMap.put(writeTxDelegate.getIdentifier(), writeTx);
86
87         return writeTx;
88     }
89
90     @Override
91     public void close() {
92         txChainDelegate.close();
93     }
94
95     @Override
96     public void onTransactionChainFailed(final TransactionChain<?, ?> transactionChain,
97                                          final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?> asyncTransaction,
98                                          final Throwable throwable) {
99         txChainListener.onTransactionChainFailed(
100                 this, transactionFromDelegate(asyncTransaction.getIdentifier()), throwable);
101     }
102
103     @Override
104     public void onTransactionChainSuccessful(final TransactionChain<?, ?> transactionChain) {
105         txChainListener.onTransactionChainSuccessful(this);
106     }
107
108     private AsyncTransaction<?, ?> transactionFromDelegate(final Object delegateId) {
109         Preconditions.checkState(transactionMap.containsKey(delegateId),
110                 "Delegate transaction {} is not present in transaction chain history", delegateId);
111         return transactionMap.get(delegateId);
112     }
113
114     private Object newTransactionIdentifier() {
115         return "DOM-CHAIN-" + txChainIdentifier + "-" + txNum.getAndIncrement();
116     }
117 }

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