Final bits of NodeIdentifier migration
[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 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
14 import org.opendaylight.dsbenchmark.DomListBuilder;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
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.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class SimpletxDomWrite extends DatastoreAbstractWriter {
29     private static final Logger LOG = LoggerFactory.getLogger(SimpletxDomWrite.class);
30     private final DOMDataBroker domDataBroker;
31     private List<MapEntryNode> list;
32
33     public SimpletxDomWrite(final DOMDataBroker domDataBroker, final StartTestInput.Operation oper,
34             final int outerListElem, final int innerListElem, final long putsPerTx, final DataStore dataStore) {
35         super(oper, outerListElem, innerListElem, putsPerTx, dataStore);
36         this.domDataBroker = domDataBroker;
37         LOG.debug("Created SimpletxDomWrite");
38     }
39
40     @Override
41     public void createList() {
42         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
43     }
44
45     @Override
46     public void executeList() {
47         final LogicalDatastoreType dsType = getDataStoreType();
48         final YangInstanceIdentifier pid =
49                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
50
51         DOMDataTreeWriteTransaction tx = domDataBroker.newWriteOnlyTransaction();
52         long writeCnt = 0;
53
54         for (MapEntryNode element : this.list) {
55             YangInstanceIdentifier yid =
56                     pid.node(NodeIdentifierWithPredicates.of(OuterList.QNAME, element.getIdentifier().asMap()));
57
58             if (oper == StartTestInput.Operation.PUT) {
59                 tx.put(dsType, yid, element);
60             } else {
61                 tx.merge(dsType, yid, element);
62             }
63
64             writeCnt++;
65
66             if (writeCnt == writesPerTx) {
67                 try {
68                     tx.commit().get();
69                     txOk++;
70                 } catch (final InterruptedException | ExecutionException e) {
71                     LOG.error("Transaction failed", e);
72                     txError++;
73                 }
74                 tx = domDataBroker.newWriteOnlyTransaction();
75                 writeCnt = 0;
76             }
77         }
78
79         if (writeCnt != 0) {
80             try {
81                 tx.commit().get();
82             } catch (final InterruptedException | ExecutionException e) {
83                 LOG.error("Transaction failed", e);
84             }
85         }
86     }
87 }

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