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