Migrate test-provider to CompositeListener 78/102678/6
authorSangwook Ha <sangwook.ha@verizon.com>
Fri, 18 Nov 2022 00:05:40 +0000 (16:05 -0800)
committerSangwook Ha <sangwook.ha@verizon.com>
Fri, 18 Nov 2022 04:23:45 +0000 (20:23 -0800)
Do not use generated Listener interface and prefer a CompositeListeners,
which allow more flexible dispatch.

Change-Id: I7848c91eb80900ed1bdde70f2f75c13325a41680
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
Signed-off-by: Sangwook Ha <sangwook.ha@verizon.com>
test-provider/pom.xml
test-provider/src/main/java/org/opendaylight/openflowplugin/test/FlowEventListenerLoggingImpl.java
test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestCommandProvider.java
test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestNodeConnectorNotification.java
test-provider/src/main/java/org/opendaylight/openflowplugin/test/OpenflowpluginTestTopologyNotification.java

index 289411d822273dcbbf39f72f2115f0b5ac3d33d0..be2d5195894244f2ff19c55336fc617b9832abd7 100644 (file)
             <groupId>org.opendaylight.mdsal</groupId>
             <artifactId>mdsal-binding-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.mdsal</groupId>
+            <artifactId>yang-binding</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.opendaylight.openflowplugin.model</groupId>
             <artifactId>model-flow-service</artifactId>
index e518647343d4b81a2120192825ebe2a66416c22a..1f382cea44b6529cb320943b4aeb82f4d1beec34 100644 (file)
@@ -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());
+            })));
     }
 }
index f9675f5dab5837d1179a6f2cecd0dc710da0e1be..573e1dcb4cc4c59a984355ac3d6bbb42ab425229 100644 (file)
@@ -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);
             }
         }
 
index 0a47edfb793b8a6b18fda994a450a8b8e9e22460..5f110874bfc0689032c417dd193d0535943f0150 100644 (file)
@@ -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> nodeUpdated = new ArrayList<>();
         List<NodeRemoved> nodeRemoved = new ArrayList<>();
         List<NodeConnectorUpdated> nodeConnectorUpdated = new ArrayList<>();
         List<NodeConnectorRemoved> 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);
+                })));
         }
     }
 }
index d5f3d6ca4a197b832b079fdc782fbda755b94690..81532f99845da5ec880b489e0cf927f922b10cfa 100644 (file)
@@ -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("-------------------------------------------");
     }
 }