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