44fcc9744f481c51410a513ca398d8884056900f
[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.MoreExecutors;
13 import java.util.concurrent.ExecutionException;
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.dsbenchmark.DatastoreAbstractWriter;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
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 final DataBroker bindingDataBroker;
35
36     public TxchainBaDelete(final DataBroker bindingDataBroker, final int outerListElem, final int innerListElem,
37             final long writesPerTx, final DataStore dataStore) {
38         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
39         this.bindingDataBroker = bindingDataBroker;
40         LOG.debug("Created TxchainBaDelete");
41     }
42
43     @Override
44     public void createList() {
45         LOG.debug("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         final LogicalDatastoreType dsType = getDataStoreType();
62         final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
63
64         WriteTransaction tx = chain.newWriteOnlyTransaction();
65         int txSubmitted = 0;
66         int writeCnt = 0;
67
68         for (int l = 0; l < outerListElem; l++) {
69             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
70                                                     .child(OuterList.class, new OuterListKey(l));
71             tx.delete(dsType, iid);
72
73             writeCnt++;
74
75             if (writeCnt == writesPerTx) {
76                 txSubmitted++;
77                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
78                     @Override
79                     public void onSuccess(final CommitInfo result) {
80                         txOk++;
81                     }
82
83                     @Override
84                     public void onFailure(final Throwable t) {
85                         LOG.error("Transaction failed, {}", t);
86                         txError++;
87                     }
88                 }, MoreExecutors.directExecutor());
89                 tx = chain.newWriteOnlyTransaction();
90                 writeCnt = 0;
91             }
92         }
93
94         // Submit the outstanding transaction even if it's empty and wait for it to finish
95         // We need to empty the chain before closing it
96         try {
97             if (writeCnt > 0) {
98                 txSubmitted++;
99             }
100             tx.commit().get();
101         } catch (final InterruptedException | ExecutionException e) {
102             LOG.error("Transaction failed", e);
103         }
104         try {
105             chain.close();
106         } catch (final IllegalStateException e) {
107             LOG.error("Transaction close failed,", e);
108         }
109         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
110     }
111
112     @Override
113     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
114             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
115         LOG.error("Broken chain {} in TxchainBaDelete, transaction {}, cause {}",
116                 chain, transaction.getIdentifier(), cause);
117     }
118
119     @Override
120     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
121         LOG.debug("TxchainBaDelete closed successfully, chain {}", chain);
122     }
123
124 }