adding unit tests for decoder framework 40/7840/1
authorAmit Mandke <ammandke@cisco.com>
Mon, 9 Jun 2014 22:56:58 +0000 (15:56 -0700)
committerAmit Mandke <ammandke@cisco.com>
Mon, 9 Jun 2014 22:57:50 +0000 (15:57 -0700)
Change-Id: I923d74c21d242fc0dce58a875136aa7c6ce27bde
Signed-off-by: Amit Mandke <ammandke@cisco.com>
packethandler/implementation/pom.xml
packethandler/implementation/src/main/java/org/opendaylight/l2switch/packethandler/PacketHandlerProvider.java
packethandler/implementation/src/main/java/org/opendaylight/l2switch/packethandler/RawPacketHandler.java
packethandler/implementation/src/main/java/org/opendaylight/l2switch/packethandler/decoders/PacketDecoderService.java
packethandler/implementation/src/main/java/org/opendaylight/l2switch/packethandler/decoders/PacketNotificationRegistry.java
packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/DecoderRegistryTest.java [new file with mode: 0644]
packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/PacketNotificationRegistryTest.java [new file with mode: 0644]

index a07a1336dbd54330e9ff4bdd61bbc130d98ee9cc..6c839ed8262faa2ea275b613b72609069c2cf04f 100644 (file)
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.opendaylight.controller</groupId>
       <artifactId>sal-binding-api</artifactId>
index af45e92bca7ac56353a0e43a720655628d8876af..e14e36126509a69bbfb72d8654245e658e74624d 100644 (file)
@@ -7,16 +7,14 @@
  */
 package org.opendaylight.l2switch.packethandler;
 
-import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareConsumer;
+import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
-import org.opendaylight.controller.sal.binding.api.NotificationService;
 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
 import org.opendaylight.l2switch.packethandler.decoders.DecoderRegistry;
 import org.opendaylight.l2switch.packethandler.decoders.PacketDecoderService;
 import org.opendaylight.l2switch.packethandler.decoders.PacketDecoderServiceImpl;
 import org.opendaylight.l2switch.packethandler.decoders.PacketNotificationRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.slf4j.Logger;
@@ -25,7 +23,7 @@ import org.slf4j.LoggerFactory;
 /**
  * PacketHandlerProvider serves as the Activator for our L2Switch OSGI bundle.
  */
-public class PacketHandlerProvider extends AbstractBindingAwareConsumer
+public class PacketHandlerProvider extends AbstractBindingAwareProvider
     implements AutoCloseable {
 
   private final static Logger _logger = LoggerFactory.getLogger(PacketHandlerProvider.class);
@@ -36,14 +34,14 @@ public class PacketHandlerProvider extends AbstractBindingAwareConsumer
   /**
    * Setup the packet handler.
    *
-   * @param consumerContext The context of the L2Switch.
+   * @param providerContext The context of the L2Switch.
    */
   @Override
-  public void onSessionInitialized(BindingAwareBroker.ConsumerContext consumerContext) {
-    DataBrokerService dataService = consumerContext.<DataBrokerService>getSALService(DataBrokerService.class);
+  public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
+    DataBrokerService dataService = providerContext.<DataBrokerService>getSALService(DataBrokerService.class);
 
     NotificationProviderService notificationProviderService =
-        consumerContext.<NotificationProviderService>getSALService(NotificationProviderService.class);
+        providerContext.<NotificationProviderService>getSALService(NotificationProviderService.class);
 
     DecoderRegistry decoderRegistry = new DecoderRegistry();
 
index 5307a1e046e7663c7f68142e57f0a5ba31aa41a8..748179736fcb66102d9abeeb076d22ea59845e35 100644 (file)
@@ -8,6 +8,8 @@ import org.opendaylight.l2switch.packethandler.decoders.PacketDecoder;
 import org.opendaylight.l2switch.packethandler.decoders.PacketNotificationRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacket;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketGrp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketReceived;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketReceivedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yangtools.yang.binding.Notification;
@@ -15,7 +17,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Created by amitmandke on 6/4/14.
+ * RawPacketHandler subscribes to RawPacket in event. Calls the Ethernet decoder to
  */
 public class RawPacketHandler implements PacketProcessingListener {
   private final static Logger _logger = LoggerFactory.getLogger(RawPacketHandler.class);
@@ -55,6 +57,13 @@ public class RawPacketHandler implements PacketProcessingListener {
       return;
     }
 
+    // publish ethernet notification if listener is subscribed
+    if(packetNotificationRegistry.isListenerSubscribed(EthernetPacketReceived.class)) {
+      EthernetPacketReceivedBuilder ethernetPacketReceivedBuilder = new EthernetPacketReceivedBuilder(ethernetPacket);
+      EthernetPacketReceived ethernetNotification = ethernetPacketReceivedBuilder.build();
+      notificationProviderService.publish(ethernetNotification);
+    }
+
     if(!packetNotificationRegistry.isListenerSubscribed(ethernetPacket.getEthertype())) {
       _logger.info("No Listener is subscribed for {} notification.",ethernetPacket.getEthertype());
       return;
index f9cce6cee3ee6918a5640a2b8107549b87a520f9..f89cafa346ba66ddccf360bcd72b51b9646a7b4d 100644 (file)
@@ -1,11 +1,12 @@
 package org.opendaylight.l2switch.packethandler.decoders;
 
+import org.opendaylight.controller.sal.binding.api.BindingAwareService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.KnownEtherType;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 /**
  * Created by amitmandke on 6/4/14.
  */
-public interface PacketDecoderService {
+public interface PacketDecoderService extends BindingAwareService {
   public <C extends Notification> void registerDecoder(KnownEtherType etherType, PacketDecoder packetDecoder, Class<C> packetReceivedNotificationType);
 }
index 81ab8257cbe962c6333215893a1e43f514f17049..ec56edac9fb8cfe8761df31caadb420ee7a2a33c 100644 (file)
@@ -17,6 +17,7 @@ public class PacketNotificationRegistry implements NotificationProviderService.N
   private Map<KnownEtherType, Class<? extends Notification>> etherTypeToPacketNotificationTypeMap = new EnumMap<KnownEtherType, Class<? extends Notification>>(KnownEtherType.class);
 
   private Map<Class<? extends Notification>, Integer> packetNotificationTypeToListenerCountMap = new HashMap<Class<? extends Notification>, Integer>();
+
   /**
    * Increments the listener count for given notification type.
    *
@@ -26,22 +27,25 @@ public class PacketNotificationRegistry implements NotificationProviderService.N
   public void onNotificationSubscribtion(Class<? extends Notification> aClass) {
     if(aClass == null) return;
 
-    int listenerCount = packetNotificationTypeToListenerCountMap.get(aClass);
-    packetNotificationTypeToListenerCountMap.put(aClass,listenerCount++);
-
+    synchronized(this) {
+      Integer listenerCount = packetNotificationTypeToListenerCountMap.get(aClass);
+      if(listenerCount == null)
+        listenerCount =0;
+      packetNotificationTypeToListenerCountMap.put(aClass, ++listenerCount);
+    }
   }
 
   /**
    * Maintains map of EtherType to notificationType
    *
    * @param notificationType
-   * @param <C>
+   * @param <N>
    */
-  public <C extends Notification> void trackPacketNotificationListener(KnownEtherType etherType, Class<C> notificationType) {
+  public <N extends Notification> void trackPacketNotificationListener(KnownEtherType etherType, Class<N> notificationType) {
     if(etherType == null || notificationType == null) return;
 
-    synchronized(this){
-      etherTypeToPacketNotificationTypeMap.put(etherType,notificationType);
+    synchronized(this) {
+      etherTypeToPacketNotificationTypeMap.put(etherType, notificationType);
     }
 
   }
@@ -55,8 +59,25 @@ public class PacketNotificationRegistry implements NotificationProviderService.N
   public boolean isListenerSubscribed(KnownEtherType etherType) {
     if(etherType == null) return false;
 
-    int listenerCount = packetNotificationTypeToListenerCountMap.get(etherTypeToPacketNotificationTypeMap.get(etherType));
+    Class<?> packetNotification = etherTypeToPacketNotificationTypeMap.get(etherType);
+    if(packetNotification == null) return false;
+
+    return isListenerSubscribed((Class<? extends Notification>) packetNotification);
+  }
+
+  /**
+   * Checks if a listener is subscribed to give the notification type .
+   *
+   * @param packetNotificationType
+   * @return
+   */
+  public boolean isListenerSubscribed(Class<? extends Notification> packetNotificationType) {
+    if(packetNotificationType == null) return false;
+
+    Integer listenerCount = packetNotificationTypeToListenerCountMap.get(packetNotificationType);
+
+    if(listenerCount == null) return false;
 
-    return listenerCount>0;
+    return listenerCount > 0;
   }
 }
diff --git a/packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/DecoderRegistryTest.java b/packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/DecoderRegistryTest.java
new file mode 100644 (file)
index 0000000..3a69746
--- /dev/null
@@ -0,0 +1,34 @@
+package org.opendaylight.l2switch.packethandler.decoders;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.KnownEtherType;
+
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+
+/**
+ * Created by amitmandke on 6/5/14.
+ */
+public class DecoderRegistryTest {
+  private DecoderRegistry decoderRegistry = null;
+  private PacketDecoder packetDecoder;
+
+  @Before
+  public void init() {
+    decoderRegistry = new DecoderRegistry();
+    packetDecoder = mock(PacketDecoder.class);
+  }
+
+  @Test
+  public void testAddDecoderSunnyDay() {
+    decoderRegistry.addDecoder(KnownEtherType.Arp, packetDecoder);
+    assertEquals(packetDecoder, decoderRegistry.getDecoder(KnownEtherType.Arp));
+  }
+
+  public void testGetDecoderWithoutAdding() {
+    PacketDecoder decoder = decoderRegistry.getDecoder(KnownEtherType.Arp);
+    assertEquals(null, decoder);
+  }
+}
diff --git a/packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/PacketNotificationRegistryTest.java b/packethandler/implementation/src/test/java/org/opendaylight/l2switch/packethandler/decoders/PacketNotificationRegistryTest.java
new file mode 100644 (file)
index 0000000..308d463
--- /dev/null
@@ -0,0 +1,59 @@
+package org.opendaylight.l2switch.packethandler.decoders;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.KnownEtherType;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+import static junit.framework.Assert.assertEquals;
+
+
+/**
+ * Created by amitmandke on 6/5/14.
+ */
+public class PacketNotificationRegistryTest {
+  PacketNotificationRegistry packetNotificationRegistry = null;
+
+  @Before
+  public void init() {
+    packetNotificationRegistry = new PacketNotificationRegistry();
+  }
+
+  @Test
+  public void testIsListenerSubscribedByNotificationTypeWithoutAddingAnyListener() {
+    assertEquals(false, packetNotificationRegistry.isListenerSubscribed(Notification.class));
+  }
+
+  @Test
+  public void testIsListenerSubscribedByEtherTypeWithoutAddingAnyListener() {
+    assertEquals(false, packetNotificationRegistry.isListenerSubscribed(KnownEtherType.Arp));
+  }
+
+  @Test
+  public void testIsListenerSubscribedByNotificationTypeSunnyDay() {
+    addNotification();
+    assertEquals(true, packetNotificationRegistry.isListenerSubscribed(Notification.class));
+  }
+
+  @Test
+  public void testIsListenerSubscribedByEtherTypeWithoutAddingEtherType() {
+    addNotification();
+    assertEquals(false, packetNotificationRegistry.isListenerSubscribed(KnownEtherType.Arp));
+  }
+
+  @Test
+  public void testIsListenerSubscribedByEtherTypeSunnyDay() {
+    addNotification();
+    addEtherTypeNotification();
+    assertEquals(true, packetNotificationRegistry.isListenerSubscribed(KnownEtherType.Arp));
+  }
+
+  private void addNotification() {
+    packetNotificationRegistry.onNotificationSubscribtion(Notification.class);
+
+  }
+
+  private void addEtherTypeNotification() {
+    packetNotificationRegistry.trackPacketNotificationListener(KnownEtherType.Arp, Notification.class);
+  }
+}