2 * Copyright (c) 2015 Cisco Systems and others. All rights reserved.
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
8 package org.opendaylight.dsbenchmark;
10 import java.util.Collections;
11 import java.util.concurrent.Future;
12 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.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
23 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
24 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
25 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
26 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
27 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
28 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
29 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
30 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
31 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
32 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
33 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.DsbenchmarkService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExecBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 import com.google.common.util.concurrent.Futures;
52 public class DsbenchmarkProvider implements BindingAwareProvider, DsbenchmarkService, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
55 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<ExecStatus>( ExecStatus.Idle );
57 private static final InstanceIdentifier<TestExec> TEST_EXEC_IID = InstanceIdentifier.builder(TestExec.class).build();
58 private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID = InstanceIdentifier.builder(TestStatus.class).build();
59 private final DOMDataBroker domDataBroker;
60 private final DataBroker bindingDataBroker;
61 private RpcRegistration<DsbenchmarkService> dstReg;
62 private DataBroker dataBroker;
64 private long testsCompleted = 0;
66 public DsbenchmarkProvider(DOMDataBroker domDataBroker, DataBroker bindingDataBroker) {
67 // We have to get the DOMDataBroker via the constructor,
68 // since we can't get it from the session
69 this.domDataBroker = domDataBroker;
70 this.bindingDataBroker = bindingDataBroker;
74 public void onSessionInitiated(ProviderContext session) {
75 this.dataBroker = session.getSALService(DataBroker.class);
76 this.dstReg = session.addRpcImplementation( DsbenchmarkService.class, this );
77 setTestOperData(this.execStatus.get(), testsCompleted);
79 LOG.info("DsbenchmarkProvider Session Initiated");
83 public void close() throws Exception {
85 LOG.info("DsbenchmarkProvider Closed");
89 public Future<RpcResult<Void>> cleanupStore() {
91 LOG.info("Data Store cleaned up");
92 return Futures.immediateFuture( RpcResultBuilder.<Void> success().build() );
96 public Future<RpcResult<StartTestOutput>> startTest(StartTestInput input) {
97 LOG.info("Starting the data store benchmark test, input: {}", input);
99 // Check if there is a test in progress
100 if ( execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false ) {
101 LOG.info("Test in progress");
102 return RpcResultBuilder.success(new StartTestOutputBuilder()
103 .setStatus(StartTestOutput.Status.TESTINPROGRESS)
104 .build()).buildFuture();
107 // Cleanup data that may be left over from a previous test run
110 // Get the appropriate writer based on operation type and data format
111 DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
113 long startTime, endTime, listCreateTime, execTime;
115 startTime = System.nanoTime();
116 dsWriter.createList();
117 endTime = System.nanoTime();
118 listCreateTime = (endTime - startTime) / 1000;
120 // Run the test and measure the execution time
122 startTime = System.nanoTime();
123 dsWriter.executeList();
124 endTime = System.nanoTime();
125 execTime = (endTime - startTime) / 1000;
127 this.testsCompleted++;
129 } catch ( Exception e ) {
130 LOG.error( "Test error: {}", e.toString());
131 execStatus.set( ExecStatus.Idle );
132 return RpcResultBuilder.success(new StartTestOutputBuilder()
133 .setStatus(StartTestOutput.Status.FAILED)
134 .build()).buildFuture();
137 LOG.info("Test finished");
138 setTestOperData( ExecStatus.Idle, testsCompleted);
139 execStatus.set(ExecStatus.Idle);
141 StartTestOutput output = new StartTestOutputBuilder()
142 .setStatus(StartTestOutput.Status.OK)
143 .setListBuildTime(listCreateTime)
144 .setExecTime(execTime)
145 .setTxOk((long)dsWriter.getTxOk())
146 .setTxError((long)dsWriter.getTxError())
149 return RpcResultBuilder.success(output).buildFuture();
152 private void setTestOperData( ExecStatus sts, long tstCompl ) {
153 TestStatus status = new TestStatusBuilder()
155 .setTestsCompleted(tstCompl)
158 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
159 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
162 tx.submit().checkedGet();
163 } catch (TransactionCommitFailedException e) {
164 throw new IllegalStateException(e);
167 LOG.info("DataStore test oper status populated: {}", status);
170 private void cleanupTestStore() {
171 TestExec data = new TestExecBuilder()
172 .setOuterList(Collections.<OuterList>emptyList())
175 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
176 tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
178 tx.submit().checkedGet();
179 LOG.info("DataStore config test data cleaned up");
180 } catch (TransactionCommitFailedException e) {
181 LOG.info("Failed to cleanup DataStore configtest data");
182 throw new IllegalStateException(e);
185 tx = dataBroker.newWriteOnlyTransaction();
186 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
188 tx.submit().checkedGet();
189 LOG.info("DataStore operational test data cleaned up");
190 } catch (TransactionCommitFailedException e) {
191 LOG.info("Failed to cleanup DataStore operational test data");
192 throw new IllegalStateException(e);
197 private DatastoreAbstractWriter getDatastoreWriter(StartTestInput input) {
199 final DatastoreAbstractWriter retVal;
201 StartTestInput.TransactionType txType = input.getTransactionType();
202 StartTestInput.Operation oper = input.getOperation();
203 StartTestInput.DataFormat dataFormat = input.getDataFormat();
204 StartTestInput.DataStore dataStore = input.getDataStore();
205 int outerListElem = input.getOuterElements().intValue();
206 int innerListElem = input.getInnerElements().intValue();
207 int writesPerTx = input.getPutsPerTx().intValue();
210 if (txType == StartTestInput.TransactionType.SIMPLETX) {
211 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
212 if (StartTestInput.Operation.DELETE == oper) {
213 retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
214 innerListElem,writesPerTx, dataStore);
215 } else if (StartTestInput.Operation.READ == oper) {
216 retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
217 innerListElem, writesPerTx, dataStore);
219 retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
220 innerListElem, writesPerTx, dataStore);
223 if (StartTestInput.Operation.DELETE == oper) {
224 retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
225 innerListElem, writesPerTx, dataStore);
226 } else if (StartTestInput.Operation.READ == oper) {
227 retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
228 innerListElem, writesPerTx, dataStore);
230 retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
231 innerListElem, writesPerTx, dataStore);
235 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
236 if (StartTestInput.Operation.DELETE == oper) {
237 retVal = new TxchainBaDelete(this.bindingDataBroker, outerListElem,
238 innerListElem, writesPerTx, dataStore);
239 } else if (StartTestInput.Operation.READ == oper) {
240 retVal = new TxchainBaRead(this.bindingDataBroker,outerListElem,
241 innerListElem,writesPerTx, dataStore);
243 retVal = new TxchainBaWrite(this.bindingDataBroker, oper, outerListElem,
244 innerListElem, writesPerTx, dataStore);
247 if (StartTestInput.Operation.DELETE == oper) {
248 retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
249 innerListElem, writesPerTx, dataStore);
250 } else if (StartTestInput.Operation.READ == oper) {
251 retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
252 innerListElem, writesPerTx, dataStore);
255 retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
256 innerListElem,writesPerTx, dataStore);
261 execStatus.set(ExecStatus.Idle);