Support to run OVS-vswitch with ONOS configured to run with Openflow SSL (Voltha ONOS)
Also various changes to make sure the test container also has access to the ovs switch interfaces.
This comes into play if user has specified start_switch to true in manifest file.
Typically true when running cord-tester with voltha/ponsim,etc.

Change-Id: I501761f2b00b8f43caa9c92cff4d3d01860cb04d
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 8b7e0fc..2b1c82f 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -84,13 +84,13 @@
             self.create = False
             self.rm = False
         self.olt = False
+        self.switch_started = False
         olt_config_file = 'olt_config.json'
         if env is not None:
             if env.has_key('OLT_CONFIG'):
                 self.olt = True
             if env.has_key('OLT_CONFIG_FILE'):
                 olt_config_file = os.path.basename(env['OLT_CONFIG_FILE'])
-
         olt_conf_file = os.path.join(self.tester_base, olt_config_file)
         olt_config = OltConfig(olt_conf_file)
         self.port_map, _ = olt_config.olt_port_map()
@@ -155,27 +155,31 @@
         devices = OnosCtrl.get_devices(controller = ctlr)
         if devices:
             device = filter(lambda d: d['id'] == device_id, devices)
-            return True
+            if device:
+                return True
         return False
 
-    def start_switch(self, boot_delay = 2):
+    def start_switch(self, manifest, boot_delay = 2):
         """Start OVS"""
         ##Determine if OVS has to be started locally or not
         s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
         ovs_cmd = os.path.join(s_sandbox, s_file)
+        switches = filter(lambda sw: sw.startswith('br-int'), self.switches)
         if self.olt:
             if CordTester.switch_on_olt is True:
                 return
             CordTester.switch_on_olt = True
-            ovs_cmd += ' {} {}'.format(len(self.switches), self.ctlr_ip)
-            print('Starting OVS on the host with %d switches for controller: %s' %(len(self.switches), self.ctlr_ip))
+            ovs_cmd += ' {} {}'.format(len(switches), self.ctlr_ip)
+            if manifest.voltha_enable and manifest.voltha_loc and Onos.ssl_key:
+                ovs_cmd += ' {}'.format(manifest.voltha_loc)
+            print('Starting OVS on the host with %d switches for controller: %s' %(len(switches), self.ctlr_ip))
         else:
             ovs_cmd += ' {}'.format(self.switches[0])
             print('Starting OVS on test container %s for controller: %s' %(self.name, self.ctlr_ip))
         self.execute_switch(ovs_cmd)
         time.sleep(5)
         ## Wait for the controller to see the switch
-        for switch in self.switches:
+        for switch in switches:
             status = 1
             tries = 0
             result = self.ctlr_switch_availability(switch) and self.test_flow(switch)
@@ -203,6 +207,8 @@
         if boot_delay:
             time.sleep(boot_delay)
 
+        self.switch_started = True
+
     def setup_vcpes(self, port_num = 0):
         res = 0
         for vcpe in self.vcpes:
@@ -257,6 +263,8 @@
         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:
+            if self.switch_started is False and host_intf.startswith('br-int'):
+                continue
             setup_ponsim = ponsim
             host_index = 0
             host_intf_base = 'pon1'
@@ -300,6 +308,10 @@
                 res += os.system(pipework_cmd)
                 port_num += 1
 
+            if setup_ponsim is True:
+                ponsim = False
+                wan = None
+
         self.setup_vcpes(vcpe_port_num)
         return res, port_num
 
@@ -629,7 +641,6 @@
         if voltha_loc:
             voltha_key = os.path.join(voltha_loc, 'docker', 'onos_cfg', 'onos.jks')
             Onos.update_ssl_key(voltha_key)
-            test_manifest.start_switch = False
         image_names = test_manifest.onos_image.rsplit(':', 1)
         onos_cnt['image'] = image_names[0]
         if len(image_names) > 1:
@@ -790,7 +801,7 @@
             continue
         if test_cnt.create and (test_manifest.start_switch or not test_manifest.olt):
             if not args.no_switch:
-                test_cnt.start_switch()
+                test_cnt.start_switch(test_manifest)
         if test_cnt.create and test_cnt.olt:
             _, port_num = test_cnt.setup_intfs(port_num = port_num)
 
@@ -820,7 +831,7 @@
             #For non parallel tests, we just restart the switch also for OLT's
             CordTester.switch_on_olt = False
             if not args.no_switch:
-                test_cnt.start_switch()
+                test_cnt.start_switch(test_manifest)
         if test_cnt.create and test_cnt.olt:
             test_cnt.setup_intfs(port_num = port_num)
         test_cnt.run_tests()
@@ -932,7 +943,6 @@
         if voltha_loc:
             voltha_key = os.path.join(voltha_loc, 'docker', 'onos_cfg', 'onos.jks')
             Onos.update_ssl_key(voltha_key)
-            test_manifest.start_switch = False
         data_volume = '{}-data'.format(Onos.NAME) if test_manifest.shared_volume else None
         onos = Onos(image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
                     data_volume = data_volume, async = async_mode, network = test_manifest.docker_network)
@@ -1049,7 +1059,7 @@
                               network = test_manifest.docker_network)
 
         if test_manifest.start_switch or not test_manifest.olt:
-            test_cnt.start_switch()
+            test_cnt.start_switch(test_manifest)
         if test_cnt.olt:
             test_cnt.setup_intfs(port_num = 0)
         print('Test container %s started and provisioned to run tests using nosetests' %(test_cnt.name))
diff --git a/src/test/setup/of-bridge-local.sh b/src/test/setup/of-bridge-local.sh
index da946dc..d41e18a 100755
--- a/src/test/setup/of-bridge-local.sh
+++ b/src/test/setup/of-bridge-local.sh
@@ -1,6 +1,7 @@
 #!/usr/bin/env bash
 bridge="$1"
 controller="$2"
+voltha_loc="$3"
 if [ x"$bridge" = "x" ]; then
   bridge="ovsbr0"
 fi
@@ -30,16 +31,31 @@
     bridges[0]=$bridge
 fi
 
-ctlr=""
-for ip in `echo $controller | tr ',' '\n'`; do
-  ctlr="$ctlr tcp:$ip:6653"
-done
-
 #Delete existing bridges if any
 for br in "${bridges[@]}"; do
     ovs-vsctl del-br $br
 done
 
+proto=tcp
+if [ x"$voltha_loc" != "x" ]; then
+    onos_jks="$voltha_loc/docker/onos_cfg/onos.jks"
+    client_cert="$voltha_loc/pki/voltha.crt"
+    if [ -f $onos_jks ]; then
+        #extract server certificate
+        keytool -export -alias onos -file /tmp/onos.der -keystore $onos_jks -storepass 222222
+        openssl x509 -inform der -in /tmp/onos.der -out /tmp/onos-cert.pem
+        cat /tmp/onos-cert.pem $client_cert > /tmp/voltha-CA.pem
+        echo "Enabling OVS SSL connection to controller"
+        ovs-vsctl set-ssl $voltha_loc/pki/voltha.key $client_cert /tmp/voltha-CA.pem
+        proto=ssl
+    fi
+fi
+
+ctlr=""
+for ip in `echo $controller | tr ',' '\n'`; do
+  ctlr="$ctlr $proto:$ip:6653"
+done
+
 for br in "${bridges[@]}"; do
     echo "Configuring OVS bridge:$br"
     ovs-vsctl add-br $br
diff --git a/src/test/utils/OltConfig.py b/src/test/utils/OltConfig.py
index 3b4254d..82ac12a 100644
--- a/src/test/utils/OltConfig.py
+++ b/src/test/utils/OltConfig.py
@@ -53,7 +53,7 @@
             if self.olt_conf['port_map'].has_key('host'):
                 #if host interface is specified, then use the host instead of ovs switch
                 port_map['host'] = self.olt_conf['port_map']['host']
-                port_map['switches'] = [ port_map['host'] ]
+                port_map['switches'] = [ port_map['host'] ] + port_map['switches']
             else:
                 port_map['host'] = port_map['switches'][0]
             nr_switches = len(port_map['switches'])
@@ -62,6 +62,17 @@
                 port_map['ports'] = self.olt_conf['port_map']['ports']
                 num_ports = len(port_map['ports'])
                 port_map['switch_port_list'].append( (port_map['switches'][0], port_map['ports']) )
+                index = 1
+                for switch in port_map['switches'][1:]:
+                    port_start = index * num_ports * 2
+                    port_end = port_start + num_ports * 2
+                    index += 1
+                    port_list = []
+                    for port in xrange(port_start, port_end, 2):
+                        port_name = 'veth{}'.format(port)
+                        port_map['ports'].append(port_name)
+                        port_list.append(port_name)
+                    port_map['switch_port_list'].append( (switch, port_list) )
             else:
                 port_map['ports'] = []
                 num_ports = int(self.olt_conf['port_map']['num_ports'])