2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Maps;
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;
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();
37 private final Map<Object, AsyncTransaction<?, ?>> transactionMap;
39 ShardedDOMDataBrokerDelegatingTransactionChain(final Object txChainIdentifier,
40 final SchemaContext schemaContext,
41 final org.opendaylight.mdsal.dom.api.DOMDataBroker
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();
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);
63 public DOMDataReadWriteTransaction newReadWriteTransaction() {
64 final Object readWriteTxId = newTransactionIdentifier();
65 final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
66 final DOMDataReadOnlyTransaction readTx = new ShardedDOMDataBrokerDelegatingReadTransaction(readWriteTxId,
69 final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
70 final DOMDataWriteTransaction writeTx = new ShardedDOMDataBrokerDelegatingWriteTransaction(readWriteTxId,
73 final DOMDataReadWriteTransaction readWriteTx = new ShardedDOMDataBrokerDelegatingReadWriteTransaction(
74 readWriteTxId, schemaContext, readTx, writeTx);
75 transactionMap.put(readTxDelegate.getIdentifier(), readWriteTx);
76 transactionMap.put(writeTxDelegate.getIdentifier(), readWriteTx);
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);
93 txChainDelegate.close();
97 public void onTransactionChainFailed(final TransactionChain<?, ?> transactionChain,
98 final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?>
100 final Throwable throwable) {
102 .onTransactionChainFailed(this, transactionFromDelegate(asyncTransaction.getIdentifier()), throwable);
106 public void onTransactionChainSuccessful(final TransactionChain<?, ?> transactionChain) {
107 txChainListener.onTransactionChainSuccessful(this);
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);
116 private Object newTransactionIdentifier() {
117 return "DOM-CHAIN-" + txChainIdentifier + "-" + txNum.getAndIncrement();