Convert dsbenchmark to mdsal APIs
[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 java.util.concurrent.ExecutionException;
13 import org.opendaylight.dsbenchmark.BaListBuilder;
14 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.key());
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.commit().get();
64                     txOk++;
65                 } catch (final InterruptedException | ExecutionException 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.commit().get();
78             } catch (final InterruptedException | ExecutionException e) {
79                 LOG.error("Transaction failed: {}", e);
80             }
81         }
82     }
83
84 }

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