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