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 java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.dsbenchmark.BaListBuilder;
22 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
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.TestExec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
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(DataBroker bindingDataBroker, Operation oper,
40 int outerListElem, int innerListElem, long writesPerTx) {
41 super(oper, outerListElem, innerListElem, writesPerTx);
42 this.bindingDataBroker = bindingDataBroker;
43 LOG.info("Created TxchainBaWrite");
47 public void createList() {
48 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
52 public void executeList() {
56 BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
57 WriteTransaction tx = chain.newWriteOnlyTransaction();
59 for (OuterList element : this.list) {
60 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
61 .child(OuterList.class, element.getKey());
62 if (oper == StartTestInput.Operation.PUT) {
63 tx.put(LogicalDatastoreType.CONFIGURATION, iid, element);
65 tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element);
70 if (writeCnt == writesPerTx) {
72 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
74 public void onSuccess(final Void result) {
78 public void onFailure(final Throwable t) {
79 LOG.error("Transaction failed, {}", t);
83 tx = chain.newWriteOnlyTransaction();
88 // *** Clean up and close the transaction chain ***
89 // Submit the outstanding transaction even if it's empty and wait for it to finish
90 // We need to empty the transaction chain before closing it
93 tx.submit().checkedGet();
95 } catch (TransactionCommitFailedException e) {
96 LOG.error("Transaction failed", e);
102 catch (IllegalStateException e){
103 LOG.error("Transaction close failed,", e);
105 LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
109 public void onTransactionChainFailed(TransactionChain<?, ?> chain,
110 AsyncTransaction<?, ?> transaction, Throwable cause) {
111 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
112 chain, transaction.getIdentifier(), cause);
116 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
117 LOG.info("DatastoreBaAbstractWrite closed successfully, chain {}", chain);