a8363df679bfdbb77c60f03c52ad6da9a063d106
[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 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterListKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class TxchainBaDelete extends DatastoreAbstractWriter implements TransactionChainListener {
32     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaDelete.class);
33     private final DataBroker bindingDataBroker;
34
35     public TxchainBaDelete(final DataBroker bindingDataBroker, final int outerListElem, final int innerListElem,
36             final long writesPerTx, final DataStore dataStore) {
37         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
38         this.bindingDataBroker = bindingDataBroker;
39         LOG.debug("Created TxchainBaDelete");
40     }
41
42     @Override
43     public void createList() {
44         LOG.debug("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         final LogicalDatastoreType dsType = getDataStoreType();
61         final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
62
63         WriteTransaction tx = chain.newWriteOnlyTransaction();
64         int txSubmitted = 0;
65         int writeCnt = 0;
66
67         for (int l = 0; l < outerListElem; l++) {
68             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
69                                                     .child(OuterList.class, new OuterListKey(l));
70             tx.delete(dsType, 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 (final TransactionCommitFailedException e) {
101             LOG.error("Transaction failed", e);
102         }
103         try {
104             chain.close();
105         } catch (final IllegalStateException e) {
106             LOG.error("Transaction close failed,", e);
107         }
108         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
109     }
110
111     @Override
112     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
113             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
114         LOG.error("Broken chain {} in TxchainBaDelete, transaction {}, cause {}",
115                 chain, transaction.getIdentifier(), cause);
116     }
117
118     @Override
119     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
120         LOG.debug("TxchainBaDelete closed successfully, chain {}", chain);
121     }
122
123 }