Adding more IGMP test cases
diff --git a/src/test/builder/buildFsm.sh b/src/test/builder/buildFsm.sh
index aee58ca..51fd8d3 100644
--- a/src/test/builder/buildFsm.sh
+++ b/src/test/builder/buildFsm.sh
@@ -4,8 +4,13 @@
     odir = "./"
 fi
 
-##Generate TLS authentication state machine
+##Generate TLS authentication Test state machine
 python yamlFsm.py -p TlsAuthHolder -f noseTlsAuthTest.yaml > ${odir}/noseTlsAuthHolder.py
 
 ##Generate PAP authentication state machine
 python yamlFsm.py -p PAPAuthHolder -f nosePAPTest.yaml > ${odir}/nosePAPAuthHolder.py
+
+
+##Generate DNS test state machine
+python yamlFsm.py -p DnsHolder -f noseDnsTest.yaml > ${odir}/noseDnsHolder.py
+
diff --git a/src/test/igmp/igmpTest.py b/src/test/igmp/igmpTest.py
index 60d314e..1c06af5 100644
--- a/src/test/igmp/igmpTest.py
+++ b/src/test/igmp/igmpTest.py
@@ -3,12 +3,20 @@
 from nose.twistedtools import reactor, deferred
 from twisted.internet import defer
 from scapy.all import *
-import time
+import time, monotonic
 import os, sys
+import tempfile
+import json
+import random
+import threading
+
 CORD_TEST_UTILS = 'utils'
 test_root = os.getenv('CORD_TEST_ROOT') or './'
 sys.path.append(test_root + CORD_TEST_UTILS)
 from IGMP import *
+from McastTraffic import *
+from Stats import Stats
+log.setLevel('INFO')
 
 class IGMPTestState:
 
@@ -19,78 +27,271 @@
             self.groups = groups
             self.group_map = {} ##create a send/recv count map
             for g in groups:
-                self.group_map[g] = [0, 0]
-
-      def update(self, group, tx = 0, rx = 0):
+                self.group_map[g] = (Stats(), Stats())
+            
+      def update(self, group, tx = 0, rx = 0, t = 0):
+            self.counter += 1
             index = 0 if rx == 0 else 1
             v = tx if rx == 0 else rx
             if self.group_map.has_key(group):
-                self.group_map[group][index] += v
+                  self.group_map[group][index].update(packets = v, t = t)
 
       def update_state(self):
           self.state = self.state ^ 1
-          self.counter += 1
 
 class igmp_exchange(unittest.TestCase):
+
+    IGMP_TEST_TIMEOUT = 5
+    MCAST_TRAFFIC_TIMEOUT = 10
+    max_packets = 100
     
-    def igmp_verify(self, igmpState):
+    def onos_ssm_table_load(self, groups, src_list):
+          ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
+          ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
+          for g in groups:
+                for s in src_list:
+                      d = {}
+                      d['source'] = s
+                      d['group'] = g
+                      ssm_xlate_list.append(d)
+          json_dict = json.JSONEncoder().encode(ssm_dict)
+          with tempfile.NamedTemporaryFile(delete=False) as temp:
+                temp.write(json_dict)
+                temp.flush()
+                temp.close()
+          log.debug('Loading SSM config in file %s to ONOS.' %temp.name)
+          os.system('./igmp_ssm_load.sh %s' %temp.name)
+          os.unlink(temp.name)
+
+    def igmp_verify_join(self, igmpStateList):
+        sendState, recvState = igmpStateList
         ## check if the send is received for the groups
-        for g in igmpState.groups:
-            tx = igmpState.group_map[g][0]
-            rx = igmpState.group_map[g][1]
+        for g in sendState.groups:
+            tx_stats = sendState.group_map[g][0]  
+            tx = tx_stats.count
             assert_greater(tx, 0)
-            assert_equal(tx, rx)
-        print 'IGMP test verification success'
+            rx_stats = recvState.group_map[g][1]
+            rx = rx_stats.count
+            assert_greater(rx, 0)
+            log.info('Receive stats %s for group %s' %(rx_stats, g))
+            
+        log.info('IGMP test verification success')
 
-    def igmp_send(self, igmpState, iface = 'veth2'):
-        dst_mac = '01:00:5e:00:01:01'
-        src_mac = '02:88:b4:e4:90:77'
-        src_ip = '1.2.3.4'
-        eth = Ether(dst = dst_mac, src = src_mac)
-        data = repr(time.time())
-        for g in igmpState.groups:
-            ip = IP(dst = g, src = src_ip)
-            sendp(eth/ip/data, iface=iface)
-            igmpState.update(g, tx = 1)
+    def igmp_verify_leave(self, igmpStateList, leave_groups):
+        sendState, recvState = igmpStateList[0], igmpStateList[1]
+        ## check if the send is received for the groups
+        for g in sendState.groups:
+            tx_stats = sendState.group_map[g][0]
+            rx_stats = recvState.group_map[g][1]
+            tx = tx_stats.count
+            rx = rx_stats.count                   
+            assert_greater(tx, 0)
+            if g not in leave_groups:
+                log.info('Received %d packets for group %s' %(rx, g))
+        for g in leave_groups:
+            rx = recvState.group_map[g][1].count
+            assert_equal(rx, 0)
+            
+        log.info('IGMP test verification success')
 
-        ##done sending. Bounce the states
-        igmpState.update_state()
+    def mcast_traffic_timer(self):
+          self.mcastTraffic.stopReceives()
+
+    def send_mcast_cb(self, send_state):
+        for g in send_state.groups:
+            send_state.update(g, tx = 1)
         return 0
 
+    ##Runs in the context of twisted reactor thread
     def igmp_recv(self, igmpState, iface = 'veth0'):
-        for g in igmpState.groups:
-            igmpState.update(g, rx = 1)
-        ##done receiving. Bounce the state back to send
-        igmpState.update_state()
+        p = self.recv_socket.recv()
+        send_time = float(p.payload.load)
+        recv_time = monotonic.monotonic()
+        #log.info( 'Recv in %.6f secs' %(recv_time - send_time))
+        igmpState.update(p.dst, rx = 1, t = recv_time - send_time)
         return 0
 
-    def send_igmp_join(self, igmpState, iface = 'veth0'):
-        src_list = ['1.2.3.4']
-        for g in igmpState.groups:
+    def send_igmp_join(self, groups, src_list = ['1.2.3.4'], iface = 'veth0', delay = 2):
+        self.onos_ssm_table_load(groups, src_list)
+        for g in groups:
+            igmp = IGMP(mtype = IGMPV3_REPORT, 
+                        group = g,
+                        rtype = IGMP_INCLUDE,
+                        src_list = src_list)
+            sendp(igmp.scapify(), iface = iface)
+        if delay != 0:
+            time.sleep(delay)
+
+    def send_igmp_leave(self, groups, src_list = ['1.2.3.4'], iface = 'veth0', delay = 2):
+        for g in groups:
             igmp = IGMP(mtype = IGMPV3_REPORT, 
                         group = g,
                         rtype = IGMP_EXCLUDE,
                         src_list = src_list)
             sendp(igmp.scapify(), iface = iface)
+        if delay != 0:
+            time.sleep(delay)
 
-    @deferred(timeout=10)
-    def test_igmp_sendrecv(self):
+    @deferred(timeout=MCAST_TRAFFIC_TIMEOUT+10)
+    def test_igmp_join_verify_traffic(self):
         groups = ['224.0.1.1', '225.0.0.1']
-        def igmp_srp_task(stateObject):
-            if stateObject.state == 0:
-                result = self.igmp_send(stateObject)
-            else:
-                result = self.igmp_recv(stateObject)
-
-            if stateObject.counter < 20:
-                reactor.callLater(0, igmp_srp_task, stateObject)
-            else:
-                self.igmp_verify(stateObject)
-                stateObject.df.callback(0)
-
         df = defer.Deferred()
         igmpState = IGMPTestState(groups = groups, df = df)
-        self.send_igmp_join(igmpState)
-        reactor.callLater(0, igmp_srp_task, igmpState)
+        igmpStateRecv = IGMPTestState(groups = groups, df = df)
+        igmpStateList = (igmpState, igmpStateRecv)
+        mcastTraffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, arg = igmpState)
+        self.df = df
+        self.mcastTraffic = mcastTraffic
+        self.recv_socket = L3PacketSocket(iface = 'veth0', type = ETH_P_IP)
+
+        def igmp_srp_task(stateList):
+            igmpSendState, igmpRecvState = stateList
+            if not mcastTraffic.isRecvStopped():
+                result = self.igmp_recv(igmpRecvState)
+                reactor.callLater(0, igmp_srp_task, stateList)
+            else:
+                self.mcastTraffic.stop()
+                self.recv_socket.close()
+                self.igmp_verify_join(stateList)
+                self.df.callback(0)
+
+        self.send_igmp_join(groups)
+        mcastTraffic.start()
+        self.test_timer = reactor.callLater(self.MCAST_TRAFFIC_TIMEOUT, self.mcast_traffic_timer)
+        reactor.callLater(0, igmp_srp_task, igmpStateList)
         return df
 
+    @deferred(timeout=MCAST_TRAFFIC_TIMEOUT+10)
+    def test_igmp_leave_verify_traffic(self):
+        groups = ['224.0.1.10', '225.0.0.10']
+        leave_groups = ['224.0.1.10']
+        df = defer.Deferred()
+        igmpState = IGMPTestState(groups = groups, df = df)
+        igmpStateRecv = IGMPTestState(groups = groups, df = df)
+        igmpStateList = (igmpState, igmpStateRecv)
+        mcastTraffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, 
+                                    arg = igmpState)
+        self.df = df
+        self.mcastTraffic = mcastTraffic
+        self.recv_socket = L3PacketSocket(iface = 'veth0', type = ETH_P_IP)
+        
+        def igmp_srp_task(stateList):
+            igmpSendState, igmpRecvState = stateList
+            if not mcastTraffic.isRecvStopped():
+                result = self.igmp_recv(igmpRecvState)
+                reactor.callLater(0, igmp_srp_task, stateList)
+            else:
+                self.mcastTraffic.stop()
+                self.recv_socket.close()
+                self.igmp_verify_leave(stateList, leave_groups)
+                self.df.callback(0)
+
+        self.send_igmp_join(groups)
+        self.send_igmp_leave(leave_groups, delay = 3)
+        mcastTraffic.start()
+        self.test_timer = reactor.callLater(self.MCAST_TRAFFIC_TIMEOUT, self.mcast_traffic_timer)
+        reactor.callLater(0, igmp_srp_task, igmpStateList)
+        return df
+
+    @deferred(timeout=100)
+    def test_igmp_leave_join_loop(self):
+        self.groups = ['226.0.1.1', '227.0.0.1', '228.0.0.1', '229.0.0.1', '230.0.0.1' ]
+        self.src_list = ['3.4.5.6', '7.8.9.10']
+        df = defer.Deferred()
+        self.df = df
+        self.iterations = 0
+        self.num_groups = len(self.groups)
+        self.MAX_TEST_ITERATIONS = 10
+
+        #self.onos_ssm_table_load(self.groups, self.src_list)
+        def igmp_srp_task(v):
+              if self.iterations < self.MAX_TEST_ITERATIONS:
+                    if v == 1:
+                          ##join test
+                          self.num_groups = random.randint(0, len(self.groups))
+                          self.send_igmp_join(self.groups[:self.num_groups],
+                                              src_list = self.src_list,
+                                              iface = 'veth0', delay = 0)
+                    else:
+                          self.send_igmp_leave(self.groups[:self.num_groups],
+                                               src_list = self.src_list,
+                                               iface = 'veth0', delay = 0)
+                    self.iterations += 1
+                    v ^= 1
+                    reactor.callLater(1.0 + 0.5*self.num_groups,
+                                      igmp_srp_task, v)
+              else:
+                    self.df.callback(0)
+
+        reactor.callLater(0, igmp_srp_task, 1)
+        return df
+
+    def igmp_join_task(self, intf, groups, state, src_list = ['1.2.3.4']):
+          self.onos_ssm_table_load(groups, src_list)
+          for g in groups:
+                igmp = IGMP(mtype = IGMPV3_REPORT,
+                            group = g,
+                            rtype = IGMP_INCLUDE,
+                            src_list = src_list)
+                state.group_map[g][0].update(1, t = monotonic.monotonic())
+                sendp(igmp.scapify(), iface = intf)
+          log.debug('Returning from join task')
+
+    def igmp_recv_task(self, intf, groups, join_state):
+          recv_socket = L3PacketSocket(iface = intf, type = ETH_P_IP)
+          group_map = {}
+          for g in groups:
+                group_map[g] = [0,0]
+
+          while True:
+                p = recv_socket.recv()
+                if p.dst in groups and group_map[p.dst][0] == 0:
+                      group_map[p.dst][0] += 1
+                      group_map[p.dst][1] = monotonic.monotonic()
+                      c = 0
+                      for g in groups:
+                            c += group_map[g][0]
+                      if c == len(groups):
+                            break
+          for g in groups:
+                join_start = join_state.group_map[g][0].start
+                recv_time = group_map[g][1] * 1000000
+                delta = (recv_time - join_start)
+                log.info('Join for group %s received in %.3f usecs' %
+                         (g, delta))
+
+          recv_socket.close()
+          log.debug('Returning from recv task')
+          
+    def group_latency_check(self, groups):
+          tasks = []
+          self.send_igmp_leave(groups = groups)
+          join_state = IGMPTestState(groups = groups)
+          tasks.append(threading.Thread(target=self.igmp_join_task, args = ('veth0', groups, join_state,)))
+          traffic_state = IGMPTestState(groups = groups)
+          mcast_traffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, 
+                                       arg = traffic_state)
+          mcast_traffic.start()
+          tasks.append(threading.Thread(target=self.igmp_recv_task, args = ('veth0', groups, join_state)))
+          for t in tasks:
+                t.start()
+          for t in tasks:
+                t.join()
+
+          mcast_traffic.stop()
+          self.send_igmp_leave(groups = groups)
+          return
+
+    def test_igmp_1group_join_latency(self):
+          groups = ['239.0.1.1']
+          self.group_latency_check(groups)
+
+    def test_igmp_2group_join_latency(self):
+          groups = ['239.0.1.1', '240.0.1.1']
+          self.group_latency_check(groups)
+
+    def test_igmp_Ngroup_join_latency(self):
+          groups = ['239.0.1.1', '240.0.1.1', '241.0.1.1', '242.0.1.1']
+          self.group_latency_check(groups)
+
+          
diff --git a/src/test/igmp/igmp_ssm_load.sh b/src/test/igmp/igmp_ssm_load.sh
new file mode 100755
index 0000000..b2e9623
--- /dev/null
+++ b/src/test/igmp/igmp_ssm_load.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+json="$1"
+controller="$2"
+if [ x"$json" = "x" ]; then
+  echo "No json file specified. Exiting"
+  exit 127
+fi
+if [ x"$controller" = "x" ]; then
+    controller=`ovs-vsctl show | egrep "Controller|tcp" | grep -v ptcp | sed 's,Controller,,g' | sed 's,\",,g' | tr -s ' '|awk -F":" '{print $2}'`
+    #echo "Controller at $controller"
+fi
+#echo "Loading ssm translate json file $json to controller at $controller"
+curl --fail -sSL --user karaf:karaf \
+    -X POST -H 'Content-Type:application/json' \
+    http://$controller:8181/onos/v1/network/configuration/ -d@$json
+
+
diff --git a/src/test/utils/IGMP.py b/src/test/utils/IGMP.py
index 95ce1ec..64aa27f 100644
--- a/src/test/utils/IGMP.py
+++ b/src/test/utils/IGMP.py
@@ -112,7 +112,7 @@
     def build_leave_msg(self, group = None):
         return self.build_igmp(msg_type = IGMPV3_REPORT, 
                                group = group, 
-                               record_type = IGMP_INCLUDE, 
+                               record_type = IGMP_EXCLUDE, 
                                src_list = [])
 
     def build_ip_igmp(self, 
diff --git a/src/test/utils/McastTraffic.py b/src/test/utils/McastTraffic.py
new file mode 100644
index 0000000..2bd28e2
--- /dev/null
+++ b/src/test/utils/McastTraffic.py
@@ -0,0 +1,49 @@
+import threading
+import sys
+import os
+import time
+import monotonic
+from scapy.all import *
+
+class McastTraffic(threading.Thread):
+
+    dst_mac = '01:00:5e:00:01:01'
+    src_mac = '02:88:b4:e4:90:77'
+    src_ip = '1.2.3.4'
+    SEND_STATE = 1
+    RECV_STATE = 2
+    def __init__(self, addrs, iface = 'eth0', cb = None, arg = None):
+        threading.Thread.__init__(self)
+        self.addrs = addrs
+        self.iface = iface
+        self.cb = cb
+        self.arg = arg
+        self.state = self.SEND_STATE | self.RECV_STATE
+
+    def run(self):
+        eth = Ether(dst = self.dst_mac, src = self.src_mac)
+        while self.state & self.SEND_STATE:
+            for addr in self.addrs:
+                #data = repr(time.time())
+                data = repr(monotonic.monotonic())
+                ip = IP(dst = addr, src = self.src_ip)
+                sendp(eth/ip/data, iface = self.iface)
+            if self.cb:
+                self.cb(self.arg)
+
+    def stop(self):
+        self.state = 0
+
+    def stopReceives(self):
+        self.state &= ~self.RECV_STATE
+
+    def stopSends(self):
+        self.state &= ~self.SEND_STATE
+
+    def isRecvStopped(self):
+        return False if self.state & self.RECV_STATE else True
+
+    def isSendStopped(self):
+        return False if self.state & self.SEND_STATE else True
+
+    
diff --git a/src/test/utils/Stats.py b/src/test/utils/Stats.py
new file mode 100644
index 0000000..3d8e40c
--- /dev/null
+++ b/src/test/utils/Stats.py
@@ -0,0 +1,34 @@
+from math import sqrt
+
+class Stats:
+      def __init__(self):
+            self.count = 0
+            self.start = 0
+            self.delta = 0
+            self.min = 0
+            self.max = 0
+            self.delta_squares = 0
+
+      def update(self, packets = 0, t = 0):
+            self.count += packets
+            t *= 1000000 ##convert to usecs
+            if self.start == 0:
+                  self.start = t
+            self.delta += t
+            self.delta_squares += t*t
+            if self.min == 0 or t < self.min:
+                  self.min = t
+            if self.max == 0 or t > self.max:
+                  self.max = t
+
+      def __repr__(self):
+            if self.count == 0:
+                  self.count = 1
+            mean = self.delta/self.count
+            mean_square = mean*mean
+            delta_square_mean = self.delta_squares/self.count
+            std_mean = sqrt(delta_square_mean - mean_square)
+            r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
+                  mean, std_mean, self.min, self.max, self.count)
+            return r
+