Convert dsbenchmark to mdsal APIs
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainBaRead.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.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.common.api.AsyncTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.common.api.TransactionChain;
19 import org.opendaylight.mdsal.common.api.TransactionChainListener;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterListKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.outer.list.InnerList;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class TxchainBaRead extends DatastoreAbstractWriter implements TransactionChainListener {
31     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaRead.class);
32     private final DataBroker bindingDataBroker;
33
34     public TxchainBaRead(final DataBroker bindingDataBroker, final int outerListElem, final int innerListElem,
35             final long writesPerTx, final DataStore dataStore) {
36         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
37         this.bindingDataBroker = bindingDataBroker;
38         LOG.debug("Created TxchainBaRead");
39     }
40
41     @Override
42     public void createList() {
43         LOG.debug("TxchainBaRead: reading data in the data store");
44
45         // Dump the whole list into the data store in a single transaction
46         // with <outerListElem> PUTs on the transaction
47         TxchainBaWrite dd = new TxchainBaWrite(bindingDataBroker,
48                                                StartTestInput.Operation.PUT,
49                                                outerListElem,
50                                                innerListElem,
51                                                outerListElem,
52                                                dataStore);
53         dd.createList();
54         dd.executeList();
55     }
56
57     @Override
58     public void executeList() {
59         final LogicalDatastoreType dsType = getDataStoreType();
60
61         try (ReadTransaction tx = bindingDataBroker.newReadOnlyTransaction()) {
62             for (long l = 0; l < outerListElem; l++) {
63
64                 InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
65                         .child(OuterList.class, new OuterListKey((int) l));
66                 FluentFuture<Optional<OuterList>> submitFuture = tx.read(dsType, iid);
67
68                 try {
69                     Optional<OuterList> optionalDataObject = submitFuture.get();
70                     if (optionalDataObject != null && optionalDataObject.isPresent()) {
71                         OuterList outerList = optionalDataObject.get();
72
73                         String[] objectsArray = new String[outerList.getInnerList().size()];
74                         for (InnerList innerList : outerList.getInnerList()) {
75                             if (objectsArray[innerList.getName()] != null) {
76                                 LOG.error("innerList: DUPLICATE name: {}, value: {}", innerList.getName(),
77                                     innerList.getValue());
78                             }
79                             objectsArray[innerList.getName()] = innerList.getValue();
80                         }
81                         for (int i = 0; i < outerList.getInnerList().size(); i++) {
82                             String itemStr = objectsArray[i];
83                             if (!itemStr.contentEquals("Item-" + String.valueOf(l) + "-" + String.valueOf(i))) {
84                                 LOG.error("innerList: name: {}, value: {}", i, itemStr);
85                                 break;
86                             }
87                         }
88                         txOk++;
89                     } else {
90                         txError++;
91                     }
92                 } catch (final InterruptedException | ExecutionException e) {
93                     LOG.warn("failed to ....", e);
94                     txError++;
95                 }
96             }
97         }
98     }
99
100     @Override
101     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
102                                          final AsyncTransaction<?, ?> transaction, final Throwable cause) {
103         LOG.error("Broken chain {} in TxchainBaDelete, transaction {}, cause {}",
104                 chain, transaction.getIdentifier(), cause);
105     }
106
107     @Override
108     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
109         LOG.debug("TxchainBaDelete closed successfully, chain {}", chain);
110     }
111
112 }

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