5357311e40a8704906f3aa33c10ef9a9458dab64
[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         long writeCnt = 0;
48
49         for (OuterList element : this.list) {
50             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
51                                                     .child(OuterList.class, element.getKey());
52             if (oper == StartTestInput.Operation.PUT) {
53                 tx.put(LogicalDatastoreType.CONFIGURATION, iid, element);
54             } else {
55                 tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element);
56             }
57
58             writeCnt++;
59
60             if (writeCnt == writesPerTx) {
61                 try {
62                     tx.submit().checkedGet();
63                     txOk++;
64                 } catch (TransactionCommitFailedException e) {
65                     LOG.error("Transaction failed: {}", e.toString());
66                     txError++;
67                 }
68                 tx = dataBroker.newWriteOnlyTransaction();
69                 writeCnt = 0;
70             }
71         }
72
73         if (writeCnt != 0) {
74             try {
75                 tx.submit().checkedGet();
76             } catch (TransactionCommitFailedException e) {
77                 LOG.error("Transaction failed: {}", e.toString());
78             }
79         }
80     }
81
82 }

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