From: Robert Varga Date: Wed, 7 Feb 2024 10:23:02 +0000 (+0100) Subject: Ditch blueprint from drop-test-karaf X-Git-Tag: release/calcium~54 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F77%2F110177%2F3;p=openflowplugin.git Ditch blueprint from drop-test-karaf 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 --- diff --git a/drop-test-karaf/pom.xml b/drop-test-karaf/pom.xml index c4f36b018b..b85c450abc 100644 --- a/drop-test-karaf/pom.xml +++ b/drop-test-karaf/pom.xml @@ -7,25 +7,22 @@ 0.18.0-SNAPSHOT ../parent + drop-test-karaf bundle - - scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git - scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git - + - org.opendaylight.openflowplugin - test-common + com.guicedee.services + javax.inject + true - org.opendaylight.mdsal - mdsal-binding-api - - - org.opendaylight.openflowplugin.model - model-flow-service + org.opendaylight.openflowplugin + test-common + + org.apache.karaf.shell org.apache.karaf.shell.console @@ -37,13 +34,8 @@ - - - - org.apache.felix - maven-bundle-plugin - true - - - + + scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git + scm:git:ssh://git.opendaylight.org:29418/openflowplugin.git + diff --git a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ClearDropStatsCommandProvider.java b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ClearDropStatsCommandProvider.java index 3691b227d9..6c785ba1e3 100644 --- a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ClearDropStatsCommandProvider.java +++ b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ClearDropStatsCommandProvider.java @@ -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; } - } diff --git a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsCommandProvider.java b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsCommandProvider.java index b05eaade40..fd9d1e31da 100644 --- a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsCommandProvider.java +++ b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsCommandProvider.java @@ -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"); diff --git a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsRpcCommandProvider.java b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsRpcCommandProvider.java index 72b3c08bec..997878fb69 100644 --- a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsRpcCommandProvider.java +++ b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropAllPacketsRpcCommandProvider.java @@ -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 index a22eb95f2a..0000000000 --- a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/DropTestProviderImpl.java +++ /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 <mbobak@cisco.com> 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(); - } -} diff --git a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ShowDropStatsCommandProvider.java b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ShowDropStatsCommandProvider.java index 3d025745dc..54abddfb5d 100644 --- a/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ShowDropStatsCommandProvider.java +++ b/drop-test-karaf/src/main/java/org/opendaylight/openflowplugin/droptestkaraf/ShowDropStatsCommandProvider.java @@ -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 index 8b36bdb3f0..0000000000 --- a/drop-test-karaf/src/main/resources/OSGI-INF/blueprint/drop-test-karaf.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - diff --git a/test-common/pom.xml b/test-common/pom.xml index 7efe8336d8..b23e754486 100644 --- a/test-common/pom.xml +++ b/test-common/pom.xml @@ -20,25 +20,35 @@ com.google.guava guava + + com.guicedee.services + javax.inject + true + + + jakarta.annotation + jakarta.annotation-api + true + org.opendaylight.mdsal mdsal-binding-api org.opendaylight.openflowplugin.model - model-flow-service + model-flow-base org.opendaylight.openflowplugin.model - model-flow-base + model-inventory org.opendaylight.openflowplugin.model - model-inventory + model-flow-service - org.opendaylight.openflowplugin - openflowplugin-common + org.osgi + org.osgi.service.component.annotations diff --git a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestCommiter.java b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestCommiter.java index beb60d7385..08d9f16b9a 100644 --- a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestCommiter.java +++ b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestCommiter.java @@ -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 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 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 index 5b87fc4d8e..0000000000 --- a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestDsProvider.java +++ /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 index 58d3d27be3..0000000000 --- a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcProvider.java +++ /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; - } -} diff --git a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcSender.java b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcSender.java index 8906f8bf38..0ce20c281a 100644 --- a/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcSender.java +++ b/test-common/src/main/java/org/opendaylight/openflowplugin/testcommon/DropTestRpcSender.java @@ -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 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> result = flowService.addFlow(flow); - Futures.addCallback(result, new FutureCallback>() { + Futures.addCallback(addFlow.invoke(flow), new FutureCallback>() { @Override public void onSuccess(final RpcResult 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(); - } - } }