From cb45881070339db01cdaf057ab0a672d2e9054eb Mon Sep 17 00:00:00 2001 From: Sangwook Ha Date: Thu, 17 Nov 2022 16:05:40 -0800 Subject: [PATCH] Migrate test-provider to CompositeListener Do not use generated Listener interface and prefer a CompositeListeners, which allow more flexible dispatch. Change-Id: I7848c91eb80900ed1bdde70f2f75c13325a41680 Signed-off-by: Robert Varga Signed-off-by: Sangwook Ha --- test-provider/pom.xml | 4 + .../test/FlowEventListenerLoggingImpl.java | 77 ++++++++----------- .../OpenflowpluginTestCommandProvider.java | 25 +++--- ...owpluginTestNodeConnectorNotification.java | 61 +++++++-------- ...penflowpluginTestTopologyNotification.java | 55 ++++--------- 5 files changed, 90 insertions(+), 132 deletions(-) diff --git a/test-provider/pom.xml b/test-provider/pom.xml index 289411d822..be2d519589 100644 --- a/test-provider/pom.xml +++ b/test-provider/pom.xml @@ -22,6 +22,10 @@ org.opendaylight.mdsal mdsal-binding-api + + org.opendaylight.mdsal + yang-binding + org.opendaylight.openflowplugin.model model-flow-service diff --git a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/FlowEventListenerLoggingImpl.java b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/FlowEventListenerLoggingImpl.java index e518647343..1f382cea44 100644 --- a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/FlowEventListenerLoggingImpl.java +++ b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/FlowEventListenerLoggingImpl.java @@ -7,12 +7,13 @@ */ package org.opendaylight.openflowplugin.test; +import java.util.Set; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,51 +21,39 @@ import org.slf4j.LoggerFactory; /** * Dummy implementation flushing events into log. */ -public class FlowEventListenerLoggingImpl implements SalFlowListener { +@Deprecated +public final class FlowEventListenerLoggingImpl { private static final Logger LOG = LoggerFactory.getLogger(FlowEventListenerLoggingImpl.class); - @Override - @Deprecated - public void onFlowAdded(final FlowAdded notification) { - LOG.info("flow to be added {}", notification.toString()); - LOG.info("added flow Xid {}", notification.getTransactionId().getValue()); + private FlowEventListenerLoggingImpl() { + // Hidden on purpose } - @Override - @Deprecated - public void onFlowRemoved(final FlowRemoved notification) { - LOG.debug("removed flow {}", notification.toString()); - LOG.debug("remove flow Xid {}", notification.getTransactionId().getValue()); - } - - @Override - @Deprecated - public void onFlowUpdated(final FlowUpdated notification) { - LOG.debug("updated flow {}", notification.toString()); - LOG.debug("updated flow Xid {}", notification.getTransactionId().getValue()); - } - - @Override - @Deprecated - public void onNodeErrorNotification(final NodeErrorNotification notification) { - //commenting as we have a NodeErrorListener - /* LOG.error("Error notification flow Xid........................." - + notification.getTransactionId().getValue()); - LOG.debug("notification Begin-Transaction:" - + notification.getTransactionUri() - + "-----------------------------------------------------------------------------------"); - */ - } - - @Override - @Deprecated - public void onNodeExperimenterErrorNotification(final NodeExperimenterErrorNotification notification) { - // TODO Auto-generated method stub - } - - @Override - @Deprecated - public void onSwitchFlowRemoved(final SwitchFlowRemoved notification) { - LOG.debug("Switch flow removed : Cookies {}", notification.getCookie().toString()); + static CompositeListener newListener() { + return new CompositeListener(Set.of( + new Component<>(FlowAdded.class, notification -> { + LOG.info("flow to be added {}", notification); + LOG.info("added flow Xid {}", notification.getTransactionId().getValue()); + }), + new Component<>(FlowRemoved.class, notification -> { + LOG.debug("removed flow {}", notification); + LOG.debug("remove flow Xid {}", notification.getTransactionId().getValue()); + }), + new Component<>(FlowUpdated.class, notification -> { + LOG.debug("updated flow {}", notification); + LOG.debug("updated flow Xid {}", notification.getTransactionId().getValue()); + }), + new Component<>(NodeErrorNotification.class, notification -> { + //commenting as we have a NodeErrorListener + /* LOG.error("Error notification flow Xid........................." + + notification.getTransactionId().getValue()); + LOG.debug("notification Begin-Transaction:" + + notification.getTransactionUri() + + "-----------------------------------------------------------------------------------"); + */ + }), + new Component<>(SwitchFlowRemoved.class, notification -> { + LOG.debug("Switch flow removed : Cookies {}", notification.getCookie().toString()); + }))); } } diff --git a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestCommandProvider.java b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestCommandProvider.java index f9675f5dab..573e1dcb4c 100644 --- a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestCommandProvider.java +++ b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestCommandProvider.java @@ -109,7 +109,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues; @@ -181,7 +180,7 @@ public class OpenflowpluginTestCommandProvider implements CommandProvider { private static final String IPV4_PREFIX = "10.0.0.1/24"; private static final String DEST_MAC_ADDRESS = "ff:ff:ff:ff:ff:ff"; private static final String SRC_MAC_ADDRESS = "00:00:00:00:23:ae"; - private final SalFlowListener flowEventListener = new FlowEventListenerLoggingImpl(); + private final NotificationService notificationService; public OpenflowpluginTestCommandProvider(final DataBroker dataBroker, final NotificationService notificationService, @@ -193,7 +192,8 @@ public class OpenflowpluginTestCommandProvider implements CommandProvider { public void init() { // For switch events - notificationService.registerNotificationListener(flowEventListener); + notificationService.registerCompositeListener(FlowEventListenerLoggingImpl.newListener()); + ctx.registerService(CommandProvider.class.getName(), this, null); createTestFlow(createTestNode(null), null, null); } @@ -2689,10 +2689,10 @@ public class OpenflowpluginTestCommandProvider implements CommandProvider { TestFlowThread(final int numberOfSwtiches, final int numberOfFlows, final CommandInterpreter ci, final int threadNumber, final int tableID) { - this.numberOfSwitches = numberOfSwtiches; + numberOfSwitches = numberOfSwtiches; this.numberOfFlows = numberOfFlows; this.ci = ci; - this.theadNumber = threadNumber; + theadNumber = threadNumber; this.tableID = tableID; } @@ -2708,27 +2708,26 @@ public class OpenflowpluginTestCommandProvider implements CommandProvider { FlowBuilder tf; //String tableId = "0"; - ci.println("New Thread started with id: ID_" - + this.theadNumber); + ci.println("New Thread started with id: ID_" + theadNumber); int totalNumberOfFlows = 0; final long startTime = System.currentTimeMillis(); - for (int i = 1; i <= this.numberOfSwitches; i++) { + for (int i = 1; i <= numberOfSwitches; i++) { dataPath = "openflow:" + i; tn = createTestNode(dataPath); - for (int flow2 = 1; flow2 <= this.numberOfFlows; flow2++) { - tf = createTestFlowPerfTest("f1", "" + this.tableID, flow2); - writeFlow(this.ci, tf, tn); + for (int flow2 = 1; flow2 <= numberOfFlows; flow2++) { + tf = createTestFlowPerfTest("f1", "" + tableID, flow2); + writeFlow(ci, tf, tn); totalNumberOfFlows++; } } final long endTime = System.currentTimeMillis(); final long timeInSeconds = Math.round((endTime - startTime) / 1000.0F); if (timeInSeconds > 0) { - ci.println("Total flows added in Thread:" + this.theadNumber + ": Flows/Sec::" + ci.println("Total flows added in Thread:" + theadNumber + ": Flows/Sec::" + Math.round((float)totalNumberOfFlows / timeInSeconds)); } else { - ci.println("Total flows added in Thread:" + this.theadNumber + ": Flows/Sec::" + totalNumberOfFlows); + ci.println("Total flows added in Thread:" + theadNumber + ": Flows/Sec::" + totalNumberOfFlows); } } diff --git a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestNodeConnectorNotification.java b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestNodeConnectorNotification.java index 0a47edfb79..5f110874bf 100644 --- a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestNodeConnectorNotification.java +++ b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestNodeConnectorNotification.java @@ -9,12 +9,14 @@ package org.opendaylight.openflowplugin.test; import java.util.ArrayList; import java.util.List; +import java.util.Set; import org.opendaylight.mdsal.binding.api.NotificationService; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,46 +33,37 @@ public class OpenflowpluginTestNodeConnectorNotification { public void init() { // For switch events - notificationService.registerNotificationListener(portEventListener); + notificationService.registerCompositeListener(portEventListener.toListener()); } - private static final class PortEventListener implements OpendaylightInventoryListener { - + private static final class PortEventListener { List nodeUpdated = new ArrayList<>(); List nodeRemoved = new ArrayList<>(); List nodeConnectorUpdated = new ArrayList<>(); List nodeConnectorRemoved = new ArrayList<>(); - @Override - @Deprecated - public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) { - LOG.debug("NodeConnectorRemoved Notification"); - LOG.debug("NodeConnectorRef {}", notification.getNodeConnectorRef()); - nodeConnectorRemoved.add(notification); - } - - @Override - @Deprecated - public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) { - LOG.debug("NodeConnectorUpdated Notification"); - LOG.debug("NodeConnectorRef {}", notification.getNodeConnectorRef()); - nodeConnectorUpdated.add(notification); - } - - @Override - @Deprecated - public void onNodeRemoved(final NodeRemoved notification) { - LOG.debug("NodeRemoved Notification"); - LOG.debug("NodeRef {}", notification.getNodeRef()); - nodeRemoved.add(notification); - } - - @Override - @Deprecated - public void onNodeUpdated(final NodeUpdated notification) { - LOG.debug("NodeUpdated Notification"); - LOG.debug("NodeRef {}", notification.getNodeRef()); - nodeUpdated.add(notification); + CompositeListener toListener() { + return new CompositeListener(Set.of( + new Component<>(NodeConnectorRemoved.class, notification -> { + LOG.debug("NodeConnectorRemoved Notification"); + LOG.debug("NodeConnectorRef {}", notification.getNodeConnectorRef()); + nodeConnectorRemoved.add(notification); + }), + new Component<>(NodeConnectorUpdated.class, notification -> { + LOG.debug("NodeConnectorUpdated Notification"); + LOG.debug("NodeConnectorRef {}", notification.getNodeConnectorRef()); + nodeConnectorUpdated.add(notification); + }), + new Component<>(NodeRemoved.class, notification -> { + LOG.debug("NodeRemoved Notification"); + LOG.debug("NodeRef {}", notification.getNodeRef()); + nodeRemoved.add(notification); + }), + new Component<>(NodeUpdated.class, notification -> { + LOG.debug("NodeUpdated Notification"); + LOG.debug("NodeRef {}", notification.getNodeRef()); + nodeUpdated.add(notification); + }))); } } } diff --git a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestTopologyNotification.java b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestTopologyNotification.java index d5f3d6ca4a..81532f9984 100644 --- a/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestTopologyNotification.java +++ b/test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestTopologyNotification.java @@ -8,20 +8,21 @@ package org.opendaylight.openflowplugin.test; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.Set; import org.opendaylight.mdsal.binding.api.NotificationService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener; +import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal; +import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class OpenflowpluginTestTopologyNotification { - private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginTestTopologyNotification.class); - private final TopologyEventListener topologyEventListener = new TopologyEventListener(); private final NotificationService notificationService; public OpenflowpluginTestTopologyNotification(final NotificationService notificationService) { @@ -30,45 +31,17 @@ public class OpenflowpluginTestTopologyNotification { public void init() { // For switch events - notificationService.registerNotificationListener(topologyEventListener); + notificationService.registerCompositeListener(new CompositeListener(Set.of( + new Component<>(LinkDiscovered.class, this::onNotification), + new Component<>(LinkOverutilized.class, this::onNotification), + new Component<>(LinkRemoved.class, this::onNotification), + new Component<>(LinkUtilizationNormal.class, this::onNotification)))); } - private static final class TopologyEventListener implements FlowTopologyDiscoveryListener { - @Override - @Deprecated - @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT") - public void onLinkDiscovered(final LinkDiscovered notification) { - LOG.debug("-------------------------------------------"); - LOG.debug("LinkDiscovered notification ........"); - LOG.debug("-------------------------------------------"); - } - - @Override - @Deprecated - @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT") - public void onLinkOverutilized(final LinkOverutilized notification) { - LOG.debug("-------------------------------------------"); - LOG.debug("LinkOverutilized notification ........"); - LOG.debug("-------------------------------------------"); - } - - @Override - @Deprecated - @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT") - public void onLinkRemoved(final LinkRemoved notification) { - LOG.debug("-------------------------------------------"); - LOG.debug("LinkRemoved notification ........"); - LOG.debug("-------------------------------------------"); - } - - @Override - @Deprecated - @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT") - public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) { - LOG.debug("-------------------------------------------"); - LOG.debug("LinkUtilizationNormal notification ........"); - LOG.debug("-------------------------------------------"); - } - + @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT") + private void onNotification(Notification notification) { + LOG.debug("-------------------------------------------"); + LOG.debug("{} notification ........", notification.getClass().getSimpleName()); + LOG.debug("-------------------------------------------"); } } -- 2.36.6