2c25951986bf952efe52fb181ff085c96422b221
[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 java.util.Collections;
11 import java.util.concurrent.Future;
12 import java.util.concurrent.atomic.AtomicReference;
13
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;
50
51 import com.google.common.util.concurrent.Futures;
52
53 public class DsbenchmarkProvider implements BindingAwareProvider, DsbenchmarkService, AutoCloseable {
54
55     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
56     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
57             InstanceIdentifier.builder(TestExec.class).build();
58     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
59             InstanceIdentifier.builder(TestStatus.class).build();
60
61     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
62     private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
63     private final DOMDataBroker domDataBroker;  // Async DOM Broker for use with all DOM operations
64     private final DataBroker bindingDataBroker; // Async Binding-Aware Broker for use in tx chains; initialized to
65                                                 // ping-pong broker in default config (see default-config.xml and
66                                                 // dsbenchmark-impl.yang)
67     private DataBroker dataBroker;              // "Legacy" OSGI Data Broker for use in simple transactions
68     private RpcRegistration<DsbenchmarkService> dstReg;
69
70     private long testsCompleted = 0;
71
72     public DsbenchmarkProvider(DOMDataBroker domDataBroker, DataBroker bindingDataBroker) {
73         // We have to get the DOMDataBroker via the constructor,
74         // since we can't get it from the session
75         this.domDataBroker = domDataBroker;
76         this.bindingDataBroker = bindingDataBroker;
77     }
78
79     @Override
80     public void onSessionInitiated(ProviderContext session) {
81         this.dataBroker = session.getSALService(DataBroker.class);
82         this.dstReg = session.addRpcImplementation( DsbenchmarkService.class, this );
83         listenerProvider.setDataBroker(dataBroker);
84         setTestOperData(this.execStatus.get(), testsCompleted);
85
86         LOG.info("DsbenchmarkProvider Session Initiated");
87     }
88
89     @Override
90     public void close() throws Exception {
91         dstReg.close();
92         LOG.info("DsbenchmarkProvider Closed");
93     }
94
95     @Override
96     public Future<RpcResult<Void>> cleanupStore() {
97         cleanupTestStore();
98         LOG.info("Data Store cleaned up");
99         return Futures.immediateFuture( RpcResultBuilder.<Void>success().build());
100     }
101
102     @Override
103     public Future<RpcResult<StartTestOutput>> startTest(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 (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( ExecStatus sts, long tstCompl ) {
169         TestStatus status = new TestStatusBuilder()
170                 .setExecStatus(sts)
171                 .setTestsCompleted(tstCompl)
172                 .build();
173
174         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
175         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
176
177         try {
178             tx.submit().checkedGet();
179         } catch (TransactionCommitFailedException e) {
180             throw new IllegalStateException(e);
181         }
182
183         LOG.info("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 = dataBroker.newWriteOnlyTransaction();
192         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
193         try {
194             tx.submit().checkedGet();
195             LOG.info("DataStore config test data cleaned up");
196         } catch (TransactionCommitFailedException e) {
197             LOG.info("Failed to cleanup DataStore configtest data");
198             throw new IllegalStateException(e);
199         }
200
201         tx = dataBroker.newWriteOnlyTransaction();
202         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
203         try {
204             tx.submit().checkedGet();
205             LOG.info("DataStore operational test data cleaned up");
206         } catch (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(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.dataBroker, outerListElem,
230                                 innerListElem,writesPerTx, dataStore);
231                     } else if (StartTestInput.Operation.READ == oper) {
232                         retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
233                                 innerListElem, writesPerTx, dataStore);
234                     } else {
235                         retVal = new SimpletxBaWrite(this.dataBroker, 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.bindingDataBroker, outerListElem,
254                                 innerListElem, writesPerTx, dataStore);
255                     } else if (StartTestInput.Operation.READ == oper) {
256                         retVal = new TxchainBaRead(this.bindingDataBroker,outerListElem,
257                                 innerListElem,writesPerTx, dataStore);
258                     } else {
259                         retVal = new TxchainBaWrite(this.bindingDataBroker, 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.