2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
10 import java.util.HashMap;
12 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
13 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
17 import org.slf4j.Logger;
20 * A factory for creating DOM transactions, either normal or chained.
22 * @author Thomas Pantelis
24 public class DOMTransactionFactory {
26 private final Map<String, DOMStoreTransactionChain> transactionChains = new HashMap<>();
27 private final InMemoryDOMDataStore store;
28 private final ShardStats shardMBean;
29 private final Logger log;
30 private final String name;
32 public DOMTransactionFactory(InMemoryDOMDataStore store, ShardStats shardMBean, Logger log, String name) {
34 this.shardMBean = shardMBean;
39 @SuppressWarnings("unchecked")
40 public <T extends DOMStoreTransaction> T newTransaction(TransactionProxy.TransactionType type,
41 String transactionID, String transactionChainID) {
43 DOMStoreTransactionFactory factory = store;
45 if(!transactionChainID.isEmpty()) {
46 factory = transactionChains.get(transactionChainID);
48 if(log.isDebugEnabled()) {
49 log.debug("{}: Creating transaction with ID {} from chain {}", name, transactionID,
53 DOMStoreTransactionChain transactionChain = store.createTransactionChain();
54 transactionChains.put(transactionChainID, transactionChain);
55 factory = transactionChain;
58 log.debug("{}: Creating transaction with ID {}", name, transactionID);
64 transaction = (T) factory.newReadOnlyTransaction();
65 shardMBean.incrementReadOnlyTransactionCount();
68 transaction = (T) factory.newReadWriteTransaction();
69 shardMBean.incrementReadWriteTransactionCount();
72 transaction = (T) factory.newWriteOnlyTransaction();
73 shardMBean.incrementWriteOnlyTransactionCount();
80 public void closeTransactionChain(String transactionChainID) {
81 DOMStoreTransactionChain chain =
82 transactionChains.remove(transactionChainID);
89 public void closeAllTransactionChains() {
90 for(Map.Entry<String, DOMStoreTransactionChain> entry : transactionChains.entrySet()){
91 entry.getValue().close();
94 transactionChains.clear();