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.MoreExecutors;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.opendaylight.dsbenchmark.BaListBuilder;
23 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
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.TestExec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
35 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
36 private final DataBroker bindingDataBroker;
37 private List<OuterList> list;
39 public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
40 final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
41 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
42 this.bindingDataBroker = bindingDataBroker;
43 LOG.debug("Created TxchainBaWrite");
47 public void createList() {
48 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
52 public void executeList() {
53 final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
54 final LogicalDatastoreType dsType = getDataStoreType();
56 WriteTransaction tx = chain.newWriteOnlyTransaction();
60 for (OuterList element : this.list) {
61 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
62 .child(OuterList.class, element.key());
64 if (oper == StartTestInput.Operation.PUT) {
65 tx.put(dsType, iid, element);
67 tx.merge(dsType, iid, element);
72 if (writeCnt == writesPerTx) {
74 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
76 public void onSuccess(final CommitInfo result) {
81 public void onFailure(final Throwable t) {
82 LOG.error("Transaction failed, {}", t);
85 }, MoreExecutors.directExecutor());
86 tx = chain.newWriteOnlyTransaction();
91 // *** Clean up and close the transaction chain ***
92 // Submit the outstanding transaction even if it's empty and wait for it to finish
93 // We need to empty the transaction chain before closing it
98 } catch (final InterruptedException | ExecutionException e) {
99 LOG.error("Transaction failed", e);
104 } catch (final IllegalStateException e) {
105 LOG.error("Transaction close failed,", e);
107 LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
111 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
112 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
113 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
114 chain, transaction.getIdentifier(), cause);
118 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
119 LOG.debug("DatastoreBaAbstractWrite closed successfully, chain {}", chain);