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 static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.dsbenchmark.BaListBuilder;
17 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.Empty;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class TxchainBaWrite extends DatastoreAbstractWriter implements FutureCallback<Empty> {
31 private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
33 private final DataBroker dataBroker;
34 private List<OuterList> list = null;
36 public TxchainBaWrite(final DataBroker dataBroker, final Operation oper, final int outerListElem,
37 final int innerListElem, final long writesPerTx, final DataStore dataStore) {
38 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
39 this.dataBroker = requireNonNull(dataBroker);
40 LOG.debug("Created TxchainBaWrite");
44 public void createList() {
45 list = BaListBuilder.buildOuterList(outerListElem, innerListElem);
49 public void executeList() {
50 final var chain = dataBroker.createMergingTransactionChain();
51 chain.addCallback(this);
52 final var dsType = getDataStoreType();
54 var tx = chain.newWriteOnlyTransaction();
58 for (var element : list) {
59 final var iid = InstanceIdentifier.create(TestExec.class).child(OuterList.class, element.key());
61 if (oper == StartTestInput.Operation.PUT) {
62 tx.put(dsType, iid, element);
64 tx.merge(dsType, iid, element);
69 if (writeCnt == writesPerTx) {
71 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
73 public void onSuccess(final CommitInfo result) {
78 public void onFailure(final Throwable cause) {
79 LOG.error("Transaction failed", cause);
82 }, MoreExecutors.directExecutor());
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
95 } catch (final InterruptedException | ExecutionException e) {
96 LOG.error("Transaction failed", e);
101 } catch (final IllegalStateException e) {
102 LOG.error("Transaction close failed,", e);
104 LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
108 public void onFailure(final Throwable cause) {
109 LOG.error("Broken chain in DatastoreBaAbstractWrite", cause);
113 public void onSuccess(final Empty result) {
114 LOG.debug("DatastoreBaAbstractWrite closed successfully");