Fix checkstyle/spotbugs violations
[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 com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.atomic.AtomicReference;
15 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
16 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
17 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
18 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
19 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
20 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
21 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
22 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
23 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
24 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
25 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
26 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
27 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutputBuilder;
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 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
52
53     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
54     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
55             InstanceIdentifier.builder(TestExec.class).build();
56     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
57             InstanceIdentifier.builder(TestStatus.class).build();
58
59     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(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 dataBroker; // Async Binding-Aware Broker for use in tx chains
63
64     private long testsCompleted = 0;
65
66     public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker dataBroker) {
67         this.domDataBroker = domDataBroker;
68         this.dataBroker = dataBroker;
69     }
70
71     @SuppressWarnings("checkstyle:illegalCatch")
72     public void init() {
73         listenerProvider.setDataBroker(dataBroker);
74
75         try {
76             // We want to set the initial operation status so users can detect we are ready to start test.
77             setTestOperData(this.execStatus.get(), testsCompleted);
78         } catch (final Exception e) {
79             // TODO: Use a singleton service to make sure the initial write is performed only once.
80             LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData", e);
81         }
82
83         LOG.info("DsbenchmarkProvider initiated");
84     }
85
86     @Override
87     public void close() {
88         LOG.info("DsbenchmarkProvider closed");
89     }
90
91     @Override
92     public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
93         cleanupTestStore();
94         LOG.debug("Data Store cleaned up");
95         return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
96     }
97
98     @Override
99     @SuppressWarnings("checkstyle:illegalCatch")
100     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final 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
121         long startTime = System.nanoTime();
122         dsWriter.createList();
123         long endTime = System.nanoTime();
124         final long listCreateTime = (endTime - startTime) / 1000;
125
126         // Run the test and measure the execution time
127         long execTime;
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 (final 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 numDataChanges = listenerProvider.getDataChangeCount();
150         long numEvents = listenerProvider.getEventCountAndDestroyListeners();
151
152         StartTestOutput output = new StartTestOutputBuilder()
153                 .setStatus(StartTestOutput.Status.OK)
154                 .setListBuildTime(listCreateTime)
155                 .setExecTime(execTime)
156                 .setTxOk((long)dsWriter.getTxOk())
157                 .setNtfOk(numEvents)
158                 .setDataChangeEventsOk(numDataChanges)
159                 .setTxError((long)dsWriter.getTxError())
160                 .build();
161
162         return RpcResultBuilder.success(output).buildFuture();
163     }
164
165     private void setTestOperData(final ExecStatus sts, final long tstCompl) {
166         TestStatus status = new TestStatusBuilder()
167                 .setExecStatus(sts)
168                 .setTestsCompleted(tstCompl)
169                 .build();
170
171         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
172         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
173
174         try {
175             tx.commit().get();
176         } catch (final InterruptedException | ExecutionException e) {
177             throw new IllegalStateException(e);
178         }
179
180         LOG.debug("DataStore test oper status populated: {}", status);
181     }
182
183     private void cleanupTestStore() {
184         TestExec data = new TestExecBuilder()
185                 .setOuterList(Collections.<OuterList>emptyList())
186                 .build();
187
188         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
189         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
190         try {
191             tx.commit().get();
192             LOG.debug("DataStore config test data cleaned up");
193         } catch (final InterruptedException | ExecutionException e) {
194             LOG.info("Failed to cleanup DataStore configtest data");
195             throw new IllegalStateException(e);
196         }
197
198         tx = dataBroker.newWriteOnlyTransaction();
199         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
200         try {
201             tx.commit().get();
202             LOG.debug("DataStore operational test data cleaned up");
203         } catch (final InterruptedException | ExecutionException e) {
204             LOG.info("Failed to cleanup DataStore operational test data");
205             throw new IllegalStateException(e);
206         }
207
208     }
209
210     private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
211
212         final DatastoreAbstractWriter retVal;
213
214         StartTestInput.TransactionType txType = input.getTransactionType();
215         StartTestInput.Operation oper = input.getOperation();
216         StartTestInput.DataFormat dataFormat = input.getDataFormat();
217         StartTestInput.DataStore dataStore = input.getDataStore();
218         int outerListElem = input.getOuterElements().intValue();
219         int innerListElem = input.getInnerElements().intValue();
220         int writesPerTx = input.getPutsPerTx().intValue();
221
222         try {
223             if (txType == StartTestInput.TransactionType.SIMPLETX) {
224                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
225                     if (StartTestInput.Operation.DELETE == oper) {
226                         retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
227                                 innerListElem,writesPerTx, dataStore);
228                     } else if (StartTestInput.Operation.READ == oper) {
229                         retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
230                                 innerListElem, writesPerTx, dataStore);
231                     } else {
232                         retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
233                                 innerListElem, writesPerTx, dataStore);
234                     }
235                 } else {
236                     if (StartTestInput.Operation.DELETE == oper) {
237                         retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
238                                 innerListElem, writesPerTx, dataStore);
239                     } else if (StartTestInput.Operation.READ == oper) {
240                         retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
241                                 innerListElem, writesPerTx, dataStore);
242                     } else {
243                         retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
244                                 innerListElem, writesPerTx, dataStore);
245                     }
246                 }
247             } else {
248                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
249                     if (StartTestInput.Operation.DELETE == oper) {
250                         retVal = new TxchainBaDelete(this.dataBroker, outerListElem,
251                                 innerListElem, writesPerTx, dataStore);
252                     } else if (StartTestInput.Operation.READ == oper) {
253                         retVal = new TxchainBaRead(this.dataBroker, outerListElem,
254                                 innerListElem,writesPerTx, dataStore);
255                     } else {
256                         retVal = new TxchainBaWrite(this.dataBroker, oper, outerListElem,
257                                 innerListElem, writesPerTx, dataStore);
258                     }
259                 } else {
260                     if (StartTestInput.Operation.DELETE == oper) {
261                         retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
262                                 innerListElem, writesPerTx, dataStore);
263                     } else if (StartTestInput.Operation.READ == oper) {
264                         retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
265                                 innerListElem, writesPerTx, dataStore);
266
267                     } else {
268                         retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
269                                 innerListElem,writesPerTx, dataStore);
270                     }
271                 }
272             }
273         } finally {
274             execStatus.set(ExecStatus.Idle);
275         }
276         return retVal;
277     }
278 }