Migrate rpcbenchmark to OSGi DS 26/97726/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Oct 2021 08:12:30 +0000 (10:12 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Oct 2021 06:56:59 +0000 (08:56 +0200)
Ditch blueprint and use turn NtfBenchmarkProvider into a simple
component. Requires some amount of refactoring.

JIRA: CONTROLLER-2001
Change-Id: Ib8db52d91013fe1ae13c55b5537b743f8ccbf320
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
benchmark/rpcbenchmark/pom.xml
benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/AbstractRpcbenchPayloadService.java [new file with mode: 0644]
benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/GlobalBindingRTCServer.java
benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/RoutedBindingRTCServer.java [new file with mode: 0644]
benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/RoutedBindingRTClient.java
benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/RpcbenchmarkProvider.java
benchmark/rpcbenchmark/src/main/resources/OSGI-INF/blueprint/rpcbenchmark.xml [deleted file]

index 978e13e..4ee9114 100644 (file)
@@ -28,5 +28,19 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <groupId>org.opendaylight.mdsal</groupId>
       <artifactId>mdsal-binding-api</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>
diff --git a/benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/AbstractRpcbenchPayloadService.java b/benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/AbstractRpcbenchPayloadService.java
new file mode 100644 (file)
index 0000000..33328f9
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 Cisco Systems 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
+ */
+package rpcbenchmark.impl;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchInput;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchOutput;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchOutputBuilder;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchInput;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchOutput;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchOutputBuilder;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+
+abstract class AbstractRpcbenchPayloadService implements RpcbenchPayloadService {
+    private int numRpcs = 0;
+
+    @Override
+    public final ListenableFuture<RpcResult<GlobalRpcBenchOutput>> globalRpcBench(final GlobalRpcBenchInput input) {
+        numRpcs++;
+        return RpcResultBuilder.success(new GlobalRpcBenchOutputBuilder(input).build()).buildFuture();
+    }
+
+    @Override
+    public final ListenableFuture<RpcResult<RoutedRpcBenchOutput>> routedRpcBench(final RoutedRpcBenchInput input) {
+        numRpcs++;
+        return RpcResultBuilder.success(new RoutedRpcBenchOutputBuilder(input).build()).buildFuture();
+    }
+
+    final int getNumRpcs() {
+        return numRpcs;
+    }
+}
index edded6a..67fde76 100644 (file)
@@ -5,49 +5,28 @@
  * 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 rpcbenchmark.impl;
 
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchInput;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchOutput;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.GlobalRpcBenchOutputBuilder;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchInput;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchOutput;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchOutputBuilder;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class GlobalBindingRTCServer implements RpcbenchPayloadService {
-
+final class GlobalBindingRTCServer extends AbstractRpcbenchPayloadService implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(GlobalBindingRTCServer.class);
-    private int numRpcs = 0;
 
-    public GlobalBindingRTCServer() {
-        LOG.debug("GlobalBindingRTCServer created.");
-    }
+    private final Registration reg;
 
-    @Override
-    public ListenableFuture<RpcResult<GlobalRpcBenchOutput>> globalRpcBench(final GlobalRpcBenchInput input) {
-        GlobalRpcBenchOutput output = new GlobalRpcBenchOutputBuilder(input).build();
-        RpcResult<GlobalRpcBenchOutput> result = RpcResultBuilder.success(output).build();
-        numRpcs++;
-        return Futures.immediateFuture(result);
+    GlobalBindingRTCServer(@Reference final RpcProviderService rpcProvider) {
+        reg = rpcProvider.registerRpcImplementation(RpcbenchPayloadService.class, this);
+        LOG.debug("GlobalBindingRTCServer started");
     }
 
     @Override
-    public ListenableFuture<RpcResult<RoutedRpcBenchOutput>> routedRpcBench(final RoutedRpcBenchInput input) {
-        RoutedRpcBenchOutput output = new RoutedRpcBenchOutputBuilder(input).build();
-        RpcResult<RoutedRpcBenchOutput> result = RpcResultBuilder.success(output).build();
-        numRpcs++;
-        return Futures.immediateFuture(result);
-    }
-
-    public int getNumRpcs() {
-        return numRpcs;
+    public void close() {
+        reg.close();
+        LOG.debug("GlobalBindingRTCServer stopped");
     }
 }
diff --git a/benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/RoutedBindingRTCServer.java b/benchmark/rpcbenchmark/src/main/java/rpcbenchmark/impl/RoutedBindingRTCServer.java
new file mode 100644 (file)
index 0000000..1982beb
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2015 Cisco Systems 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
+ */
+package rpcbenchmark.impl;
+
+import java.util.Set;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
+import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+final class RoutedBindingRTCServer extends AbstractRpcbenchPayloadService implements AutoCloseable {
+    private final Registration reg;
+
+    RoutedBindingRTCServer(final RpcProviderService rpcProvider, final Set<InstanceIdentifier<?>> paths) {
+        reg = rpcProvider.registerRpcImplementation(RpcbenchPayloadService.class, this, paths);
+    }
+
+    @Override
+    public void close() {
+        reg.close();
+    }
+}
index a7cb4bf..d2c3ae3 100644 (file)
@@ -34,18 +34,13 @@ public class RoutedBindingRTClient implements RTCClient {
     private final RpcbenchPayloadService service;
     private final AtomicLong rpcOk = new AtomicLong(0);
     private final AtomicLong rpcError = new AtomicLong(0);
-    private final List<RoutedRpcBenchInput> inVal;
+    private final List<RoutedRpcBenchInput> inVal = new ArrayList<>();
     private final int inSize;
 
     public RoutedBindingRTClient(final RpcConsumerRegistry registry, final int inSize,
             final List<InstanceIdentifier<?>> routeIid) {
-        if (registry != null) {
-            this.service = registry.getRpcService(RpcbenchPayloadService.class);
-        } else {
-            this.service = null;
-        }
+        service = registry.getRpcService(RpcbenchPayloadService.class);
         this.inSize = inSize;
-        this.inVal = new ArrayList<>();
 
         Builder<PayloadKey, Payload> listVals = ImmutableMap.builderWithExpectedSize(inSize);
         for (int i = 0; i < inSize; i++) {
index 0c9a54b..306539c 100644 (file)
@@ -8,6 +8,7 @@
 package rpcbenchmark.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;
@@ -17,9 +18,11 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
 import org.opendaylight.mdsal.binding.api.RpcProviderService;
-import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
@@ -31,38 +34,49 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbench
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 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 RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
-
+@Singleton
+@Component(service = {})
+@RequireServiceComponentRuntime
+public final class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
     private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
     private static final int TEST_TIMEOUT = 5;
 
-    private final GlobalBindingRTCServer globalServer;
     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
     private final RpcProviderService providerRegistry;
     private final RpcConsumerRegistry consumerRegistry;
-
-    public RpcbenchmarkProvider(final RpcProviderService providerRegistry, final RpcConsumerRegistry consumerRegistry,
-            final GlobalBindingRTCServer globalServer) {
-        this.providerRegistry = providerRegistry;
-        this.consumerRegistry = consumerRegistry;
-        this.globalServer = globalServer;
-    }
-
-    public void init() {
+    private final GlobalBindingRTCServer globalServer;
+    private final Registration reg;
+
+    @Inject
+    @Activate
+    public RpcbenchmarkProvider(@Reference final RpcProviderService providerRegistry,
+            @Reference final RpcConsumerRegistry consumerRegistry) {
+        this.providerRegistry = requireNonNull(providerRegistry);
+        this.consumerRegistry = requireNonNull(consumerRegistry);
+        globalServer = new GlobalBindingRTCServer(providerRegistry);
+        reg = providerRegistry.registerRpcImplementation(RpcbenchmarkService.class, this);
         LOG.info("RpcbenchmarkProvider initiated");
     }
 
     @Override
+    @Deactivate
+    @PreDestroy
     public void close() {
+        globalServer.close();
+        reg.close();
         LOG.info("RpcbenchmarkProvider closed");
     }
 
@@ -71,24 +85,17 @@ public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService
         LOG.debug("startTest {}", input);
 
         final RTCClient client;
-        final List<ObjectRegistration<?>> rpcRegs = new ArrayList<>();
+        RoutedBindingRTCServer routed = null;
 
         switch (input.getOperation()) {
             case ROUTEDRTC:
                 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
                 for (int i = 0; i < input.getNumServers().intValue(); i++) {
-                    GlobalBindingRTCServer server = new GlobalBindingRTCServer();
-                    KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
-                            InstanceIdentifier.create(RpcbenchRpcRoutes.class)
-                                .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
-                    routeIid.add(iid);
-
-                    ObjectRegistration<?> routedReg = providerRegistry.registerRpcImplementation(
-                        RpcbenchPayloadService.class, server, Set.of(iid));
-
-                    rpcRegs.add(routedReg);
+                    routeIid.add(InstanceIdentifier.create(RpcbenchRpcRoutes.class)
+                        .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i))));
                 }
 
+                routed = new RoutedBindingRTCServer(providerRegistry, Set.copyOf(routeIid));
                 client = new RoutedBindingRTClient(consumerRegistry, input.getPayloadSize().intValue(), routeIid);
                 break;
 
@@ -136,7 +143,9 @@ public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService
                                             .build();
             return RpcResultBuilder.success(output).buildFuture();
         } finally {
-            rpcRegs.forEach(ObjectRegistration::close);
+            if (routed != null) {
+                routed.close();
+            }
         }
     }
 
diff --git a/benchmark/rpcbenchmark/src/main/resources/OSGI-INF/blueprint/rpcbenchmark.xml b/benchmark/rpcbenchmark/src/main/resources/OSGI-INF/blueprint/rpcbenchmark.xml
deleted file mode 100644 (file)
index 3c666e4..0000000
+++ /dev/null
@@ -1,27 +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">
-
-  <bean id="globalServer" class="rpcbenchmark.impl.GlobalBindingRTCServer"/>
-
-  <reference id="rpcProviderService" interface="org.opendaylight.mdsal.binding.api.RpcProviderService"/>
-  <reference id="rpcConsumerRegistry" interface="org.opendaylight.mdsal.binding.api.RpcConsumerRegistry"/>
-
-  <bean id="provider" class="rpcbenchmark.impl.RpcbenchmarkProvider"
-          init-method="init" destroy-method="close">
-    <argument ref="rpcProviderService"/>
-    <argument ref="rpcConsumerRegistry"/>
-    <argument ref="globalServer"/>
-  </bean>
-
-  <odl:rpc-implementation ref="globalServer"/>
-  <odl:rpc-implementation ref="provider"/>
-</blueprint>