Adjust for Binding RPC codegen changes
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / DsbenchmarkProvider.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 package org.opendaylight.dsbenchmark;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.concurrent.atomic.AtomicReference;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
20 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
21 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
22 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
23 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
24 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
25 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
26 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
27 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
28 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
29 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
30 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
31 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.DsbenchmarkService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExecBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
52
53     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
54     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
55             InstanceIdentifier.builder(TestExec.class).build();
56     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
57             InstanceIdentifier.builder(TestStatus.class).build();
58
59     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
60     private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
61     private final DOMDataBroker domDataBroker;  // Async DOM Broker for use with all DOM operations
62     private final DataBroker txChainDataBroker; // Async Binding-Aware Broker for use in tx chains; initialized to
63                                                 // ping-pong broker in default config (see default-config.xml and
64                                                 // dsbenchmark-impl.yang)
65     private final DataBroker simpleTxDataBroker;      // "Legacy" OSGI Data Broker for use in simple transactions
66
67     private long testsCompleted = 0;
68
69     public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker txChainDataBroker,
70             final DataBroker simpleTxDataBroker) {
71         this.domDataBroker = domDataBroker;
72         this.txChainDataBroker = txChainDataBroker;
73         this.simpleTxDataBroker = simpleTxDataBroker;
74     }
75
76     public void init() {
77         listenerProvider.setDataBroker(simpleTxDataBroker);
78
79         try {
80             // We want to set the initial operation status so users can detect we are ready to start test.
81             setTestOperData(this.execStatus.get(), testsCompleted);
82         } catch (final Exception e) {
83             // TODO: Use a singleton service to make sure the initial write is performed only once.
84             LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData: {}", e);
85         }
86
87         LOG.info("DsbenchmarkProvider initiated");
88     }
89
90     @Override
91     public void close() {
92         LOG.info("DsbenchmarkProvider closed");
93     }
94
95     @Override
96     public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
97         cleanupTestStore();
98         LOG.debug("Data Store cleaned up");
99         return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
100     }
101
102     @Override
103     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
104         LOG.info("Starting the data store benchmark test, input: {}", input);
105
106         // Check if there is a test in progress
107         if (execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false) {
108             LOG.info("Test in progress");
109             return RpcResultBuilder.success(new StartTestOutputBuilder()
110                     .setStatus(StartTestOutput.Status.TESTINPROGRESS)
111                     .build()).buildFuture();
112         }
113
114         // Cleanup data that may be left over from a previous test run
115         cleanupTestStore();
116
117         // Get the appropriate writer based on operation type and data format
118         DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
119
120         // Create listeners on OPERATIONAL and CONFIG test data subtrees
121         listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
122
123         long startTime, endTime, listCreateTime, execTime;
124
125         startTime = System.nanoTime();
126         dsWriter.createList();
127         endTime = System.nanoTime();
128         listCreateTime = (endTime - startTime) / 1000;
129
130         // Run the test and measure the execution time
131         try {
132             startTime = System.nanoTime();
133             dsWriter.executeList();
134             endTime = System.nanoTime();
135             execTime = (endTime - startTime) / 1000;
136
137             this.testsCompleted++;
138
139         } catch (final Exception e) {
140             LOG.error("Test error: {}", e.toString());
141             execStatus.set(ExecStatus.Idle);
142             return RpcResultBuilder.success(new StartTestOutputBuilder()
143                     .setStatus(StartTestOutput.Status.FAILED)
144                     .build()).buildFuture();
145         }
146
147         LOG.info("Test finished");
148         setTestOperData(ExecStatus.Idle, testsCompleted);
149         execStatus.set(ExecStatus.Idle);
150
151         // Get the number of data change events and cleanup the data change listeners
152         long numDataChanges = listenerProvider.getDataChangeCount();
153         long numEvents = listenerProvider.getEventCountAndDestroyListeners();
154
155         StartTestOutput output = new StartTestOutputBuilder()
156                 .setStatus(StartTestOutput.Status.OK)
157                 .setListBuildTime(listCreateTime)
158                 .setExecTime(execTime)
159                 .setTxOk((long)dsWriter.getTxOk())
160                 .setNtfOk(numEvents)
161                 .setDataChangeEventsOk(numDataChanges)
162                 .setTxError((long)dsWriter.getTxError())
163                 .build();
164
165         return RpcResultBuilder.success(output).buildFuture();
166     }
167
168     private void setTestOperData(final ExecStatus sts, final long tstCompl) {
169         TestStatus status = new TestStatusBuilder()
170                 .setExecStatus(sts)
171                 .setTestsCompleted(tstCompl)
172                 .build();
173
174         WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
175         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
176
177         try {
178             tx.submit().checkedGet();
179         } catch (final TransactionCommitFailedException e) {
180             throw new IllegalStateException(e);
181         }
182
183         LOG.debug("DataStore test oper status populated: {}", status);
184     }
185
186     private void cleanupTestStore() {
187         TestExec data = new TestExecBuilder()
188                 .setOuterList(Collections.<OuterList>emptyList())
189                 .build();
190
191         WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
192         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
193         try {
194             tx.submit().checkedGet();
195             LOG.debug("DataStore config test data cleaned up");
196         } catch (final TransactionCommitFailedException e) {
197             LOG.info("Failed to cleanup DataStore configtest data");
198             throw new IllegalStateException(e);
199         }
200
201         tx = simpleTxDataBroker.newWriteOnlyTransaction();
202         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
203         try {
204             tx.submit().checkedGet();
205             LOG.debug("DataStore operational test data cleaned up");
206         } catch (final TransactionCommitFailedException e) {
207             LOG.info("Failed to cleanup DataStore operational test data");
208             throw new IllegalStateException(e);
209         }
210
211     }
212
213     private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
214
215         final DatastoreAbstractWriter retVal;
216
217         StartTestInput.TransactionType txType = input.getTransactionType();
218         StartTestInput.Operation oper = input.getOperation();
219         StartTestInput.DataFormat dataFormat = input.getDataFormat();
220         StartTestInput.DataStore dataStore = input.getDataStore();
221         int outerListElem = input.getOuterElements().intValue();
222         int innerListElem = input.getInnerElements().intValue();
223         int writesPerTx = input.getPutsPerTx().intValue();
224
225         try {
226             if (txType == StartTestInput.TransactionType.SIMPLETX) {
227                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
228                     if (StartTestInput.Operation.DELETE == oper) {
229                         retVal = new SimpletxBaDelete(this.simpleTxDataBroker, outerListElem,
230                                 innerListElem,writesPerTx, dataStore);
231                     } else if (StartTestInput.Operation.READ == oper) {
232                         retVal = new SimpletxBaRead(this.simpleTxDataBroker, outerListElem,
233                                 innerListElem, writesPerTx, dataStore);
234                     } else {
235                         retVal = new SimpletxBaWrite(this.simpleTxDataBroker, oper, outerListElem,
236                                 innerListElem, writesPerTx, dataStore);
237                     }
238                 } else {
239                     if (StartTestInput.Operation.DELETE == oper) {
240                         retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
241                                 innerListElem, writesPerTx, dataStore);
242                     } else if (StartTestInput.Operation.READ == oper) {
243                         retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
244                                 innerListElem, writesPerTx, dataStore);
245                     } else {
246                         retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
247                                 innerListElem, writesPerTx, dataStore);
248                     }
249                 }
250             } else {
251                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
252                     if (StartTestInput.Operation.DELETE == oper) {
253                         retVal = new TxchainBaDelete(this.txChainDataBroker, outerListElem,
254                                 innerListElem, writesPerTx, dataStore);
255                     } else if (StartTestInput.Operation.READ == oper) {
256                         retVal = new TxchainBaRead(this.txChainDataBroker,outerListElem,
257                                 innerListElem,writesPerTx, dataStore);
258                     } else {
259                         retVal = new TxchainBaWrite(this.txChainDataBroker, oper, outerListElem,
260                                 innerListElem, writesPerTx, dataStore);
261                     }
262                 } else {
263                     if (StartTestInput.Operation.DELETE == oper) {
264                         retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
265                                 innerListElem, writesPerTx, dataStore);
266                     } else if (StartTestInput.Operation.READ == oper) {
267                         retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
268                                 innerListElem, writesPerTx, dataStore);
269
270                     } else {
271                         retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
272                                 innerListElem,writesPerTx, dataStore);
273                     }
274                 }
275             }
276         } finally {
277             execStatus.set(ExecStatus.Idle);
278         }
279         return retVal;
280     }
281 }

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