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