Reduce use of deprecated methods
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainDomWrite.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.List;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
23 import org.opendaylight.dsbenchmark.DomListBuilder;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class TxchainDomWrite extends DatastoreAbstractWriter implements TransactionChainListener {
36     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
37     private final DOMDataBroker domDataBroker;
38     private List<MapEntryNode> list;
39
40     public TxchainDomWrite(final DOMDataBroker domDataBroker, final StartTestInput.Operation oper, final int outerListElem,
41             final int innerListElem, final long writesPerTx, final DataStore dataStore) {
42         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
43         this.domDataBroker = domDataBroker;
44         LOG.debug("Created TxchainDomWrite");
45     }
46
47     @Override
48     public void createList() {
49         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
50     }
51
52     @Override
53     public void executeList() {
54         final LogicalDatastoreType dsType = getDataStoreType();
55         final YangInstanceIdentifier pid =
56                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
57         final DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
58
59         DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
60         int txSubmitted = 0;
61         int writeCnt = 0;
62
63         for (MapEntryNode element : this.list) {
64             YangInstanceIdentifier yid =
65                     pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
66
67             if (oper == StartTestInput.Operation.PUT) {
68                 tx.put(dsType, yid, element);
69             } else {
70                 tx.merge(dsType, yid, element);
71             }
72
73             writeCnt++;
74
75             // Start performing the operation; submit the transaction at every n-th operation
76             if (writeCnt == writesPerTx) {
77                 txSubmitted++;
78                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
79                     @Override
80                     public void onSuccess(final CommitInfo result) {
81                         txOk++;
82                     }
83
84                     @Override
85                     public void onFailure(final Throwable t) {
86                         LOG.error("Transaction failed, {}", t);
87                         txError++;
88                     }
89                 }, MoreExecutors.directExecutor());
90                 tx = chain.newWriteOnlyTransaction();
91                 writeCnt = 0;
92             }
93         }
94
95         // *** Clean up and close the transaction chain ***
96         // Submit the outstanding transaction even if it's empty and wait for it to finish
97         // We need to empty the transaction chain before closing it
98
99         try {
100             txSubmitted++;
101             tx.commit().get();
102             txOk++;
103         } catch (final InterruptedException | ExecutionException e) {
104             LOG.error("Transaction failed", e);
105             txError++;
106         }
107         try {
108             chain.close();
109         } catch (final IllegalStateException e) {
110             LOG.error("Transaction close failed,", e);
111         }
112
113         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
114     }
115
116     @Override
117     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
118             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
119         LOG.error("Broken chain {} in TxchainDomWrite, transaction {}, cause {}",
120                 chain, transaction.getIdentifier(), cause);
121     }
122
123     @Override
124     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
125         LOG.debug("Chain {} closed successfully", chain);
126     }
127
128 }

©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.