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
8 package org.opendaylight.dsbenchmark.txchain;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.dsbenchmark.BaListBuilder;
15 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.Transaction;
18 import org.opendaylight.mdsal.binding.api.TransactionChain;
19 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
33 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
34 private final DataBroker bindingDataBroker;
35 private List<OuterList> list;
37 public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
38 final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
39 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
40 this.bindingDataBroker = bindingDataBroker;
41 LOG.debug("Created TxchainBaWrite");
45 public void createList() {
46 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
50 public void executeList() {
51 final TransactionChain chain = bindingDataBroker.createTransactionChain(this);
52 final LogicalDatastoreType dsType = getDataStoreType();
54 WriteTransaction tx = chain.newWriteOnlyTransaction();
58 for (OuterList element : this.list) {
59 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
60 .child(OuterList.class, element.key());
62 if (oper == StartTestInput.Operation.PUT) {
63 tx.put(dsType, iid, element);
65 tx.merge(dsType, iid, element);
70 if (writeCnt == writesPerTx) {
72 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
74 public void onSuccess(final CommitInfo result) {
79 public void onFailure(final Throwable cause) {
80 LOG.error("Transaction failed", cause);
83 }, MoreExecutors.directExecutor());
84 tx = chain.newWriteOnlyTransaction();
89 // *** Clean up and close the transaction chain ***
90 // Submit the outstanding transaction even if it's empty and wait for it to finish
91 // We need to empty the transaction chain before closing it
96 } catch (final InterruptedException | ExecutionException e) {
97 LOG.error("Transaction failed", e);
102 } catch (final IllegalStateException e) {
103 LOG.error("Transaction close failed,", e);
105 LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
109 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
110 final Throwable cause) {
111 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}", chain,
112 transaction.getIdentifier(), cause);
116 public void onTransactionChainSuccessful(final TransactionChain chain) {
117 LOG.debug("DatastoreBaAbstractWrite closed successfully, chain {}", chain);