Migrate ntfbenchmark to OSGi DS
[controller.git] / benchmark / ntfbenchmark / src / main / java / ntfbenchmark / impl / NtfbenchmarkProvider.java
index 35bea7808f1303137bfd915457a77b0b71aadddb..acd5af61436a9be62f886e8e1fdfa6372c286c6a 100644 (file)
@@ -5,60 +5,76 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package ntfbenchmark.impl;
 
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.binding.api.NotificationService;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
-import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.NtfbenchmarkService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.StartTestInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.StartTestInput.ProducerType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.StartTestOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.StartTestOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.TestStatusInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ntfbenchmark.rev150105.TestStatusOutput;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 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 NtfbenchmarkProvider implements BindingAwareProvider, AutoCloseable, NtfbenchmarkService {
-
+@Singleton
+@Component(service = {})
+@RequireServiceComponentRuntime
+public final class NtfbenchmarkProvider implements AutoCloseable, NtfbenchmarkService {
     private static final Logger LOG = LoggerFactory.getLogger(NtfbenchmarkProvider.class);
-    private NotificationService listenService;
-    private NotificationPublishService publishService;
-    private static final int testTimeout = 5;
-
-    public NtfbenchmarkProvider(NotificationService listenServiceDependency,
-            NotificationPublishService publishServiceDependency) {
-        LOG.info("NtfbenchmarkProvider Constructor");
-        listenService = listenServiceDependency;
-        publishService = publishServiceDependency;
+    private static final int TEST_TIMEOUT = 5;
+
+    private final NotificationService listenService;
+    private final NotificationPublishService publishService;
+    private final Registration reg;
+
+    @Inject
+    @Activate
+    public NtfbenchmarkProvider(@Reference final NotificationService listenService,
+            @Reference final NotificationPublishService publishService,
+            @Reference final RpcProviderService rpcService) {
+        this.listenService = requireNonNull(listenService);
+        this.publishService = requireNonNull(publishService);
+        reg = rpcService.registerRpcImplementation(NtfbenchmarkService.class, this);
+        LOG.debug("NtfbenchmarkProvider initiated");
     }
 
     @Override
-    public void onSessionInitiated(final ProviderContext session) {
-        LOG.info("NtfbenchmarkProvider Session Initiated");
-        session.addRpcImplementation(NtfbenchmarkService.class, this);
+    @PreDestroy
+    @Deactivate
+    public void close() {
+        reg.close();
+        LOG.info("NtfbenchmarkProvider closed");
     }
 
     @Override
-    public void close() throws Exception {
-        LOG.info("NtfbenchmarkProvider Closed");
-    }
-
-    @Override
-    public Future<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
+    public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
         final int producerCount = input.getProducers().intValue();
         final int listenerCount = input.getListeners().intValue();
         final int iterations = input.getIterations().intValue();
@@ -88,16 +104,17 @@ public class NtfbenchmarkProvider implements BindingAwareProvider, AutoCloseable
             final long startTime = System.nanoTime();
 
             for (int i = 0; i < input.getProducers().intValue(); i++) {
-                executor.submit(producers.get(i));
+                // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE for now, but we should check some more
+                verifyNotNull(executor.submit(producers.get(i)));
             }
             executor.shutdown();
             try {
-                executor.awaitTermination(testTimeout, TimeUnit.MINUTES);
+                executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
                 for (ListenerRegistration<NtfbenchTestListener> listenerRegistration : listeners) {
                     listenerRegistration.getInstance().getAllDone().get();
                 }
             } catch (final InterruptedException | ExecutionException e) {
-                LOG.error("Out of time: test did not finish within the {} min deadline ", testTimeout);
+                LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
             }
 
             final long producerEndTime = System.nanoTime();
@@ -111,7 +128,6 @@ public class NtfbenchmarkProvider implements BindingAwareProvider, AutoCloseable
                 allListeners += listenerRegistration.getInstance().getReceived();
             }
 
-            final long listenerEndTime = System.nanoTime();
             final long listenerElapsedTime = producerEndTime - startTime;
 
             LOG.info("Test Done");
@@ -121,16 +137,16 @@ public class NtfbenchmarkProvider implements BindingAwareProvider, AutoCloseable
                 allProducersError += abstractNtfbenchProducer.getNtfError();
             }
 
-            final StartTestOutput output =
-                    new StartTestOutputBuilder()
-                            .setProducerElapsedTime(producerElapsedTime / 1000000)
-                            .setListenerElapsedTime(listenerElapsedTime / 1000000)
-                            .setListenerOk(allListeners)
-                            .setProducerOk(allProducersOk)
-                            .setProducerError(allProducersError)
-                            .setProducerRate(((allProducersOk + allProducersError) * 1000000000) / producerElapsedTime)
-                            .setListenerRate((allListeners * 1000000000) / listenerElapsedTime)
-                           .build();
+            final StartTestOutput output = new StartTestOutputBuilder()
+                .setProducerElapsedTime(Uint32.valueOf(producerElapsedTime / 1000000))
+                .setListenerElapsedTime(Uint32.valueOf(listenerElapsedTime / 1000000))
+                .setListenerOk(Uint32.valueOf(allListeners))
+                .setProducerOk(Uint32.valueOf(allProducersOk))
+                .setProducerError(Uint32.valueOf(allProducersError))
+                .setProducerRate(
+                    Uint32.valueOf((allProducersOk + allProducersError) * 1000000000 / producerElapsedTime))
+                .setListenerRate(Uint32.valueOf(allListeners * 1000000000 / listenerElapsedTime))
+                .build();
             return RpcResultBuilder.success(output).buildFuture();
         } finally {
             for (final ListenerRegistration<NtfbenchTestListener> listenerRegistration : listeners) {
@@ -140,9 +156,7 @@ public class NtfbenchmarkProvider implements BindingAwareProvider, AutoCloseable
     }
 
     @Override
-    public Future<RpcResult<TestStatusOutput>> testStatus() {
-        // TODO Auto-generated method stub
-        return null;
+    public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
+        throw new UnsupportedOperationException("Not implemented");
     }
-
 }