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

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