f367b8359849825130951cd7a7b7b9b5cb4de23f
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / simpletx / SimpletxDomWrite.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.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
18 import org.opendaylight.dsbenchmark.DomListBuilder;
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.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class SimpletxDomWrite extends DatastoreAbstractWriter {
30     private static final Logger LOG = LoggerFactory.getLogger(SimpletxDomWrite.class);
31     private final DOMDataBroker domDataBroker;
32     private List<MapEntryNode> list;
33
34     public SimpletxDomWrite(DOMDataBroker domDataBroker, StartTestInput.Operation oper,
35                                     int outerListElem, int innerListElem, long putsPerTx, DataStore dataStore ) {
36         super(oper, outerListElem, innerListElem, putsPerTx, dataStore);
37         this.domDataBroker = domDataBroker;
38         LOG.info("Created SimpletxDomWrite");
39     }
40
41     @Override
42     public void createList() {
43         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
44     }
45
46     @Override
47     public void executeList() {
48         DOMDataWriteTransaction tx = domDataBroker.newWriteOnlyTransaction();
49         long writeCnt = 0;
50
51         YangInstanceIdentifier pid = YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
52         for (MapEntryNode element : this.list) {
53             YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
54
55             if (oper == StartTestInput.Operation.PUT) {
56                 tx.put(LogicalDatastoreType.CONFIGURATION, yid, element);
57             } else {
58                 tx.merge(LogicalDatastoreType.CONFIGURATION, yid, element);
59             }
60
61             writeCnt++;
62
63             if (writeCnt == writesPerTx) {
64                 try {
65                     tx.submit().checkedGet();
66                     txOk++;
67                 } catch (TransactionCommitFailedException e) {
68                     LOG.error("Transaction failed", e);
69                     txError++;
70                 }
71                 tx = domDataBroker.newWriteOnlyTransaction();
72                 writeCnt = 0;
73             }
74         }
75
76         if (writeCnt != 0) {
77             try {
78                 tx.submit().checkedGet();
79             } catch (TransactionCommitFailedException e) {
80                 LOG.error("Transaction failed", e);
81             }
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.