Enable multi-subscriber support with ponsim.

Change-Id: I8ad6684f717ca78074f815d0907e8909e500734d
diff --git a/src/test/cordSubscriber/cordSubscriberTest.py b/src/test/cordSubscriber/cordSubscriberTest.py
index 0906735..db6bbe1 100644
--- a/src/test/cordSubscriber/cordSubscriberTest.py
+++ b/src/test/cordSubscriber/cordSubscriberTest.py
@@ -2723,3 +2723,16 @@
           self.cord_subscriber_voltha(services, cbs = cbs,
                                       num_subscribers = num_subscribers,
                                       num_channels = num_channels)
+
+      def test_cord_subscriber_voltha_tls_igmp_3(self):
+          """Test subscriber join next for channel surfing with 3 subscribers browsing 3 channels each"""
+          if self.VOLTHA_HOST is None:
+                log_test.info('Skipping test as no voltha host')
+                return
+          num_subscribers = 3
+          num_channels = 3
+          services = ('TLS','IGMP',)
+          cbs = ( self.tls_verify, self.voltha_igmp_next_verify,)
+          self.cord_subscriber_voltha(services, cbs = cbs,
+                                      num_subscribers = num_subscribers,
+                                      num_channels = num_channels)
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 6f8ffe7..73b2db5 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -243,22 +243,45 @@
         res = 0
         switches = self.port_map['switches']
         start_vlan = self.port_map['start_vlan']
+        ponsim = self.port_map['ponsim']
         start_vlan += port_num
         uplink = self.port_map['uplink']
         wan = self.port_map['wan']
+        if ponsim is True:
+            if not wan:
+                wan = 'ponmgmt'
         vcpe_port_num = port_num
         port_list = self.port_map['switch_port_list'] + self.port_map['switch_relay_port_list']
         print('Provisioning the ports for the test container\n')
         for host_intf, ports in port_list:
+            setup_ponsim = ponsim
+            host_index = 0
+            host_intf_base = 'pon1'
             #if the host interface/switch does not exist, just create a dummy ovs switch
             #needed if we are running with no-switch option
             if not os.access('/sys/class/net/{}'.format(host_intf), os.F_OK):
                 os.system('ovs-vsctl add-br {}'.format(host_intf))
             uplink = self.port_map[host_intf]['uplink']
+            if setup_ponsim is True:
+                if host_intf.find('_') < 0:
+                    print('Invalid host interface specified with ponsim. Disabling ponsim setup')
+                    setup_ponsim = False
+                else:
+                    try:
+                        host_index = int(host_intf.split('_')[-1])
+                        host_intf_base = host_intf.split('_')[0]
+                    except:
+                        print('Invalid host interface with ponsim. Disabling ponsim setup')
+                        setup_ponsim = False
             for port in ports:
                 guest_if = port
                 local_if = 'l{}'.format(port_num+1) #port #'{0}_{1}'.format(guest_if, port_num+1)
                 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, port_num+1)
+                if setup_ponsim is True:
+                    if port != self.port_map[uplink]:
+                        host_intf = '{}_{}'.format(host_intf_base, host_index)
+                        host_index += 1
+
                 ##Use pipeworks to configure container interfaces on host/bridge interfaces
                 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if,
                                                                            local_if, self.name, guest_ip)
@@ -270,7 +293,7 @@
                     if start_vlan != 0:
                         pipework_cmd += ' @{}'.format(start_vlan)
                         start_vlan += 1
-                #print('Running PIPEWORK cmd: %s' %pipework_cmd)
+                print('Running PIPEWORK cmd: %s' %pipework_cmd)
                 res += os.system(pipework_cmd)
                 port_num += 1
 
@@ -668,7 +691,7 @@
 
     if voltha_loc:
         #start voltha
-        voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf)
+        voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf, olt_config = olt_config_file)
         voltha.start()
 
     if radius_ip is None:
@@ -944,7 +967,7 @@
 
     if voltha_loc:
         #start voltha
-        voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf)
+        voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf, olt_config = olt_config_file)
         voltha.start()
 
     ##Start Radius container if not started
diff --git a/src/test/setup/olt_config_ponsim.json b/src/test/setup/olt_config_ponsim.json
index 1bdc9fa..6c5737d 100644
--- a/src/test/setup/olt_config_ponsim.json
+++ b/src/test/setup/olt_config_ponsim.json
@@ -1,11 +1,11 @@
 { "olt" : false,
   "uplink" : 2,
   "vlan" : 0,
-  "port_map" : { "ports": [ "veth0", "veth2", "veth4", "veth6", "veth8", "veth10", "veth12", "veth14", "veth16", "veth18", "veth20", "veth22" ], "start_vlan" : 0, "host": "pon1_128" },
+  "port_map" : { "num_ports" : 11, "start_vlan" : 0, "nr_switches": 1, "host" : "pon1_128", "ponsim" : true},
   "vcpe" :
   [
         {
-          "port" : "enp0s9", "type" : "reserved", "s_tag" : 222, "c_tag" : 111
+          "port" : "enp0s9", "type" : "reserved", "s_tag" : 222, "c_tag" : 128
         },
         {
           "port" : "enp0s9", "type" : "dhcp", "s_tag" : 304, "c_tag" : 304
diff --git a/src/test/utils/OltConfig.py b/src/test/utils/OltConfig.py
index cd52ff5..3b4254d 100644
--- a/src/test/utils/OltConfig.py
+++ b/src/test/utils/OltConfig.py
@@ -38,6 +38,7 @@
     def olt_port_map(self):
         if self.on_olt() and self.olt_conf.has_key('port_map'):
             port_map = {}
+            port_map['ponsim'] = self.olt_conf['port_map'].has_key('ponsim')
             if self.olt_conf['port_map'].has_key('switches'):
                 port_map['switches'] = self.olt_conf['port_map']['switches']
             else:
diff --git a/src/test/utils/VolthaCtrl.py b/src/test/utils/VolthaCtrl.py
index d018edb..8d88d9f 100644
--- a/src/test/utils/VolthaCtrl.py
+++ b/src/test/utils/VolthaCtrl.py
@@ -6,13 +6,14 @@
 from CordTestUtils import log_test as log, getstatusoutput, get_controller
 from CordContainer import Container
 from OnosCtrl import OnosCtrl
+from OltConfig import OltConfig
 
 class VolthaService(object):
     services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd')
     compose_file = 'docker-compose-system-test.yml'
     service_map = {}
 
-    def __init__(self, voltha_loc, controller, interface = 'eth0'):
+    def __init__(self, voltha_loc, controller, interface = 'eth0', olt_config = None):
         if not os.access(voltha_loc, os.F_OK):
             raise Exception('Voltha location %s not found' %voltha_loc)
         compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file)
@@ -22,6 +23,12 @@
         self.controller = controller
         self.interface = interface
         self.compose_file_loc = compose_file_loc
+        num_onus = 1
+        if olt_config is not None:
+            port_map, _ = OltConfig(olt_config).olt_port_map()
+            if port_map['ponsim'] is True:
+                num_onus = max(1, len(port_map['ports']))
+        self.num_onus = num_onus
 
     def start(self):
         start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc,
@@ -85,7 +92,7 @@
             print('VOLTHA ofagent is already running. Skipped start')
 
         ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \
-        nohup python ponsim/main.py -v >/tmp/ponsim.log 2>&1 &'".format(self.voltha_loc)
+        nohup python ponsim/main.py -o {} -v >/tmp/ponsim.log 2>&1 &'".format(self.voltha_loc, self.num_onus)
         if not self.service_running('python ponsim/main.py'):
             ret = os.system(ponsim_start_cmd)
             if ret != 0: