2465548c74be1633fe8d091f2786814b3bc55505
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainDomDelete.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.common.api.data.AsyncTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
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.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class TxchainDomDelete extends DatastoreAbstractWriter implements TransactionChainListener {
33     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
34     private final DOMDataBroker domDataBroker;
35
36     public TxchainDomDelete(final DOMDataBroker domDataBroker, final int outerListElem, final int innerListElem,
37             final long writesPerTx, final DataStore dataStore) {
38         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
39         this.domDataBroker = domDataBroker;
40         LOG.info("Created TxchainDomDelete");
41     }
42
43     @Override
44     public void createList() {
45         LOG.info("TxchainDomDelete: 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         TxchainDomWrite dd = new TxchainDomWrite(domDataBroker,
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 org.opendaylight.yangtools.yang.common.QName olId = QName.create(OuterList.QNAME, "id");
63         final YangInstanceIdentifier pid =
64                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
65         final DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
66
67         DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
68         int txSubmitted = 0;
69         int writeCnt = 0;
70
71         for (int l = 0; l < outerListElem; l++) {
72             YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, olId, l));
73             tx.delete(dsType, yid);
74
75             writeCnt++;
76
77             if (writeCnt == writesPerTx) {
78                 txSubmitted++;
79                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
80                     @Override
81                     public void onSuccess(final Void result) {
82                         txOk++;
83                     }
84
85                     @Override
86                     public void onFailure(final Throwable t) {
87                         LOG.error("Transaction failed, {}", t);
88                         txError++;
89                     }
90                 });
91                 tx = chain.newWriteOnlyTransaction();
92                 writeCnt = 0;
93             }
94         }
95
96         // *** Clean up and close the transaction chain ***
97         // Submit the outstanding transaction even if it's empty and wait for it to finish
98         // We need to empty the transaction chain before closing it
99         try {
100             txSubmitted++;
101             tx.submit().checkedGet();
102             txOk++;
103         } catch (TransactionCommitFailedException e) {
104             LOG.error("Transaction failed", e);
105             txError++;
106         }
107         try {
108             chain.close();
109         } catch (IllegalStateException e) {
110             LOG.error("Transaction close failed,", e);
111         }
112         LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
113     }
114
115     @Override
116     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
117             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
118         LOG.error("Broken chain {} in TxchainDomDelete, transaction {}, cause {}",
119                 chain, transaction.getIdentifier(), cause);
120     }
121
122     @Override
123     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
124         LOG.info("TxchainDomDelete closed successfully, chain {}", chain);
125     }
126 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.