Migrate dsbenchmark to OSGi DS 87/97687/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 30 Sep 2021 12:09:35 +0000 (14:09 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 30 Sep 2021 12:10:10 +0000 (14:10 +0200)
Ditch blueprint and use turn DsbenchmarkProvider into a simple
component.

JIRA: CONTROLLER-2001
Change-Id: I5378359af1ae12d2f7cc03c281599f71ded5a06a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
benchmark/dsbenchmark/pom.xml
benchmark/dsbenchmark/src/main/java/org/opendaylight/dsbenchmark/DsbenchmarkProvider.java
benchmark/dsbenchmark/src/main/java/org/opendaylight/dsbenchmark/listener/DsbenchmarkListenerProvider.java
benchmark/dsbenchmark/src/main/resources/OSGI-INF/blueprint/dsbenchmark.xml [deleted file]

index 1a3d389..0936fcf 100644 (file)
@@ -36,5 +36,19 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <groupId>org.opendaylight.yangtools</groupId>
       <artifactId>yang-data-impl</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.guicedee.services</groupId>
+      <artifactId>javax.inject</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>javax.annotation</groupId>
+      <artifactId>javax.annotation-api</artifactId>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 </project>
index eea490a..0bcc581 100644 (file)
@@ -7,11 +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 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;
@@ -26,6 +31,7 @@ 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;
@@ -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.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,34 +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 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 dataBroker) {
-        this.domDataBroker = domDataBroker;
-        this.dataBroker = dataBroker;
-    }
-
+    @Inject
+    @Activate
     @SuppressWarnings("checkstyle:illegalCatch")
-    public void init() {
-        listenerProvider.setDataBroker(dataBroker);
+    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);
         }
 
+        rpcReg = rpcService.registerRpcImplementation(DsbenchmarkService.class, this);
         LOG.info("DsbenchmarkProvider initiated");
     }
 
     @Override
+    @PreDestroy
+    @Deactivate
     public void close() {
+        rpcReg.close();
         LOG.info("DsbenchmarkProvider closed");
     }
 
@@ -131,7 +150,7 @@ public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
             endTime = System.nanoTime();
             execTime = (endTime - startTime) / 1000;
 
-            this.testsCompleted++;
+            testsCompleted++;
 
         } catch (final Exception e) {
             LOG.error("Test error: {}", e.toString());
@@ -221,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.dataBroker, outerListElem,
+                        retVal = new SimpletxBaDelete(dataBroker, outerListElem,
                                 innerListElem,writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
+                        retVal = new SimpletxBaRead(dataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else {
-                        retVal = new SimpletxBaWrite(this.dataBroker, 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.dataBroker, outerListElem,
+                        retVal = new TxchainBaDelete(dataBroker, outerListElem,
                                 innerListElem, writesPerTx, dataStore);
                     } else if (StartTestInput.Operation.READ == oper) {
-                        retVal = new TxchainBaRead(this.dataBroker, outerListElem,
+                        retVal = new TxchainBaRead(dataBroker, outerListElem,
                                 innerListElem,writesPerTx, dataStore);
                     } else {
-                        retVal = new TxchainBaWrite(this.dataBroker, 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);
                     }
                 }
index 6d52d60..6bc931c 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.dsbenchmark.listener;
 
+import static java.util.Objects.requireNonNull;
+
 import java.util.ArrayList;
 import java.util.List;
 import org.opendaylight.mdsal.binding.api.DataBroker;
@@ -22,12 +24,11 @@ public class DsbenchmarkListenerProvider {
     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkListenerProvider.class);
     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
             InstanceIdentifier.builder(TestExec.class).build();
-    private final List<ListenerRegistration<DsbenchmarkListener>> listeners =
-            new ArrayList<>();
-    private DataBroker dataBroker;
+    private final List<ListenerRegistration<DsbenchmarkListener>> listeners = new ArrayList<>();
+    private final DataBroker dataBroker;
 
-    public void setDataBroker(final DataBroker dataBroker) {
-        this.dataBroker = dataBroker;
+    public DsbenchmarkListenerProvider(final DataBroker dataBroker) {
+        this.dataBroker = requireNonNull(dataBroker);
         LOG.debug("DsbenchmarkListenerProvider created");
     }
 
diff --git a/benchmark/dsbenchmark/src/main/resources/OSGI-INF/blueprint/dsbenchmark.xml b/benchmark/dsbenchmark/src/main/resources/OSGI-INF/blueprint/dsbenchmark.xml
deleted file mode 100644 (file)
index 5b47231..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Copyright (c) 2017 Inocybe Technologies Inc. and others.  All rights reserved.
-
- This program and the accompanying materials are made available under the
- terms of the Eclipse Public License v1.0 which accompanies this distribution,
- and is available at http://www.eclipse.org/legal/epl-v10.html
--->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
-           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
-           odl:use-default-for-reference-types="true">
-
-  <reference id="domDataBroker" interface="org.opendaylight.mdsal.dom.api.DOMDataBroker"/>
-  <reference id="dataBroker" interface="org.opendaylight.mdsal.binding.api.DataBroker"/>
-
-  <bean id="provider" class="org.opendaylight.dsbenchmark.DsbenchmarkProvider"
-          init-method="init" destroy-method="close">
-    <argument ref="domDataBroker"/>
-    <argument ref="dataBroker"/>
-  </bean>
-
-  <odl:rpc-implementation ref="provider"/>
-</blueprint>