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;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.dsbenchmark.BaListBuilder;
23 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
34 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
35 private final DataBroker bindingDataBroker;
36 private List<OuterList> list;
38 public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
39 final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
40 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
41 this.bindingDataBroker = bindingDataBroker;
42 LOG.debug("Created TxchainBaWrite");
46 public void createList() {
47 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
51 public void executeList() {
52 final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
53 final LogicalDatastoreType dsType = getDataStoreType();
55 WriteTransaction tx = chain.newWriteOnlyTransaction();
59 for (OuterList element : this.list) {
60 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
61 .child(OuterList.class, element.getKey());
63 if (oper == StartTestInput.Operation.PUT) {
64 tx.put(dsType, iid, element);
66 tx.merge(dsType, iid, element);
71 if (writeCnt == writesPerTx) {
73 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
75 public void onSuccess(final Void result) {
80 public void onFailure(final Throwable t) {
81 LOG.error("Transaction failed, {}", t);
85 tx = chain.newWriteOnlyTransaction();
90 // *** Clean up and close the transaction chain ***
91 // Submit the outstanding transaction even if it's empty and wait for it to finish
92 // We need to empty the transaction chain before closing it
95 tx.submit().checkedGet();
97 } catch (TransactionCommitFailedException e) {
98 LOG.error("Transaction failed", e);
103 } catch (IllegalStateException e) {
104 LOG.error("Transaction close failed,", e);
106 LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
110 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
111 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
112 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
113 chain, transaction.getIdentifier(), cause);
117 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
118 LOG.debug("DatastoreBaAbstractWrite closed successfully, chain {}", chain);