Changes to TLS/voltha interface to make the TLS test work when running cord-tester under voltha.

Change-Id: Id3701cf695d210307bb9ce86e4cf85e7f7b39908
diff --git a/src/test/tls/tlsTest.json b/src/test/tls/tlsTest.json
new file mode 100644
index 0000000..fd88ff0
--- /dev/null
+++ b/src/test/tls/tlsTest.json
@@ -0,0 +1,6 @@
+{
+    "VOLTHA_HOST" : "172.17.0.1",
+    "VOLTHA_REST_PORT" : 8881,
+    "VOLTHA_OLT_TYPE" : "ponsim_olt",
+    "VOLTHA_OLT_MAC" : "00:0c:e2:31:12:00"
+}
diff --git a/src/test/tls/tlsTest.py b/src/test/tls/tlsTest.py
index 295f23e..3fd72f5 100644
--- a/src/test/tls/tlsTest.py
+++ b/src/test/tls/tlsTest.py
@@ -23,6 +23,8 @@
 from OnosCtrl import OnosCtrl
 from CordLogger import CordLogger
 from CordTestUtils import log_test
+from CordTestConfig import setup_module
+from VolthaCtrl import VolthaCtrl, voltha_setup, voltha_teardown
 from scapy.all import *
 from scapy_ssl_tls.ssl_tls import *
 from scapy_ssl_tls.ssl_tls_crypto import *
@@ -33,6 +35,15 @@
     app = 'org.opencord.aaa'
     TLS_TIMEOUT = 20
     TEST_TIMEOUT = 3600
+    VOLTHA_HOST = None
+    VOLTHA_REST_PORT = 8881
+    VOLTHA_ENABLED = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
+    VOLTHA_OLT_TYPE = 'simulated_olt'
+    VOLTHA_OLT_MAC = '00:0c:e2:31:12:00'
+    VOLTHA_UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' }
+    voltha_device = None
+    voltha_ctrl = None
+    voltha_switch_map = None
     #this is from ca.pem file
     CLIENT_CERT_INVALID = '''-----BEGIN CERTIFICATE-----
 MIIEyTCCA7GgAwIBAgIJAN3OagiHm6AXMA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD
@@ -74,6 +85,25 @@
                              'TLS_DHE_RSA_WITH_AES_256_CBC_SHA256',
                              'TLS_DH_anon_WITH_AES_256_CBC_SHA256']
 
+
+    @classmethod
+    def setUpClass(cls):
+        #activate the device if voltha was enabled
+        if cls.VOLTHA_ENABLED is False or cls.VOLTHA_HOST is None:
+            return
+        ret = voltha_setup(host = cls.VOLTHA_HOST,
+                           rest_port = cls.VOLTHA_REST_PORT,
+                           olt_type = cls.VOLTHA_OLT_TYPE,
+                           olt_mac = cls.VOLTHA_OLT_MAC,
+                           uplink_vlan_map = cls.VOLTHA_UPLINK_VLAN_MAP)
+        if ret is not None:
+            cls.voltha_ctrl, cls.voltha_device, cls.voltha_switch_map = ret[0], ret[1], ret[2]
+
+    @classmethod
+    def tearDownClass(cls):
+        if cls.voltha_ctrl and cls.voltha_device:
+            voltha_teardown(cls.voltha_ctrl, cls.voltha_device, cls.voltha_switch_map)
+
     def setUp(self):
         super(eap_auth_exchange, self).setUp()
         self.onos_ctrl = OnosCtrl(self.app)
diff --git a/src/test/utils/VolthaCtrl.py b/src/test/utils/VolthaCtrl.py
index ccf0c65..955644e 100644
--- a/src/test/utils/VolthaCtrl.py
+++ b/src/test/utils/VolthaCtrl.py
@@ -4,7 +4,7 @@
 import os
 import signal
 from CordTestUtils import log_test as log, getstatusoutput, get_controller
-from CordContainer import Container
+from CordContainer import Container, Onos
 from OnosCtrl import OnosCtrl
 from OltConfig import OltConfig
 
@@ -126,8 +126,9 @@
 class VolthaCtrl(object):
 
     UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' }
+    REST_PORT = 8881
 
-    def __init__(self, host, rest_port = 8881, uplink_vlan_map = UPLINK_VLAN_MAP):
+    def __init__(self, host, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP):
         self.host = host
         self.rest_port = rest_port
         self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port)
@@ -285,3 +286,67 @@
         if device_info['admin_status'] == 'PREPROVISIONED':
            return True
         return False
+
+def get_olt_app():
+    our_path = os.path.dirname(os.path.realpath(__file__))
+    version = Onos.getVersion()
+    major = int(version.split('.')[0])
+    minor = int(version.split('.')[1])
+    olt_app_version = '1.2-SNAPSHOT'
+    if major > 1:
+        olt_app_version = '2.0-SNAPSHOT'
+    elif major == 1:
+        if minor > 10:
+            olt_app_version = '2.0-SNAPSHOT'
+        elif minor <= 8:
+            olt_app_version = '1.1-SNAPSHOT'
+    olt_app_file = os.path.join(our_path, '..', 'apps/olt-app-{}.oar'.format(olt_app_version))
+    return olt_app_file
+
+def voltha_setup(host = '172.17.0.1', rest_port = VolthaCtrl.REST_PORT,
+                 olt_type = 'ponsim', olt_mac = '00:0c:e2:31:12:00',
+                 uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP,
+                 config_fake = False, olt_app = None):
+
+    voltha = VolthaCtrl(host, rest_port = rest_port, uplink_vlan_map = uplink_vlan_map)
+    if olt_type.startswith('ponsim'):
+        ponsim_address = '{}:50060'.format(host)
+        log.info('Enabling ponsim olt')
+        device_id, status = voltha.enable_device(olt_type, address = ponsim_address)
+    else:
+        log.info('Enabling OLT instance for %s with mac %s' %(olt_type, olt_mac))
+        device_id, status = voltha.enable_device(olt_type, olt_mac)
+
+    if device_id is None or status is False:
+        voltha.disable_device(device_id)
+        return None
+
+    switch_map = None
+    olt_installed = False
+    if olt_app is None:
+        olt_app = get_olt_app()
+    try:
+        switch_map = voltha.config(fake = config_fake)
+        if switch_map is None:
+            voltha.disable_device(device_id)
+            return None
+        log.info('Installing OLT app %s' %olt_app)
+        OnosCtrl.install_app(olt_app)
+        olt_installed = True
+        time.sleep(5)
+        return voltha, device_id, switch_map
+    except:
+        voltha.disable_device(device_id)
+        time.sleep(10)
+        if olt_installed is True:
+            log.info('Uninstalling OLT app %s' %olt_app)
+            OnosCtrl.uninstall_app(olt_app)
+
+    return None
+
+def voltha_teardown(voltha_ctrl, device_id, switch_map, olt_app = None):
+    voltha_ctrl.disable_device(device_id)
+    time.sleep(10)
+    if olt_app is None:
+        olt_app = get_olt_app()
+    OnosCtrl.uninstall_app(olt_app)