962c95037ff09f1d5be741220dab30c474721eac
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainBaWrite.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
14 import java.util.List;
15
16 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.dsbenchmark.BaListBuilder;
25 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
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.StartTestInput.Operation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
36     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
37     private final DataBroker bindingDataBroker;
38     private List<OuterList> list;
39
40     public TxchainBaWrite(DataBroker bindingDataBroker, Operation oper,
41                           int outerListElem, int innerListElem, long writesPerTx, DataStore dataStore) {
42         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
43         this.bindingDataBroker = bindingDataBroker;
44         LOG.info("Created TxchainBaWrite");
45     }
46
47     @Override
48     public void createList() {
49         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
50     }
51
52     @Override
53     public void executeList() {
54         int txSubmitted = 0;
55         int writeCnt = 0;
56
57         BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
58         WriteTransaction tx = chain.newWriteOnlyTransaction();
59         LogicalDatastoreType dsType = getDataStoreType();
60
61         for (OuterList element : this.list) {
62             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
63                                                     .child(OuterList.class, element.getKey());
64
65             if (oper == StartTestInput.Operation.PUT) {
66                 tx.put(dsType, iid, element);
67             } else {
68                 tx.merge(dsType, iid, 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
81                     @Override
82                     public void onFailure(final Throwable t) {
83                         LOG.error("Transaction failed, {}", t);
84                         txError++;
85                     }
86                 });
87                 tx = chain.newWriteOnlyTransaction();
88                 dsType = getDataStoreType();
89                 writeCnt = 0;
90             }
91         }
92
93         // *** Clean up and close the transaction chain ***
94         // Submit the outstanding transaction even if it's empty and wait for it to finish
95         // We need to empty the transaction chain before closing it
96         try {
97             txSubmitted++;
98             tx.submit().checkedGet();
99             txOk++;
100         } catch (TransactionCommitFailedException e) {
101             LOG.error("Transaction failed", e);
102             txError++;
103         }
104         try {
105             chain.close();
106         } catch (IllegalStateException e) {
107             LOG.error("Transaction close failed,", e);
108         }
109         LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
110     }
111
112     @Override
113     public void onTransactionChainFailed(TransactionChain<?, ?> chain,
114             AsyncTransaction<?, ?> transaction, Throwable cause) {
115         LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
116                 chain, transaction.getIdentifier(), cause);
117     }
118
119     @Override
120     public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
121         LOG.info("DatastoreBaAbstractWrite closed successfully, chain {}", chain);
122     }
123
124 }

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