b36404d52c848e5f51321bc7c6651d994c39882c
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainBaWrite.java
1 /*
2  * Copyright (c) 2015 Cisco Systems and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.dsbenchmark.txchain;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.dsbenchmark.BaListBuilder;
24 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
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;
38
39     public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
40                           final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
41         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
42         this.bindingDataBroker = bindingDataBroker;
43         LOG.debug("Created TxchainBaWrite");
44     }
45
46     @Override
47     public void createList() {
48         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
49     }
50
51     @Override
52     public void executeList() {
53         final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
54         final LogicalDatastoreType dsType = getDataStoreType();
55
56         WriteTransaction tx = chain.newWriteOnlyTransaction();
57         int txSubmitted = 0;
58         int writeCnt = 0;
59
60         for (OuterList element : this.list) {
61             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
62                                                     .child(OuterList.class, element.key());
63
64             if (oper == StartTestInput.Operation.PUT) {
65                 tx.put(dsType, iid, element);
66             } else {
67                 tx.merge(dsType, iid, element);
68             }
69
70             writeCnt++;
71
72             if (writeCnt == writesPerTx) {
73                 txSubmitted++;
74                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
75                     @Override
76                     public void onSuccess(final Void result) {
77                         txOk++;
78                     }
79
80                     @Override
81                     public void onFailure(final Throwable t) {
82                         LOG.error("Transaction failed, {}", t);
83                         txError++;
84                     }
85                 }, MoreExecutors.directExecutor());
86                 tx = chain.newWriteOnlyTransaction();
87                 writeCnt = 0;
88             }
89         }
90
91         // *** Clean up and close the transaction chain ***
92         // Submit the outstanding transaction even if it's empty and wait for it to finish
93         // We need to empty the transaction chain before closing it
94         try {
95             txSubmitted++;
96             tx.submit().checkedGet();
97             txOk++;
98         } catch (final TransactionCommitFailedException e) {
99             LOG.error("Transaction failed", e);
100             txError++;
101         }
102         try {
103             chain.close();
104         } catch (final IllegalStateException e) {
105             LOG.error("Transaction close failed,", e);
106         }
107         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
108     }
109
110     @Override
111     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
112             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
113         LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
114                 chain, transaction.getIdentifier(), cause);
115     }
116
117     @Override
118     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
119         LOG.debug("DatastoreBaAbstractWrite closed successfully, chain {}", chain);
120     }
121
122 }