Migrate dsbenchmark to OSGi DS
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / DsbenchmarkProvider.java
index 160f4888d096365c6dd8f3c4d9d160056baee10e..0bcc5811844d4c8fdecf2682d9b9441067903350 100644 (file)
@@ -7,15 +7,16 @@
  */
 package org.opendaylight.dsbenchmark;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.Collections;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicReference;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
@@ -29,6 +30,11 @@ import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutputBuilder;
@@ -41,15 +47,23 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchm
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
-
+@Singleton
+@Component(service = { })
+@RequireServiceComponentRuntime
+public final class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
             InstanceIdentifier.builder(TestExec.class).build();
@@ -57,38 +71,39 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
             InstanceIdentifier.builder(TestStatus.class).build();
 
     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
-    private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
+    private final DsbenchmarkListenerProvider listenerProvider;
     private final DOMDataBroker domDataBroker;  // Async DOM Broker for use with all DOM operations
-    private final DataBroker txChainDataBroker; // Async Binding-Aware Broker for use in tx chains; initialized to
-                                                // ping-pong broker in default config (see default-config.xml and
-                                                // dsbenchmark-impl.yang)
-    private final DataBroker simpleTxDataBroker;      // "Legacy" OSGI Data Broker for use in simple transactions
+    private final DataBroker dataBroker; // Async Binding-Aware Broker for use in tx chains
+    private final Registration rpcReg;
 
     private long testsCompleted = 0;
 
-    public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker txChainDataBroker,
-            final DataBroker simpleTxDataBroker) {
-        this.domDataBroker = domDataBroker;
-        this.txChainDataBroker = txChainDataBroker;
-        this.simpleTxDataBroker = simpleTxDataBroker;
-    }
-
-    public void init() {
-        listenerProvider.setDataBroker(simpleTxDataBroker);
+    @Inject
+    @Activate
+    @SuppressWarnings("checkstyle:illegalCatch")
+    public DsbenchmarkProvider(@Reference final DOMDataBroker domDataBroker, @Reference final DataBroker dataBroker,
+            @Reference final RpcProviderService rpcService) {
+        this.domDataBroker = requireNonNull(domDataBroker);
+        this.dataBroker = requireNonNull(dataBroker);
+        listenerProvider = new DsbenchmarkListenerProvider(dataBroker);
 
         try {
             // We want to set the initial operation status so users can detect we are ready to start test.
-            setTestOperData(this.execStatus.get(), testsCompleted);
+            setTestOperData(execStatus.get(), testsCompleted);
         } catch (final Exception e) {
             // TODO: Use a singleton service to make sure the initial write is performed only once.
-            LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData: {}", e);
+            LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData", e);
         }
 
+        rpcReg = rpcService.registerRpcImplementation(DsbenchmarkService.class, this);
         LOG.info("DsbenchmarkProvider initiated");
     }
 
     @Override
+    @PreDestroy
+    @Deactivate
     public void close() {
+        rpcReg.close();
         LOG.info("DsbenchmarkProvider closed");
     }
 
@@ -100,6 +115,7 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
     }
 
     @Override
+    @SuppressWarnings("checkstyle:illegalCatch")
     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
         LOG.info("Starting the data store benchmark test, input: {}", input);
 
@@ -120,21 +136,21 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
         // Create listeners on OPERATIONAL and CONFIG test data subtrees
         listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
 
-        long startTime, endTime, listCreateTime, execTime;
 
-        startTime = System.nanoTime();
+        long startTime = System.nanoTime();
         dsWriter.createList();
-        endTime = System.nanoTime();
-        listCreateTime = (endTime - startTime) / 1000;
+        long endTime = System.nanoTime();
+        final long listCreateTime = (endTime - startTime) / 1000;
 
         // Run the test and measure the execution time
+        long execTime;
         try {
             startTime = System.nanoTime();
             dsWriter.executeList();
             endTime = System.nanoTime();
             execTime = (endTime - startTime) / 1000;
 
-            this.testsCompleted++;
+            testsCompleted++;
 
         } catch (final Exception e) {
             LOG.error("Test error: {}", e.toString());
@@ -156,10 +172,10 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
                 .setStatus(StartTestOutput.Status.OK)
                 .setListBuildTime(listCreateTime)
                 .setExecTime(execTime)
-                .setTxOk((long)dsWriter.getTxOk())
-                .setNtfOk(numEvents)
-                .setDataChangeEventsOk(numDataChanges)
-                .setTxError((long)dsWriter.getTxError())
+                .setTxOk(Uint32.valueOf(dsWriter.getTxOk()))
+                .setNtfOk(Uint32.valueOf(numEvents))
+                .setDataChangeEventsOk(Uint32.valueOf(numDataChanges))
+                .setTxError(Uint32.valueOf(dsWriter.getTxError()))
                 .build();
 
         return RpcResultBuilder.success(output).buildFuture();
@@ -168,15 +184,15 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
     private void setTestOperData(final ExecStatus sts, final long tstCompl) {
         TestStatus status = new TestStatusBuilder()
                 .setExecStatus(sts)
-                .setTestsCompleted(tstCompl)
+                .setTestsCompleted(Uint32.valueOf(tstCompl))
                 .build();
 
-        WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
+        WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
 
         try {
-            tx.submit().checkedGet();
-        } catch (final TransactionCommitFailedException e) {
+            tx.commit().get();
+        } catch (final InterruptedException | ExecutionException e) {
             throw new IllegalStateException(e);
         }
 
@@ -184,26 +200,24 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
     }
 
     private void cleanupTestStore() {
-        TestExec data = new TestExecBuilder()
-                .setOuterList(Collections.<OuterList>emptyList())
-                .build();
+        TestExec data = new TestExecBuilder().setOuterList(Collections.emptyMap()).build();
 
-        WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
+        WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
         try {
-            tx.submit().checkedGet();
+            tx.commit().get();
             LOG.debug("DataStore config test data cleaned up");
-        } catch (final TransactionCommitFailedException e) {
+        } catch (final InterruptedException | ExecutionException e) {
             LOG.info("Failed to cleanup DataStore configtest data");
             throw new IllegalStateException(e);
         }
 
-        tx = simpleTxDataBroker.newWriteOnlyTransaction();
+        tx = dataBroker.newWriteOnlyTransaction();
         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
         try {
-            tx.submit().checkedGet();
+            tx.commit().get();
             LOG.debug("DataStore operational test data cleaned up");
-        } catch (final TransactionCommitFailedException e) {
+        } catch (final InterruptedException | ExecutionException e) {
             LOG.info("Failed to cleanup DataStore operational test data");
             throw new IllegalStateException(e);
         }
@@ -226,49 +240,49 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
             if (txType == StartTestInput.TransactionType.SIMPLETX) {
                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
                     if (StartTestInput.Operation.DELETE == oper) {
-                        retVal = new SimpletxBaDelete(this.simpleTxDataBroker, outerListElem,
+                        retVal = new SimpletxBaDelete(dataBroker, outerListElem,
                                 innerListElem,writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new SimpletxBaRead(this.simpleTxDataBroker, outerListElem,
+                        retVal = new SimpletxBaRead(dataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else {
-                        retVal = new SimpletxBaWrite(this.simpleTxDataBroker, oper, outerListElem,
+                        retVal = new SimpletxBaWrite(dataBroker, oper, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     }
                 } else {
                     if (StartTestInput.Operation.DELETE == oper) {
-                        retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
+                        retVal = new SimpletxDomDelete(domDataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
+                        retVal = new SimpletxDomRead(domDataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else {
-                        retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
+                        retVal = new SimpletxDomWrite(domDataBroker, oper, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     }
                 }
             } else {
                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
                     if (StartTestInput.Operation.DELETE == oper) {
-                        retVal = new TxchainBaDelete(this.txChainDataBroker, outerListElem,
+                        retVal = new TxchainBaDelete(dataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new TxchainBaRead(this.txChainDataBroker,outerListElem,
+                        retVal = new TxchainBaRead(dataBroker, outerListElem,
                                 innerListElem,writesPerTx, dataStore);
                     } else {
-                        retVal = new TxchainBaWrite(this.txChainDataBroker, oper, outerListElem,
+                        retVal = new TxchainBaWrite(dataBroker, oper, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     }
                 } else {
                     if (StartTestInput.Operation.DELETE == oper) {
-                        retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
+                        retVal = new TxchainDomDelete(domDataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
+                        retVal = new TxchainDomRead(domDataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
 
                     } else {
-                        retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
+                        retVal = new TxchainDomWrite(domDataBroker, oper, outerListElem,
                                 innerListElem,writesPerTx, dataStore);
                     }
                 }