Added the data store benchmark (dsbenchmark, Bug 4519, https://bugs.opendaylight...
[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
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.dsbenchmark.BaListBuilder;
18 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
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(DataBroker dataBroker, StartTestInput.Operation oper,
32             int outerListElem, int innerListElem, long writesPerTx) {
33         super(oper, outerListElem, innerListElem, writesPerTx);
34         this.dataBroker = dataBroker;
35         LOG.info("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         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
46         long writeCnt = 0;
47
48         for (OuterList element : this.list) {
49             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
50                                                     .child(OuterList.class, element.getKey());
51             if (oper == StartTestInput.Operation.PUT) {
52                 tx.put(LogicalDatastoreType.CONFIGURATION, iid, element);
53             } else {
54                 tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element);
55             }
56
57             writeCnt++;
58
59             if (writeCnt == writesPerTx) {
60                 try {
61                     tx.submit().checkedGet();
62                     txOk++;
63                 } catch (TransactionCommitFailedException e) {
64                     LOG.error("Transaction failed: {}", e.toString());
65                     txError++;
66                 }
67                 tx = dataBroker.newWriteOnlyTransaction();
68                 writeCnt = 0;
69             }
70         }
71
72         if (writeCnt != 0) {
73             try {
74                 tx.submit().checkedGet();
75             } catch (TransactionCommitFailedException e) {
76                 LOG.error("Transaction failed: {}", e.toString());
77             }
78         }
79     }
80
81 }