9a3f27eabfde002a89f27234c840492cd39fa89c
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / simpletx / SimpletxBaWrite.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.simpletx;
10
11 import java.util.List;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.dsbenchmark.BaListBuilder;
17 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class SimpletxBaWrite extends DatastoreAbstractWriter {
27     private static final Logger LOG = LoggerFactory.getLogger(SimpletxBaWrite.class);
28     private final DataBroker dataBroker;
29     private List<OuterList> list;
30
31     public SimpletxBaWrite(final DataBroker dataBroker, final StartTestInput.Operation oper,
32             final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
33         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
34         this.dataBroker = dataBroker;
35         LOG.debug("Created SimpletxBaWrite");
36     }
37
38     @Override
39     public void createList() {
40         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
41     }
42
43     @Override
44     public void executeList() {
45         final LogicalDatastoreType dsType = getDataStoreType();
46
47         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
48         long writeCnt = 0;
49
50         for (OuterList element : this.list) {
51             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
52                                                     .child(OuterList.class, element.getKey());
53             if (oper == StartTestInput.Operation.PUT) {
54                 tx.put(dsType, iid, element);
55             } else {
56                 tx.merge(dsType, iid, element);
57             }
58
59             writeCnt++;
60
61             if (writeCnt == writesPerTx) {
62                 try {
63                     tx.submit().checkedGet();
64                     txOk++;
65                 } catch (final TransactionCommitFailedException e) {
66                     LOG.error("Transaction failed: {}", e);
67                     txError++;
68                 }
69                 tx = dataBroker.newWriteOnlyTransaction();
70
71                 writeCnt = 0;
72             }
73         }
74
75         if (writeCnt != 0) {
76             try {
77                 tx.submit().checkedGet();
78             } catch (final TransactionCommitFailedException e) {
79                 LOG.error("Transaction failed: {}", e);
80             }
81         }
82     }
83
84 }