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.listener.DsbenchmarkListenerProvider;
23 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
24 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
25 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
26 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
27 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
28 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
29 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
30 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
31 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
32 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
33 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
34 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
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;
51 import com.google.common.util.concurrent.Futures;
53 public class DsbenchmarkProvider implements BindingAwareProvider, DsbenchmarkService, AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
56 private static final InstanceIdentifier<TestExec> TEST_EXEC_IID = InstanceIdentifier.builder(TestExec.class).build();
57 private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID = InstanceIdentifier.builder(TestStatus.class).build();
59 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<ExecStatus>( 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 bindingDataBroker; // Async Binding-Aware Broker for use in tx chains; initialized to ping-pong
63 // broker in default config (see default-config.xml and dsbenchmark-impl.yang)
64 private DataBroker dataBroker; // "Legacy" OSGI Data Broker for use in simple transactions
65 private RpcRegistration<DsbenchmarkService> dstReg;
67 private long testsCompleted = 0;
69 public DsbenchmarkProvider(DOMDataBroker domDataBroker, DataBroker bindingDataBroker) {
70 // We have to get the DOMDataBroker via the constructor,
71 // since we can't get it from the session
72 this.domDataBroker = domDataBroker;
73 this.bindingDataBroker = bindingDataBroker;
77 public void onSessionInitiated(ProviderContext session) {
78 this.dataBroker = session.getSALService(DataBroker.class);
79 this.dstReg = session.addRpcImplementation( DsbenchmarkService.class, this );
80 listenerProvider.setDataBroker(dataBroker);
81 setTestOperData(this.execStatus.get(), testsCompleted);
83 LOG.info("DsbenchmarkProvider Session Initiated");
87 public void close() throws Exception {
89 LOG.info("DsbenchmarkProvider Closed");
93 public Future<RpcResult<Void>> cleanupStore() {
95 LOG.info("Data Store cleaned up");
96 return Futures.immediateFuture( RpcResultBuilder.<Void> success().build() );
100 public Future<RpcResult<StartTestOutput>> startTest(StartTestInput input) {
101 LOG.info("Starting the data store benchmark test, input: {}", input);
103 // Check if there is a test in progress
104 if ( execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false ) {
105 LOG.info("Test in progress");
106 return RpcResultBuilder.success(new StartTestOutputBuilder()
107 .setStatus(StartTestOutput.Status.TESTINPROGRESS)
108 .build()).buildFuture();
111 // Cleanup data that may be left over from a previous test run
114 // Get the appropriate writer based on operation type and data format
115 DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
117 // Create listeners on OPERATIONAL and CONFIG test data subtrees
118 listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
120 long startTime, endTime, listCreateTime, execTime;
122 startTime = System.nanoTime();
123 dsWriter.createList();
124 endTime = System.nanoTime();
125 listCreateTime = (endTime - startTime) / 1000;
127 // Run the test and measure the execution time
129 startTime = System.nanoTime();
130 dsWriter.executeList();
131 endTime = System.nanoTime();
132 execTime = (endTime - startTime) / 1000;
134 this.testsCompleted++;
136 } catch ( Exception e ) {
137 LOG.error( "Test error: {}", e.toString());
138 execStatus.set( ExecStatus.Idle );
139 return RpcResultBuilder.success(new StartTestOutputBuilder()
140 .setStatus(StartTestOutput.Status.FAILED)
141 .build()).buildFuture();
144 LOG.info("Test finished");
145 setTestOperData( ExecStatus.Idle, testsCompleted);
146 execStatus.set(ExecStatus.Idle);
148 // Get the number of data change events and cleanup the data change listeners
149 long numEvents = listenerProvider.getEventCountAndDestroyListeners();
151 StartTestOutput output = new StartTestOutputBuilder()
152 .setStatus(StartTestOutput.Status.OK)
153 .setListBuildTime(listCreateTime)
154 .setExecTime(execTime)
155 .setTxOk((long)dsWriter.getTxOk())
157 .setTxError((long)dsWriter.getTxError())
160 return RpcResultBuilder.success(output).buildFuture();
163 private void setTestOperData( ExecStatus sts, long tstCompl ) {
164 TestStatus status = new TestStatusBuilder()
166 .setTestsCompleted(tstCompl)
169 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
170 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
173 tx.submit().checkedGet();
174 } catch (TransactionCommitFailedException e) {
175 throw new IllegalStateException(e);
178 LOG.info("DataStore test oper status populated: {}", status);
181 private void cleanupTestStore() {
182 TestExec data = new TestExecBuilder()
183 .setOuterList(Collections.<OuterList>emptyList())
186 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
187 tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
189 tx.submit().checkedGet();
190 LOG.info("DataStore config test data cleaned up");
191 } catch (TransactionCommitFailedException e) {
192 LOG.info("Failed to cleanup DataStore configtest data");
193 throw new IllegalStateException(e);
196 tx = dataBroker.newWriteOnlyTransaction();
197 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
199 tx.submit().checkedGet();
200 LOG.info("DataStore operational test data cleaned up");
201 } catch (TransactionCommitFailedException e) {
202 LOG.info("Failed to cleanup DataStore operational test data");
203 throw new IllegalStateException(e);
208 private DatastoreAbstractWriter getDatastoreWriter(StartTestInput input) {
210 final DatastoreAbstractWriter retVal;
212 StartTestInput.TransactionType txType = input.getTransactionType();
213 StartTestInput.Operation oper = input.getOperation();
214 StartTestInput.DataFormat dataFormat = input.getDataFormat();
215 StartTestInput.DataStore dataStore = input.getDataStore();
216 int outerListElem = input.getOuterElements().intValue();
217 int innerListElem = input.getInnerElements().intValue();
218 int writesPerTx = input.getPutsPerTx().intValue();
221 if (txType == StartTestInput.TransactionType.SIMPLETX) {
222 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
223 if (StartTestInput.Operation.DELETE == oper) {
224 retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
225 innerListElem,writesPerTx, dataStore);
226 } else if (StartTestInput.Operation.READ == oper) {
227 retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
228 innerListElem, writesPerTx, dataStore);
230 retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
231 innerListElem, writesPerTx, dataStore);
234 if (StartTestInput.Operation.DELETE == oper) {
235 retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
236 innerListElem, writesPerTx, dataStore);
237 } else if (StartTestInput.Operation.READ == oper) {
238 retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
239 innerListElem, writesPerTx, dataStore);
241 retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
242 innerListElem, writesPerTx, dataStore);
246 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
247 if (StartTestInput.Operation.DELETE == oper) {
248 retVal = new TxchainBaDelete(this.bindingDataBroker, outerListElem,
249 innerListElem, writesPerTx, dataStore);
250 } else if (StartTestInput.Operation.READ == oper) {
251 retVal = new TxchainBaRead(this.bindingDataBroker,outerListElem,
252 innerListElem,writesPerTx, dataStore);
254 retVal = new TxchainBaWrite(this.bindingDataBroker, oper, outerListElem,
255 innerListElem, writesPerTx, dataStore);
258 if (StartTestInput.Operation.DELETE == oper) {
259 retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
260 innerListElem, writesPerTx, dataStore);
261 } else if (StartTestInput.Operation.READ == oper) {
262 retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
263 innerListElem, writesPerTx, dataStore);
266 retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
267 innerListElem,writesPerTx, dataStore);
272 execStatus.set(ExecStatus.Idle);