Ditch blueprint from drop-test-karaf 77/110177/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 7 Feb 2024 10:23:02 +0000 (11:23 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 7 Feb 2024 11:02:09 +0000 (12:02 +0100)
Use simple OSGi injection instead of a blueprint. This forces a refactor
in test-common, which ends up working much more nicely than before.

JIRA: OPNFLWPLUG-1112
Change-Id: I9496c9fd41bb6919e205ab354a35c03e1206b697
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
12 files changed:
drop-test-karaf/pom.xml
drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ClearDropStatsCommandProvider.java
drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsCommandProvider.java
drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsRpcCommandProvider.java
drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropTestProviderImpl.java [deleted file]
drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ShowDropStatsCommandProvider.java
drop-test-karaf/src/main/resources/OSGI-INF/blueprint/drop-test-karaf.xml [deleted file]
test-common/pom.xml
test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestCommiter.java
test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestDsProvider.java [deleted file]
test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcProvider.java [deleted file]
test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcSender.java

index c4f36b018b09ae517d619bb34d35ef003591d95c..b85c450abc202eb5bd2f26eaa213766f8aa8b08c 100644 (file)
@@ -7,25 +7,22 @@
         <version>0.18.0-SNAPSHOT</version>
         <relativePath>../parent</relativePath>
     </parent>
+
     <artifactId>drop-test-karaf</artifactId>
     <packaging>bundle</packaging>
-    <scm>
-        <connection>scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git</connection>
-        <developerConnection>scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git</developerConnection>
-    </scm>
+
     <dependencies>
         <dependency>
-            <groupId>org.opendaylight.openflowplugin</groupId>
-            <artifactId>test-common</artifactId>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
         </dependency>
         <dependency>
-            <groupId>org.opendaylight.mdsal</groupId>
-            <artifactId>mdsal-binding-api</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.opendaylight.openflowplugin.model</groupId>
-            <artifactId>model-flow-service</artifactId>
+            <groupId>org.opendaylight.openflowplugin</groupId>
+            <artifactId>test-common</artifactId>
         </dependency>
+
+        <!-- FIXME: migrate karaf commands and adjust these two dependencies -->
         <dependency>
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.console</artifactId>
         </dependency>
     </dependencies>
 
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.felix</groupId>
-                <artifactId>maven-bundle-plugin</artifactId>
-                <extensions>true</extensions>
-            </plugin>
-        </plugins>
-    </build>
+    <scm>
+        <connection>scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git</connection>
+        <developerConnection>scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git</developerConnection>
+    </scm>
 </project>
index 3691b227d99308a82192b3a507e9c951c7b7612a..6c785ba1e3001ecd9573ec5498b3078ffda3f975 100644 (file)
@@ -7,27 +7,26 @@
  */
 package org.opendaylight.openflowplugin.droptestkaraf;
 
-import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.openflowplugin.testcommon.DropTestDsProvider;
-import org.opendaylight.openflowplugin.testcommon.DropTestRpcProvider;
+import org.opendaylight.openflowplugin.testcommon.DropTestCommiter;
+import org.opendaylight.openflowplugin.testcommon.DropTestRpcSender;
 
 @Command(scope = "drop-test", name = "clearDropStats", description = "Clear drop statistics.")
 public class ClearDropStatsCommandProvider extends OsgiCommandSupport {
+    @Reference
+    DropTestRpcSender rpcProvider;
+    @Reference
+    DropTestCommiter dsProvider;
 
     @Override
     protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final DropTestRpcProvider rpcProvider = DropTestProviderImpl.getDropRpcProvider();
-        final DropTestDsProvider provider = DropTestProviderImpl.getDropDsProvider();
-
+        final var out = session.getConsole();
         out.println("Clearing drop statistics... ");
         rpcProvider.clearStats();
-        provider.clearStats();
+        dsProvider.clearStats();
         out.println("Done.");
-
         return null;
     }
-
 }
index b05eaade40f068755fbda44a93d928590070434d..fd9d1e31dae2e7007d4b41445da31e5380a9af1f 100644 (file)
@@ -5,40 +5,37 @@
  * 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 org.opendaylight.openflowplugin.droptestkaraf;
 
-import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.openflowplugin.testcommon.DropTestDsProvider;
+import org.opendaylight.openflowplugin.testcommon.DropTestCommiter;
 
 @Command(scope = "drop-test", name = "dropAllPackets",
          description = "drop packet responder involving dataStore and FRM")
 public class DropAllPacketsCommandProvider extends OsgiCommandSupport {
+    @Reference
+    DropTestCommiter provider;
 
     @Argument(index = 0, name = "on-off",
             description = "target state of drop responder",
             required = true, multiValued = false)
     String targetStateArg;
 
-
     @Override
     protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final DropTestDsProvider provider = DropTestProviderImpl.getDropDsProvider();
+        final var out = session.getConsole();
 
         if ("on".equalsIgnoreCase(targetStateArg)) {
-            if (! provider.isActive()) {
-                provider.start();
+            if (provider.start()) {
                 out.println("DropAllFlows transitions to on");
             } else {
                 out.println("DropAllFlows is already on");
             }
         } else if ("off".equalsIgnoreCase(targetStateArg)) {
-            if (provider.isActive()) {
-                provider.close();
+            if (provider.stop()) {
                 out.println("DropAllFlows transitions to off");
             } else {
                 out.println("DropAllFlows is already off");
index 72b3c08bec45f7fde05e3c593b101054b828955a..997878fb69db7d50be4b7363853443d6c21a4292 100644 (file)
@@ -5,18 +5,20 @@
  * 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 org.opendaylight.openflowplugin.droptestkaraf;
 
 import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.openflowplugin.testcommon.DropTestRpcProvider;
+import org.opendaylight.openflowplugin.testcommon.DropTestRpcSender;
 
 @Command(scope = "drop-test", name = "dropAllPacketsRpc",
          description = "drop packet responder involving SalFlowService")
 public class DropAllPacketsRpcCommandProvider extends OsgiCommandSupport {
+    @Reference
+    DropTestRpcSender provider;
 
     @Argument(index = 0, name = "on-off",
             description = "target state of drop responder",
@@ -26,18 +28,14 @@ public class DropAllPacketsRpcCommandProvider extends OsgiCommandSupport {
     @Override
     protected Object doExecute() {
         PrintStream out = session.getConsole();
-        final DropTestRpcProvider provider = DropTestProviderImpl.getDropRpcProvider();
-
         if ("on".equalsIgnoreCase(targetStateArg)) {
-            if (! provider.isActive()) {
-                provider.start();
+            if (provider.start()) {
                 out.println("DropAllFlows transitions to on");
             } else {
                 out.println("DropAllFlows is already on");
             }
         } else if ("off".equalsIgnoreCase(targetStateArg)) {
-            if (provider.isActive()) {
-                provider.close();
+            if (provider.stop()) {
                 out.println("DropAllFlows transitions to off");
             } else {
                 out.println("DropAllFlows is already off");
diff --git a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropTestProviderImpl.java b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropTestProviderImpl.java
deleted file mode 100644 (file)
index a22eb95..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (c) 2015 Cisco Systems, 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
- */
-package org.opendaylight.openflowplugin.droptestkaraf;
-
-import static java.util.Objects.requireNonNull;
-
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.openflowplugin.testcommon.DropTestDsProvider;
-import org.opendaylight.openflowplugin.testcommon.DropTestRpcProvider;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 26.4.2015.
- */
-public class DropTestProviderImpl implements AutoCloseable {
-
-    private static final Logger LOG = LoggerFactory.getLogger(DropTestProviderImpl.class);
-
-    private static DropTestDsProvider dropDsProvider = new DropTestDsProvider();
-
-    private static DropTestRpcProvider dropRpcProvider = new DropTestRpcProvider();
-
-    public DropTestProviderImpl(final DataBroker dataBroker,
-                                final NotificationService notificationService,
-                                final SalFlowService salFlowService) {
-        requireNonNull(dataBroker, "Data broker can't be empty");
-        requireNonNull(notificationService, "NotificationProviderService can't be empty");
-        requireNonNull(salFlowService, "SalFlowService can't be empty");
-
-        LOG.debug("Activator DropAllPack INIT");
-
-        dropDsProvider.setDataService(dataBroker);
-        dropDsProvider.setNotificationService(notificationService);
-
-        dropRpcProvider.setNotificationService(notificationService);
-        dropRpcProvider.setFlowService(salFlowService);
-
-        LOG.debug("Activator DropAllPack END");
-    }
-
-    public static DropTestDsProvider getDropDsProvider() {
-        return dropDsProvider;
-    }
-
-    public static DropTestRpcProvider getDropRpcProvider() {
-        return dropRpcProvider;
-    }
-
-    @Override
-    public void close() {
-        dropDsProvider.close();
-        dropRpcProvider.close();
-    }
-}
index 3d025745dcc1d56949cf7f57df45d7be0ca8de79..54abddfb5d42785f7aa19ce34f14aea07f1eaf01 100644 (file)
@@ -7,25 +7,24 @@
  */
 package org.opendaylight.openflowplugin.droptestkaraf;
 
-import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.openflowplugin.testcommon.DropTestDsProvider;
-import org.opendaylight.openflowplugin.testcommon.DropTestRpcProvider;
+import org.opendaylight.openflowplugin.testcommon.DropTestCommiter;
+import org.opendaylight.openflowplugin.testcommon.DropTestRpcSender;
 
 @Command(scope = "drop-test", name = "showDropStats", description = "Show drop statistics.")
 public class ShowDropStatsCommandProvider extends OsgiCommandSupport {
+    @Reference
+    DropTestRpcSender rpcProvider;
+    @Reference
+    DropTestCommiter dsProvider;
 
     @Override
     protected Object doExecute() {
-        PrintStream out = session.getConsole();
-        final DropTestRpcProvider rpcProvider = DropTestProviderImpl.getDropRpcProvider();
-        final DropTestDsProvider provider = DropTestProviderImpl.getDropDsProvider();
-
+        var out = session.getConsole();
         out.format("RPC Test Statistics: %s%n", rpcProvider.getStats().toString());
-        out.format("FRM Test Statistics: %s%n", provider.getStats().toString());
-
+        out.format("FRM Test Statistics: %s%n", dsProvider.getStats().toString());
         return null;
     }
-
 }
diff --git a/drop-test-karaf/src/main/resources/OSGI-INF/blueprint/drop-test-karaf.xml b/drop-test-karaf/src/main/resources/OSGI-INF/blueprint/drop-test-karaf.xml
deleted file mode 100644 (file)
index 8b36bdb..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<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="dataBroker" interface="org.opendaylight.mdsal.binding.api.DataBroker"/>
-  <reference id="notificationService" interface="org.opendaylight.mdsal.binding.api.NotificationService"/>
-
-  <odl:rpc-service id="flowService"
-      interface="org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService"/>
-
-  <bean id="dropTestProvider" class="org.opendaylight.openflowplugin.droptestkaraf.DropTestProviderImpl">
-    <argument ref="dataBroker"/>
-    <argument ref="notificationService"/>
-    <argument ref="flowService"/>
-  </bean>
-</blueprint>
index 7efe8336d8bebee2aacb5898da590295322d0370..b23e754486b556c92d5323014815589a5f76146b 100644 (file)
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.annotation</groupId>
+            <artifactId>jakarta.annotation-api</artifactId>
+            <optional>true</optional>
+        </dependency>
         <dependency>
             <groupId>org.opendaylight.mdsal</groupId>
             <artifactId>mdsal-binding-api</artifactId>
         </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin.model</groupId>
-            <artifactId>model-flow-service</artifactId>
+            <artifactId>model-flow-base</artifactId>
         </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin.model</groupId>
-            <artifactId>model-flow-base</artifactId>
+            <artifactId>model-inventory</artifactId>
         </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin.model</groupId>
-            <artifactId>model-inventory</artifactId>
+            <artifactId>model-flow-service</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.opendaylight.openflowplugin</groupId>
-            <artifactId>openflowplugin-common</artifactId>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
         </dependency>
     </dependencies>
 
index beb60d73855f42e5e00092ca8f8e150c0469e93e..08d9f16b9abbe27f7ccfbb88b38fe6733cc7b10f 100644 (file)
@@ -7,10 +7,14 @@
  */
 package org.opendaylight.openflowplugin.testcommon;
 
+import static java.util.Objects.requireNonNull;
+
 import java.util.concurrent.atomic.AtomicLong;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -29,17 +33,22 @@ import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.Uint64;
 import org.opendaylight.yangtools.yang.common.Uint8;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
- * device using dataStore strategy (FRM involved).
+ * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to device using dataStore
+ * strategy (FRM involved).
  */
-public class DropTestCommiter extends AbstractDropTest {
+@Singleton
+@Component(service = DropTestCommiter.class, immediate = true)
+public final class DropTestCommiter extends AbstractDropTest {
     private static final Logger LOG = LoggerFactory.getLogger(DropTestCommiter.class);
     private static final TableKey ZERO_TABLE = new TableKey(Uint8.ZERO);
-    private static final AtomicLong ID_COUNTER = new AtomicLong();
     private static final ThreadLocal<FlowBuilder> BUILDER = ThreadLocal.withInitial(() -> {
         final var cookie = new FlowCookie(Uint64.TEN);
         return new FlowBuilder()
@@ -53,19 +62,56 @@ public class DropTestCommiter extends AbstractDropTest {
             .setFlags(new FlowModFlags(false, false, false, false, false));
     });
 
-    private NotificationService notificationService = null;
-    private Registration notificationRegistration = null;
-    private DataBroker dataService = null;
+    private final AtomicLong idCounter = new AtomicLong();
+    private final DataBroker dataBroker;
+    private final NotificationService notificationService;
+
+    private Registration reg = null;
+
+    @Inject
+    @Activate
+    public DropTestCommiter(@Reference final DataBroker dataBroker,
+            @Reference final NotificationService notificationService) {
+        this.dataBroker = requireNonNull(dataBroker);
+        this.notificationService = requireNonNull(notificationService);
+    }
+
+    @PreDestroy
+    @Deactivate
+    @Override
+    public void close() {
+        stop();
+        super.close();
+        LOG.debug("DropTestProvider terminated");
+    }
 
     /**
-     * start listening on packetIn.
+     * Start listening on packetIn.
+     *
+     * @return {@code false} if already started
      */
-    public void start() {
-        notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
+    public synchronized boolean start() {
+        if (reg != null) {
+            return false;
+        }
+        reg = notificationService.registerListener(PacketReceived.class, this);
+        LOG.debug("DropTestProvider started");
+        return true;
     }
 
-    public void setDataService(final DataBroker dataService) {
-        this.dataService = dataService;
+    /**
+     * Stop listening on packetIn.
+     *
+     * @return {@code false} if already stopped
+     */
+    public synchronized boolean stop() {
+        if (reg == null) {
+            return false;
+        }
+        reg.close();
+        reg = null;
+        LOG.debug("DropTestProvider stopped");
+        return true;
     }
 
     @Override
@@ -76,10 +122,10 @@ public class DropTestCommiter extends AbstractDropTest {
         final FlowBuilder fb = BUILDER.get();
         fb.setMatch(match);
         fb.setInstructions(instructions);
-        fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + ID_COUNTER.getAndIncrement()));
+        fb.setId(new FlowId(String.valueOf(fb.hashCode()) + "." + idCounter.getAndIncrement()));
 
         // Construct the flow instance id
-        final InstanceIdentifier<Flow> flowInstanceId = node.builder()
+        final var flowInstanceId = node.builder()
                 // That is flow capable, only FlowCapableNodes have tables
                 .augmentation(FlowCapableNode.class)
                 // In the table identified by TableKey
@@ -88,27 +134,12 @@ public class DropTestCommiter extends AbstractDropTest {
                 .child(Flow.class, new FlowKey(fb.getId()))
                 .build();
 
-        final Flow flow = fb.build();
-        final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
+        final var flow = fb.build();
+        final var transaction = dataBroker.newReadWriteTransaction();
 
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("onPacketReceived - About to write flow {}", flow);
-        }
+        LOG.debug("onPacketReceived - About to write flow {}", flow);
         transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
         transaction.commit();
         LOG.debug("onPacketReceived - About to write flow commited");
     }
-
-    @Override
-    public void close() {
-        super.close();
-        LOG.debug("DropTestProvider stopped.");
-        if (notificationRegistration != null) {
-            notificationRegistration.close();
-        }
-    }
-
-    public void setNotificationService(final NotificationService notificationService) {
-        this.notificationService = notificationService;
-    }
 }
diff --git a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestDsProvider.java b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestDsProvider.java
deleted file mode 100644 (file)
index 5b87fc4..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, 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
- */
-package org.opendaylight.openflowplugin.testcommon;
-
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Provides activation and deactivation of drop responder service - responds on packetIn.
- */
-public class DropTestDsProvider implements AutoCloseable {
-    private static final Logger LOG = LoggerFactory.getLogger(DropTestDsProvider.class);
-
-    private DataBroker dataService;
-    private NotificationService notificationService;
-    private final DropTestCommiter commiter = new DropTestCommiter();
-    private boolean active = false;
-
-    /**
-     * Returns the message counts.
-     */
-    public DropTestStats getStats() {
-        return commiter.getStats();
-    }
-
-    /**
-     * Reset message counts.
-     */
-    public void clearStats() {
-        commiter.clearStats();
-    }
-
-    public void setDataService(final DataBroker dataService) {
-        this.dataService = dataService;
-    }
-
-    public void setNotificationService(final NotificationService notificationService) {
-        this.notificationService = notificationService;
-    }
-
-    /**
-     * Activates the drop responder.
-     */
-    public void start() {
-        commiter.setDataService(dataService);
-        commiter.setNotificationService(notificationService);
-        commiter.start();
-        active = true;
-        LOG.debug("DropTestProvider Started.");
-    }
-
-    @Override
-    public void close() {
-        LOG.debug("DropTestProvider stopped.");
-        if (commiter != null) {
-            commiter.close();
-            active = false;
-        }
-    }
-
-    /**
-     * Returns the active state.
-     */
-    public boolean isActive() {
-        return active;
-    }
-}
diff --git a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcProvider.java b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcProvider.java
deleted file mode 100644 (file)
index 58d3d27..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, 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
- */
-package org.opendaylight.openflowplugin.testcommon;
-
-import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Provides activation and deactivation of drop responder service - responds on packetIn.
- */
-public class DropTestRpcProvider implements AutoCloseable {
-    private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcProvider.class);
-
-    private SalFlowService flowService;
-    private NotificationService notificationService;
-    private final DropTestRpcSender commiter = new DropTestRpcSender();
-    private boolean active = false;
-
-    public void setFlowService(final SalFlowService flowService) {
-        this.flowService = flowService;
-    }
-
-    public void setNotificationService(final NotificationService notificationService) {
-        this.notificationService = notificationService;
-    }
-
-    /**
-     * Activates drop responder.
-     */
-    public void start() {
-        commiter.setFlowService(flowService);
-        commiter.setNotificationService(notificationService);
-        commiter.start();
-        active = true;
-        LOG.debug("DropTestProvider Started.");
-    }
-
-    /**
-     * Returns the message counts.
-     */
-    public DropTestStats getStats() {
-        if (this.commiter != null) {
-            return commiter.getStats();
-        } else {
-            return new DropTestStats("Not initialized yet.");
-        }
-    }
-
-    /**
-     * Reset message counts.
-     */
-    public void clearStats() {
-        if (commiter != null) {
-            commiter.clearStats();
-        }
-    }
-
-    @Override
-    public void close() {
-        LOG.debug("DropTestProvider stopped.");
-        if (commiter != null) {
-            commiter.close();
-            active = false;
-        }
-    }
-
-    /**
-     * Returns the active state.
-     */
-    public boolean isActive() {
-        return active;
-    }
-}
index 8906f8bf38a89a18b230fd68b404aac519e4f480..0ce20c281a39d138049c455ef2f921aa0942eb9d 100644 (file)
@@ -7,12 +7,17 @@
  */
 package org.opendaylight.openflowplugin.testcommon;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
+import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
@@ -27,6 +32,10 @@ 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.Uint64;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -34,7 +43,9 @@ import org.slf4j.LoggerFactory;
  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
  * device using {@link SalFlowService} strategy.
  */
-public class DropTestRpcSender extends AbstractDropTest {
+@Singleton
+@Component(service = DropTestRpcSender.class, immediate = true)
+public final class DropTestRpcSender extends AbstractDropTest {
     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
         final var cookie = new FlowCookie(Uint64.TEN);
@@ -49,19 +60,45 @@ public class DropTestRpcSender extends AbstractDropTest {
             .setFlags(new FlowModFlags(false, false, false, false, false));
     });
 
-    private NotificationService notificationService = null;
-    private Registration notificationRegistration = null;
-    private SalFlowService flowService = null;
+    private final NotificationService notificationService;
+    private final AddFlow addFlow;
+
+    private Registration reg = null;
 
-    public void setFlowService(final SalFlowService flowService) {
-        this.flowService = flowService;
+    @Inject
+    @Activate
+    public DropTestRpcSender(@Reference final NotificationService notificationService,
+            @Reference final RpcConsumerRegistry rpcService) {
+        this.notificationService = requireNonNull(notificationService);
+        addFlow = rpcService.getRpc(AddFlow.class);
+    }
+
+    @PreDestroy
+    @Deactivate
+    @Override
+    public void close() {
+        stop();
+        super.close();
+        LOG.debug("DropTestProvider terminated");
     }
 
-    /**
-     * Start listening on packetIn.
-     */
-    public void start() {
-        notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
+    public synchronized boolean start() {
+        if (reg != null) {
+            return false;
+        }
+        reg = notificationService.registerListener(PacketReceived.class, this);
+        LOG.debug("DropTestProvider started");
+        return true;
+    }
+
+    public synchronized boolean stop() {
+        if (reg == null) {
+            return false;
+        }
+        reg.close();
+        reg = null;
+        LOG.debug("DropTestProvider stopped");
+        return true;
     }
 
     @Override
@@ -78,10 +115,9 @@ public class DropTestRpcSender extends AbstractDropTest {
         fb.setNode(new NodeRef(node));
 
         // Add flow
-        final AddFlowInput flow = fb.build();
+        final var flow = fb.build();
         LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
-        ListenableFuture<RpcResult<AddFlowOutput>> result = flowService.addFlow(flow);
-        Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
+        Futures.addCallback(addFlow.invoke(flow), new FutureCallback<RpcResult<AddFlowOutput>>() {
             @Override
             public void onSuccess(final RpcResult<AddFlowOutput> result) {
                 countFutureSuccess();
@@ -93,17 +129,4 @@ public class DropTestRpcSender extends AbstractDropTest {
             }
         }, MoreExecutors.directExecutor());
     }
-
-    public void setNotificationService(final NotificationService notificationService) {
-        this.notificationService = notificationService;
-    }
-
-    @Override
-    public void close() {
-        super.close();
-        LOG.debug("DropTestProvider stopped.");
-        if (notificationRegistration != null) {
-            notificationRegistration.close();
-        }
-    }
 }