902397c3b5a9776c5e8bcbe5df80bcb86f6a18e1
[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 java.util.List;
12
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.dsbenchmark.DomListBuilder;
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.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.util.concurrent.FutureCallback;
34 import com.google.common.util.concurrent.Futures;
35
36 public class TxchainDomWrite extends DatastoreAbstractWriter implements TransactionChainListener {
37     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
38     private final DOMDataBroker domDataBroker;
39     private List<MapEntryNode> list;
40
41     public TxchainDomWrite(DOMDataBroker domDataBroker, StartTestInput.Operation oper, int outerListElem,
42             int innerListElem, long writesPerTx, DataStore dataStore) {
43         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
44         this.domDataBroker = domDataBroker;
45         LOG.info("Created TxchainDomWrite");
46     }
47
48     @Override
49     public void createList() {
50         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
51     }
52
53     @Override
54     public void executeList() {
55         int txSubmitted = 0;
56         int writeCnt = 0;
57
58         DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
59         DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
60
61         YangInstanceIdentifier pid = YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
62         for (MapEntryNode element : this.list) {
63             YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
64
65             if (oper == StartTestInput.Operation.PUT) {
66                 tx.put(LogicalDatastoreType.CONFIGURATION, yid, element);
67             } else {
68                 tx.merge(LogicalDatastoreType.CONFIGURATION, yid, element);
69             }
70
71             writeCnt++;
72
73             if (writeCnt == writesPerTx) {
74                 txSubmitted++;
75                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
76                     @Override
77                     public void onSuccess(final Void result) {
78                         txOk++;
79                     }
80                     @Override
81                     public void onFailure(final Throwable t) {
82                         LOG.error("Transaction failed, {}", t);
83                         txError++;
84                     }
85                 });
86                 tx = chain.newWriteOnlyTransaction();
87                 writeCnt = 0;
88             }
89         }
90
91         // *** Clean up and close the transaction chain ***
92         // Submit the outstanding transaction even if it's empty and wait for it to finish
93         // We need to empty the transaction chain before closing it
94         try {
95             txSubmitted++;
96             tx.submit().checkedGet();
97             txOk++;
98         } catch (TransactionCommitFailedException e) {
99             LOG.error("Transaction failed", e);
100             txError++;
101         }
102         try {
103             chain.close();
104         }
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 TxchainDomWrite, transaction {}, cause {}",
115                 chain, transaction.getIdentifier(), cause);
116     }
117
118     @Override
119     public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
120         LOG.info("Chain {} closed successfully", chain);
121     }
122
123 }

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