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,
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();
37 private final Map<Object, AsyncTransaction<?, ?>> transactionMap;
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();
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);
61 public DOMDataReadWriteTransaction newReadWriteTransaction() {
62 final Object readWriteTxId = newTransactionIdentifier();
63 final DOMDataTreeReadTransaction readTxDelegate = txChainDelegate.newReadOnlyTransaction();
64 final DOMDataReadOnlyTransaction readTx =
65 new ShardedDOMDataBrokerDelegatingReadTransaction(readWriteTxId, readTxDelegate);
67 final DOMDataTreeWriteTransaction writeTxDelegate = txChainDelegate.newWriteOnlyTransaction();
68 final DOMDataWriteTransaction writeTx =
69 new ShardedDOMDataBrokerDelegatingWriteTransaction(readWriteTxId, writeTxDelegate);
71 final DOMDataReadWriteTransaction readWriteTx =
72 new ShardedDOMDataBrokerDelegatingReadWriteTransaction(readWriteTxId, schemaContext,
74 transactionMap.put(readTxDelegate.getIdentifier(), readWriteTx);
75 transactionMap.put(writeTxDelegate.getIdentifier(), readWriteTx);
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);
92 txChainDelegate.close();
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);
104 public void onTransactionChainSuccessful(final TransactionChain<?, ?> transactionChain) {
105 txChainListener.onTransactionChainSuccessful(this);
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);
114 private Object newTransactionIdentifier() {
115 return "DOM-CHAIN-" + txChainIdentifier + "-" + txNum.getAndIncrement();