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