Fix checkstyle/spotbugs violations
[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 package org.opendaylight.dsbenchmark.txchain;
9
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.BaListBuilder;
15 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.Transaction;
18 import org.opendaylight.mdsal.binding.api.TransactionChain;
19 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.StartTestInput.Operation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
33     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
34     private final DataBroker bindingDataBroker;
35     private List<OuterList> list;
36
37     public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
38             final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
39         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
40         this.bindingDataBroker = bindingDataBroker;
41         LOG.debug("Created TxchainBaWrite");
42     }
43
44     @Override
45     public void createList() {
46         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
47     }
48
49     @Override
50     public void executeList() {
51         final TransactionChain chain = bindingDataBroker.createMergingTransactionChain(this);
52         final LogicalDatastoreType dsType = getDataStoreType();
53
54         WriteTransaction tx = chain.newWriteOnlyTransaction();
55         int txSubmitted = 0;
56         int writeCnt = 0;
57
58         for (OuterList element : this.list) {
59             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
60                                                     .child(OuterList.class, element.key());
61
62             if (oper == StartTestInput.Operation.PUT) {
63                 tx.put(dsType, iid, element);
64             } else {
65                 tx.merge(dsType, iid, element);
66             }
67
68             writeCnt++;
69
70             if (writeCnt == writesPerTx) {
71                 txSubmitted++;
72                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
73                     @Override
74                     public void onSuccess(final CommitInfo result) {
75                         txOk++;
76                     }
77
78                     @Override
79                     public void onFailure(final Throwable cause) {
80                         LOG.error("Transaction failed", cause);
81                         txError++;
82                     }
83                 }, MoreExecutors.directExecutor());
84                 tx = chain.newWriteOnlyTransaction();
85                 writeCnt = 0;
86             }
87         }
88
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
92         try {
93             txSubmitted++;
94             tx.commit().get();
95             txOk++;
96         } catch (final InterruptedException | ExecutionException e) {
97             LOG.error("Transaction failed", e);
98             txError++;
99         }
100         try {
101             chain.close();
102         } catch (final IllegalStateException e) {
103             LOG.error("Transaction close failed,", e);
104         }
105         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
106     }
107
108     @Override
109     public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
110             final Throwable cause) {
111         LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}", chain, transaction.getIdentifier(),
112             cause);
113     }
114
115     @Override
116     public void onTransactionChainSuccessful(final TransactionChain chain) {
117         LOG.debug("DatastoreBaAbstractWrite closed successfully, chain {}", chain);
118     }
119 }