Added the option for user-confugrable number of data tree change listeners that liste...
[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 = InstanceIdentifier.builder(TestExec.class).build();
57     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID = InstanceIdentifier.builder(TestStatus.class).build();
58
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;
66
67     private long testsCompleted = 0;
68
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;
74     }
75
76     @Override
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);
82
83         LOG.info("DsbenchmarkProvider Session Initiated");
84     }
85
86     @Override
87     public void close() throws Exception {
88         dstReg.close();
89         LOG.info("DsbenchmarkProvider Closed");
90     }
91
92     @Override
93     public Future<RpcResult<Void>> cleanupStore() {
94         cleanupTestStore();
95         LOG.info("Data Store cleaned up");
96         return Futures.immediateFuture( RpcResultBuilder.<Void> success().build() );
97     }
98
99     @Override
100     public Future<RpcResult<StartTestOutput>> startTest(StartTestInput input) {
101         LOG.info("Starting the data store benchmark test, input: {}", input);
102
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();
109         }
110
111         // Cleanup data that may be left over from a previous test run
112         cleanupTestStore();
113
114         // Get the appropriate writer based on operation type and data format
115         DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
116
117         // Create listeners on OPERATIONAL and CONFIG test data subtrees
118         listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
119
120         long startTime, endTime, listCreateTime, execTime;
121
122         startTime = System.nanoTime();
123         dsWriter.createList();
124         endTime = System.nanoTime();
125         listCreateTime = (endTime - startTime) / 1000;
126
127         // Run the test and measure the execution time
128         try {
129             startTime = System.nanoTime();
130             dsWriter.executeList();
131             endTime = System.nanoTime();
132             execTime = (endTime - startTime) / 1000;
133
134             this.testsCompleted++;
135
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();
142         }
143
144         LOG.info("Test finished");
145         setTestOperData( ExecStatus.Idle, testsCompleted);
146         execStatus.set(ExecStatus.Idle);
147
148         // Get the number of data change events and cleanup the data change listeners
149         long numEvents = listenerProvider.getEventCountAndDestroyListeners();
150
151         StartTestOutput output = new StartTestOutputBuilder()
152                 .setStatus(StartTestOutput.Status.OK)
153                 .setListBuildTime(listCreateTime)
154                 .setExecTime(execTime)
155                 .setTxOk((long)dsWriter.getTxOk())
156                 .setNtfOk(numEvents)
157                 .setTxError((long)dsWriter.getTxError())
158                 .build();
159
160         return RpcResultBuilder.success(output).buildFuture();
161     }
162
163     private void setTestOperData( ExecStatus sts, long tstCompl ) {
164         TestStatus status = new TestStatusBuilder()
165                 .setExecStatus(sts)
166                 .setTestsCompleted(tstCompl)
167                 .build();
168
169         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
170         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
171
172         try {
173             tx.submit().checkedGet();
174         } catch (TransactionCommitFailedException e) {
175             throw new IllegalStateException(e);
176         }
177
178         LOG.info("DataStore test oper status populated: {}", status);
179     }
180
181     private void cleanupTestStore() {
182         TestExec data = new TestExecBuilder()
183                 .setOuterList(Collections.<OuterList>emptyList())
184                 .build();
185
186         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
187         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
188         try {
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);
194         }
195         
196         tx = dataBroker.newWriteOnlyTransaction();
197         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
198         try {
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);
204         }
205
206     }
207
208     private DatastoreAbstractWriter getDatastoreWriter(StartTestInput input) {
209
210         final DatastoreAbstractWriter retVal;
211
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();
219
220         try {
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);
229                     } else {
230                         retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
231                                 innerListElem, writesPerTx, dataStore);
232                     }
233                 } else {
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);
240                     } else {
241                         retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
242                                 innerListElem, writesPerTx, dataStore);
243                     }
244                 }
245             } else {
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);
253                     } else {
254                         retVal = new TxchainBaWrite(this.bindingDataBroker, oper, outerListElem,
255                                 innerListElem, writesPerTx, dataStore);
256                     }
257                 } else {
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);
264
265                     } else {
266                         retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
267                                 innerListElem,writesPerTx, dataStore);
268                     }
269                 }
270             }
271         } finally {
272             execStatus.set(ExecStatus.Idle);
273         }
274         return retVal;
275     }
276 }

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