Enable VOLTHA auto configuration during test module setup.
Also remove voltha configuration of test module teardown.
The test case files just have to import setup_module, teardown_module from CordTestConfig

Change-Id: I13d04931d947714a67cc665f48c0351a2431875c
diff --git a/src/test/cordSubscriber/cordSubscriberTest.py b/src/test/cordSubscriber/cordSubscriberTest.py
index b9d23ff..9aeba36 100644
--- a/src/test/cordSubscriber/cordSubscriberTest.py
+++ b/src/test/cordSubscriber/cordSubscriberTest.py
@@ -267,6 +267,8 @@
 yg==
 -----END CERTIFICATE-----'''
 
+      #disable voltha auto configuration
+      VOLTHA_AUTO_CONFIGURE = False
       VOLTHA_HOST = None
       VOLTHA_REST_PORT = 8881
       VOLTHA_UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' }
diff --git a/src/test/tls/tlsTest.py b/src/test/tls/tlsTest.py
index 3fd72f5..3417b1f 100644
--- a/src/test/tls/tlsTest.py
+++ b/src/test/tls/tlsTest.py
@@ -23,8 +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 CordTestConfig import setup_module, teardown_module
+from VolthaCtrl import VolthaCtrl
 from scapy.all import *
 from scapy_ssl_tls.ssl_tls import *
 from scapy_ssl_tls.ssl_tls_crypto import *
@@ -41,9 +41,6 @@
     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
@@ -86,24 +83,6 @@
                              '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/CordTestConfig.py b/src/test/utils/CordTestConfig.py
index 7aca5cb..e57b948 100644
--- a/src/test/utils/CordTestConfig.py
+++ b/src/test/utils/CordTestConfig.py
@@ -6,6 +6,7 @@
 from nose.plugins import Plugin
 from CordTestUtils import log_test as log
 from CordTestUtils import running_on_pod
+from VolthaCtrl import voltha_setup, voltha_teardown
 from SSHTestAgent import SSHTestAgent
 log.setLevel('INFO')
 
@@ -49,7 +50,7 @@
     def addFailure(self, test, exception):
         self.doFailure(test, exception)
 
-def setup_module(module):
+def get_test_class(module):
     class_test = None
     for name, obj in inspect.getmembers(module):
         if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
@@ -59,6 +60,10 @@
             else:
                 class_test = obj
 
+    return class_test
+
+def setup_module(module):
+    class_test = get_test_class(module)
     assert_not_equal(class_test, None)
     module_name = module.__name__.split('.')[-1]
     cfg = '{}.json'.format(module_name)
@@ -69,6 +74,47 @@
             for k, v in json_data.iteritems():
                 setattr(class_test, k, v)
 
+    #check for voltha and configure as appropriate
+    voltha_attrs = dict(host='172.17.0.1',
+                        rest_port = 8881,
+                        config_fake = False,
+                        olt_type = 'ponsim',
+                        olt_mac = '00:0c:e2:31:12:00',
+                        uplink_vlan_map = { 'of:0000000000000001' : '222' }
+                        )
+    voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
+    voltha_configure = True
+    if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'):
+        voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE')
+
+    if voltha_enabled and voltha_configure:
+        for k,v in voltha_attrs.iteritems():
+            voltha_attr = 'VOLTHA_{}'.format(k.upper())
+            if hasattr(class_test, voltha_attr):
+                v = getattr(class_test, voltha_attr)
+                voltha_attrs[k] = v
+            else:
+                setattr(class_test, voltha_attr, v)
+        ret = voltha_setup(**voltha_attrs)
+        if ret is not None:
+            #setup the stage to drop voltha on the way out
+            setattr(class_test, 'voltha_ctrl', ret[0])
+            setattr(class_test, 'voltha_device', ret[1])
+            setattr(class_test, 'voltha_switch_map', ret[2])
+
+def teardown_module(module):
+    class_test = get_test_class(module)
+    if class_test is None:
+        return
+    if not hasattr(class_test, 'voltha_ctrl') or \
+       not hasattr(class_test, 'voltha_device') or \
+       not hasattr(class_test, 'voltha_switch_map'):
+        return
+    voltha_ctrl = getattr(class_test, 'voltha_ctrl')
+    voltha_device = getattr(class_test, 'voltha_device')
+    voltha_switch_map = getattr(class_test, 'voltha_switch_map')
+    voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
+
 def running_on_ciab():
     if running_on_pod() is False:
         return False
diff --git a/src/test/utils/VolthaCtrl.py b/src/test/utils/VolthaCtrl.py
index 955644e..9e9b757 100644
--- a/src/test/utils/VolthaCtrl.py
+++ b/src/test/utils/VolthaCtrl.py
@@ -349,4 +349,5 @@
     time.sleep(10)
     if olt_app is None:
         olt_app = get_olt_app()
+    log.info('Uninstalling OLT app %s' %olt_app)
     OnosCtrl.uninstall_app(olt_app)