Support to disable voltha teardown after tests.
One can just re-run the test again if existing configuration exists in voltha.

Change-Id: I4c85097a8324ffa5923a7491da25968f14067fb3
diff --git a/src/test/utils/CordTestConfig.py b/src/test/utils/CordTestConfig.py
index 95320e5..5653773 100644
--- a/src/test/utils/CordTestConfig.py
+++ b/src/test/utils/CordTestConfig.py
@@ -99,7 +99,8 @@
                         olt_mac = '00:0c:e2:31:12:00',
                         olt_ip = None,
                         uplink_vlan_map = { 'of:0000000000000001' : '222' },
-                        uplink_vlan_start = 333
+                        uplink_vlan_start = 333,
+                        teardown = True,
                         )
     voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
     voltha_configure = True
@@ -127,6 +128,10 @@
             setattr(class_test, 'voltha_ctrl', ret[0])
             setattr(class_test, 'voltha_device', ret[1])
             setattr(class_test, 'voltha_switch_map', ret[2])
+            voltha_driver_configured = ret[3]
+            setattr(class_test, 'voltha_preconfigured', voltha_driver_configured)
+            if voltha_driver_configured:
+                setattr(class_test, 'VOLTHA_TEARDOWN', False)
 
 def teardown_module(module):
     class_test = get_test_class(module)
@@ -134,12 +139,17 @@
         return
     if not hasattr(class_test, 'voltha_ctrl') or \
        not hasattr(class_test, 'voltha_device') or \
-       not hasattr(class_test, 'voltha_switch_map'):
+       not hasattr(class_test, 'voltha_switch_map') or \
+       not hasattr(class_test, 'voltha_preconfigured') or \
+       not hasattr(class_test, 'VOLTHA_TEARDOWN'):
         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)
+    voltha_preconfigured = getattr(class_test, 'voltha_preconfigured')
+    voltha_teardown = getattr(class_test, 'VOLTHA_TEARDOWN')
+    if voltha_preconfigured is False and voltha_teardown is True:
+        voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
 
 def running_on_ciab():
     if running_on_pod() is False:
diff --git a/src/test/utils/VolthaCtrl.py b/src/test/utils/VolthaCtrl.py
index 615330d..aa19e6a 100644
--- a/src/test/utils/VolthaCtrl.py
+++ b/src/test/utils/VolthaCtrl.py
@@ -218,7 +218,8 @@
     OPER_STATUS = 'oper_status'
     CONNECT_STATUS = 'connect_status'
 
-    def __init__(self, host = HOST, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP, uplink_vlan_start = UPLINK_VLAN_START):
+    def __init__(self, host = HOST, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP,
+                 uplink_vlan_start = UPLINK_VLAN_START):
         self.host = host
         self.rest_port = rest_port
         self.rest_url = 'http://{}:{}/api/v1/local'.format(host, rest_port)
@@ -232,7 +233,7 @@
         self.switches = []
         self.switch_map = {}
 
-    def config(self, fake = False):
+    def config(self, fake = False, driver_configured = False):
         devices = OnosCtrl.get_devices()
         if not devices:
             return self.switch_map
@@ -248,6 +249,7 @@
             if device_id not in self.uplink_vlan_map:
                 uplink_vlan = VolthaCtrl.UPLINK_VLAN_START
                 VolthaCtrl.UPLINK_VLAN_START += 1
+                self.uplink_vlan_map[device_id] = uplink_vlan
                 log.info('Voltha device %s not in map. Using uplink vlan %d' %(device_id, uplink_vlan))
             else:
                 uplink_vlan = self.uplink_vlan_map[device_id]
@@ -275,7 +277,7 @@
                                                                        vlan = uplink_vlan,
                                                                        defaultVlan=str(onu_ports[0])
                                                                        )
-        if device_id:
+        if device_id and driver_configured is False:
             #toggle drivers/openflow base before reconfiguring the driver and olt config data
             OnosCtrl('org.onosproject.drivers').deactivate()
             OnosCtrl('org.onosproject.openflow-base').deactivate()
@@ -413,25 +415,40 @@
                  olt_type = 'ponsim_olt', olt_mac = '00:0c:e2:31:12:00',
                  uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP,
                  uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START,
-                 config_fake = False, olt_app = None):
-
+                 config_fake = False, olt_app = None, teardown = True):
+    devices = OnosCtrl.get_devices()
+    olt_devices = filter(lambda d: not d['mfr'].startswith('Nicira') and d['driver'] == 'pmc-olt', devices)
     voltha = VolthaCtrl(host, rest_port = rest_port,
                         uplink_vlan_map = uplink_vlan_map,
                         uplink_vlan_start = uplink_vlan_start)
-    if olt_type.startswith('ponsim'):
-        ponsim_address = '{}:50060'.format(ponsim_host)
-        log.info('Enabling ponsim olt')
-        device_id, status = voltha.enable_device(olt_type, address = ponsim_address)
+    voltha_devices = voltha.get_devices()
+    if voltha_devices:
+        voltha_device_ids = filter(lambda d: d[voltha.OPER_STATUS] == 'ACTIVE' and d[voltha.ADMIN_STATE] == 'ENABLED',
+                                   voltha_devices['items'])
     else:
-        if olt_type.startswith('maple'):
-            if olt_ip:
-                log.info('Enabling %s' %olt_type)
-                device_id, status = voltha.enable_device(olt_type, address = olt_ip)
-            else:
-                log.info('OLT IP needs to be specified for maple olt')
+        voltha_device_ids = []
+
+    driver_configured = len(olt_devices) > 0 and len(voltha_device_ids) > 0
+    if olt_type.startswith('ponsim'):
+        if driver_configured:
+            device_id, status = voltha_device_ids[0], True
         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)
+            ponsim_address = '{}:50060'.format(ponsim_host)
+            log.info('Enabling ponsim olt')
+            device_id, status = voltha.enable_device(olt_type, address = ponsim_address)
+    else:
+        if driver_configured:
+            device_id, status = voltha_device_ids[0], True
+        else:
+            if olt_type.startswith('maple'):
+                if olt_ip:
+                    log.info('Enabling %s' %olt_type)
+                    device_id, status = voltha.enable_device(olt_type, address = olt_ip)
+                else:
+                    log.info('OLT IP needs to be specified for maple olt')
+            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:
         if device_id:
@@ -444,7 +461,7 @@
         olt_app = get_olt_app()
     try:
         time.sleep(5)
-        switch_map = voltha.config(fake = config_fake)
+        switch_map = voltha.config(fake = config_fake, driver_configured = driver_configured)
         if switch_map is None:
             voltha.disable_device(device_id)
             return None
@@ -452,7 +469,7 @@
         OnosCtrl.install_app(olt_app)
         olt_installed = True
         time.sleep(5)
-        return voltha, device_id, switch_map
+        return voltha, device_id, switch_map, driver_configured
     except:
         voltha.disable_device(device_id)
         time.sleep(10)