Convert dsbenchmark to mdsal APIs
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainDomRead.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.dsbenchmark.txchain;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.Optional;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
14 import org.opendaylight.mdsal.common.api.AsyncTransaction;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.TransactionChain;
17 import org.opendaylight.mdsal.common.api.TransactionChainListener;
18 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class TxchainDomRead extends DatastoreAbstractWriter implements TransactionChainListener {
32     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomRead.class);
33     private final DOMDataBroker domDataBroker;
34
35     public TxchainDomRead(final DOMDataBroker domDataBroker, final int outerListElem, final int innerListElem,
36             final long writesPerTx, final DataStore dataStore) {
37         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
38         this.domDataBroker = domDataBroker;
39         LOG.debug("Created TxchainDomDelete");
40     }
41
42     @Override
43     public void createList() {
44         LOG.debug("TxchainDomDelete: creating data in the data store");
45
46         // Dump the whole list into the data store in a single transaction
47         // with <outerListElem> PUTs on the transaction
48         TxchainDomWrite dd = new TxchainDomWrite(domDataBroker,
49                                                  StartTestInput.Operation.PUT,
50                                                      outerListElem,
51                                                      innerListElem,
52                                                      outerListElem,
53                                                      dataStore);
54         dd.createList();
55         dd.executeList();
56     }
57
58     @Override
59     public void executeList() {
60         final LogicalDatastoreType dsType = getDataStoreType();
61         final org.opendaylight.yangtools.yang.common.QName olId = QName.create(OuterList.QNAME, "id");
62         final YangInstanceIdentifier pid =
63                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
64
65         try (DOMDataTreeReadTransaction tx = domDataBroker.newReadOnlyTransaction()) {
66             for (int l = 0; l < outerListElem; l++) {
67                 YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, olId, l));
68                 Optional<NormalizedNode<?,?>> optionalDataObject;
69                 FluentFuture<Optional<NormalizedNode<?, ?>>> submitFuture = tx.read(dsType, yid);
70                 try {
71                     optionalDataObject = submitFuture.get();
72                     if (optionalDataObject != null && optionalDataObject.isPresent()) {
73                         txOk++;
74                     }
75                 } catch (final InterruptedException | ExecutionException e) {
76                     LOG.warn("failed to ....", e);
77                     txError++;
78                 }
79             }
80         }
81     }
82
83     @Override
84     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
85                                          final AsyncTransaction<?, ?> transaction, final Throwable cause) {
86         LOG.error("Broken chain {} in TxchainDomDelete, transaction {}, cause {}",
87                 chain, transaction.getIdentifier(), cause);
88     }
89
90     @Override
91     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
92         LOG.debug("TxchainDomDelete closed successfully, chain {}", chain);
93     }
94 }

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