2 * Copyright (c) 2015 Cisco Systems 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.dsbenchmark.txchain;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
14 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.dsbenchmark.BaListBuilder;
25 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
36 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
37 private final DataBroker bindingDataBroker;
38 private List<OuterList> list;
40 public TxchainBaWrite(DataBroker bindingDataBroker, Operation oper,
41 int outerListElem, int innerListElem, long writesPerTx, DataStore dataStore) {
42 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
43 this.bindingDataBroker = bindingDataBroker;
44 LOG.info("Created TxchainBaWrite");
48 public void createList() {
49 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
53 public void executeList() {
57 BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
58 WriteTransaction tx = chain.newWriteOnlyTransaction();
59 LogicalDatastoreType dsType = getDataStoreType();
61 for (OuterList element : this.list) {
62 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
63 .child(OuterList.class, element.getKey());
65 if (oper == StartTestInput.Operation.PUT) {
66 tx.put(dsType, iid, element);
68 tx.merge(dsType, iid, element);
73 if (writeCnt == writesPerTx) {
75 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
77 public void onSuccess(final Void result) {
82 public void onFailure(final Throwable t) {
83 LOG.error("Transaction failed, {}", t);
87 tx = chain.newWriteOnlyTransaction();
88 dsType = getDataStoreType();
93 // *** Clean up and close the transaction chain ***
94 // Submit the outstanding transaction even if it's empty and wait for it to finish
95 // We need to empty the transaction chain before closing it
98 tx.submit().checkedGet();
100 } catch (TransactionCommitFailedException e) {
101 LOG.error("Transaction failed", e);
106 } catch (IllegalStateException e) {
107 LOG.error("Transaction close failed,", e);
109 LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
113 public void onTransactionChainFailed(TransactionChain<?, ?> chain,
114 AsyncTransaction<?, ?> transaction, Throwable cause) {
115 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
116 chain, transaction.getIdentifier(), cause);
120 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
121 LOG.info("DatastoreBaAbstractWrite closed successfully, chain {}", chain);