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.DatastoreAbstractWriter;
15 import org.opendaylight.dsbenchmark.DomListBuilder;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class TxchainDomWrite extends DatastoreAbstractWriter implements FutureCallback<Empty> {
30 private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
32 private final DOMDataBroker dataBroker;
33 private List<MapEntryNode> list = null;
35 public TxchainDomWrite(final DOMDataBroker dataBroker, final StartTestInput.Operation oper,
36 final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
37 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
38 this.dataBroker = dataBroker;
39 LOG.debug("Created TxchainDomWrite");
43 public void createList() {
44 list = DomListBuilder.buildOuterList(outerListElem, innerListElem);
48 public void executeList() {
49 final var dsType = getDataStoreType();
50 final var pid = YangInstanceIdentifier.of(TestExec.QNAME, OuterList.QNAME);
51 final var chain = dataBroker.createMergingTransactionChain();
52 chain.addCallback(this);
54 var tx = chain.newWriteOnlyTransaction();
58 for (var element : list) {
59 var yid = pid.node(NodeIdentifierWithPredicates.of(OuterList.QNAME, element.name().asMap()));
61 if (oper == StartTestInput.Operation.PUT) {
62 tx.put(dsType, yid, element);
64 tx.merge(dsType, yid, element);
69 // Start performing the operation; submit the transaction at every n-th operation
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
97 } catch (final InterruptedException | ExecutionException e) {
98 LOG.error("Transaction failed", e);
103 } catch (final IllegalStateException e) {
104 LOG.error("Transaction close failed,", e);
107 LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
111 public void onFailure(final Throwable cause) {
112 LOG.error("Broken chain in TxchainDomWrite", cause);
116 public void onSuccess(final Empty result) {
117 LOG.debug("Chain closed successfully");