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;
12 import java.util.Random;
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.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.StartTestInput;
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 import com.google.common.util.concurrent.FutureCallback;
34 import com.google.common.util.concurrent.Futures;
36 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
37 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
38 private final DataBroker bindingDataBroker;
39 private List<OuterList> list;
41 public TxchainBaWrite(DataBroker bindingDataBroker, Operation oper,
42 int outerListElem, int innerListElem, long writesPerTx, DataStore dataStore) {
43 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
44 this.bindingDataBroker = bindingDataBroker;
45 LOG.info("Created TxchainBaWrite");
49 public void createList() {
50 list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
54 public void executeList() {
58 BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
59 WriteTransaction tx = chain.newWriteOnlyTransaction();
60 LogicalDatastoreType dsType = getDataStoreType();
62 for (OuterList element : this.list) {
63 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
64 .child(OuterList.class, element.getKey());
66 if (oper == StartTestInput.Operation.PUT) {
67 tx.put(dsType, iid, element);
69 tx.merge(dsType, iid, element);
74 if (writeCnt == writesPerTx) {
76 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
78 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);
107 catch (IllegalStateException e){
108 LOG.error("Transaction close failed,", e);
110 LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
114 public void onTransactionChainFailed(TransactionChain<?, ?> chain,
115 AsyncTransaction<?, ?> transaction, Throwable cause) {
116 LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
117 chain, transaction.getIdentifier(), cause);
121 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
122 LOG.info("DatastoreBaAbstractWrite closed successfully, chain {}", chain);