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.common.api.data.AsyncTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
22 import org.opendaylight.dsbenchmark.DomListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
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.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.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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 TxchainDomWrite extends DatastoreAbstractWriter implements TransactionChainListener {
37 private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
38 private final DOMDataBroker domDataBroker;
39 private List<MapEntryNode> list;
41 public TxchainDomWrite(DOMDataBroker domDataBroker, StartTestInput.Operation oper, int outerListElem,
42 int innerListElem, long writesPerTx, DataStore dataStore) {
43 super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
44 this.domDataBroker = domDataBroker;
45 LOG.info("Created TxchainDomWrite");
49 public void createList() {
50 list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
54 public void executeList() {
58 DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
59 LogicalDatastoreType dsType = getDataStoreType();
60 DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
62 YangInstanceIdentifier pid = YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
63 for (MapEntryNode element : this.list) {
64 YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
66 if (oper == StartTestInput.Operation.PUT) {
67 tx.put(dsType, yid, element);
69 tx.merge(dsType, yid, element);
74 // Start performing the operation; submit the transaction at every n-th operation
75 if (writeCnt == writesPerTx) {
77 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
79 public void onSuccess(final Void result) {
83 public void onFailure(final Throwable t) {
84 LOG.error("Transaction failed, {}", t);
88 tx = chain.newWriteOnlyTransaction();
89 dsType = getDataStoreType();
94 // *** Clean up and close the transaction chain ***
95 // Submit the outstanding transaction even if it's empty and wait for it to finish
96 // We need to empty the transaction chain before closing it
100 tx.submit().checkedGet();
102 } catch (TransactionCommitFailedException e) {
103 LOG.error("Transaction failed", e);
109 catch (IllegalStateException e){
110 LOG.error("Transaction close failed,", e);
112 LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
116 public void onTransactionChainFailed(TransactionChain<?, ?> chain,
117 AsyncTransaction<?, ?> transaction, Throwable cause) {
118 LOG.error("Broken chain {} in TxchainDomWrite, transaction {}, cause {}",
119 chain, transaction.getIdentifier(), cause);
123 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
124 LOG.info("Chain {} closed successfully", chain);