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/cordSubscriber/cordSubscriberTest.json b/src/test/cordSubscriber/cordSubscriberTest.json
index 3a03f11..fb29f57 100644
--- a/src/test/cordSubscriber/cordSubscriberTest.json
+++ b/src/test/cordSubscriber/cordSubscriberTest.json
@@ -1,9 +1,9 @@
 {
-    "VOLTHA_HOST" : "172.17.0.1",
-    "VOLTHA_REST_PORT" : 8881,
-    "VOLTHA_IGMP_ITERATIONS" : 100,
-    "VOLTHA_CONFIG_FAKE" : true,
-    "VOLTHA_OLT_TYPE" : "ponsim_olt",
-    "VOLTHA_OLT_MAC"  : "00:0c:e2:31:12:00",
-    "VOLTHA_UPLINK_VLAN_START" : 333
-}
+    "VOLTHA_HOST": "172.17.0.1", 
+    "VOLTHA_REST_PORT": 8881, 
+    "VOLTHA_IGMP_ITERATIONS": 100, 
+    "VOLTHA_CONFIG_FAKE": true, 
+    "VOLTHA_OLT_TYPE": "ponsim_olt", 
+    "VOLTHA_OLT_MAC": "00:0c:e2:31:12:00", 
+    "VOLTHA_UPLINK_VLAN_START": 333
+}
\ No newline at end of file
diff --git a/src/test/cordSubscriber/cordSubscriberTest.py b/src/test/cordSubscriber/cordSubscriberTest.py
index 1915361..580989d 100644
--- a/src/test/cordSubscriber/cordSubscriberTest.py
+++ b/src/test/cordSubscriber/cordSubscriberTest.py
@@ -283,6 +283,7 @@
 -----END CERTIFICATE-----'''
 
       VOLTHA_HOST = None
+      VOLTHA_TEARDOWN = True
       VOLTHA_REST_PORT = 8881
       VOLTHA_UPLINK_VLAN_MAP = { 'of:0001000000000001' : '222' }
       VOLTHA_UPLINK_VLAN_START = 333
@@ -294,6 +295,7 @@
       voltha_ctrl = None
       voltha_device = None
       voltha_switch_map = None
+      voltha_preconfigured = False
 
       @classmethod
       def update_apps_version(cls):
@@ -2661,8 +2663,8 @@
                                                           services = services)
                 assert_equal(test_status, True)
           finally:
-                if switch_map is not None:
-                      if olt_configured is True:
+                if self.VOLTHA_TEARDOWN is True and switch_map is not None:
+                      if self.voltha_preconfigured is False and olt_configured is True:
                             self.remove_olt(switch_map)
 
       def test_cord_subscriber_voltha_tls(self):
diff --git a/src/test/setup/olt-test.py b/src/test/setup/olt-test.py
index 287d7a3..e3cf443 100755
--- a/src/test/setup/olt-test.py
+++ b/src/test/setup/olt-test.py
@@ -103,8 +103,14 @@
         manifest = self.args.manifest
         olt_type = self.args.olt_type
         test_type = self.args.test_type
+        disable_teardown = self.args.disable_teardown
+        test_mode = self.args.test_mode
+        disable_cleanup = self.args.disable_cleanup
+        if test_mode is True:
+            disable_cleanup = True
         test_config = { 'VOLTHA_HOST' : self.args.voltha_host,
                         'VOLTHA_OLT_TYPE' : self.args.olt_type,
+                        'VOLTHA_TEARDOWN' : not disable_teardown,
                         }
         if olt_type.startswith('tibit'):
             test_config['VOLTHA_OLT_MAC'] = self.args.olt_arg
@@ -119,8 +125,9 @@
         if self.start_in:
             time.sleep(self.start_in)
 
-        _, status = self.tester.start(manifest = manifest)
-        assert status == httplib.OK, 'Test setup failed with status %d' %status
+        if test_mode is False:
+            _, status = self.tester.start(manifest = manifest)
+            assert status == httplib.OK, 'Test setup failed with status %d' %status
 
         for test in test_type.split(','):
             print('Running test case %s' %(test))
@@ -128,8 +135,9 @@
             if status != httplib.OK:
                 print('Test case %s failed with status code %d' %(test, status))
 
-        print('Cleaning up the test')
-        self.tester.cleanup(manifest = manifest)
+        if disable_cleanup is False:
+            print('Cleaning up the test')
+            self.tester.cleanup(manifest = manifest)
         return 0 if status == httplib.OK else 127
 
 class CordTesterWebServer(object):
@@ -173,6 +181,11 @@
     parser.add_argument('-voltha-host', '--voltha-host', default='172.17.0.1', help = 'VOLTHA host ip')
     parser.add_argument('-olt-type', '--olt-type', default = 'ponsim_olt', help = 'OLT type')
     parser.add_argument('-olt-arg', '--olt-arg', default = '172.17.0.1', help = 'OLT type argument')
+    parser.add_argument('-disable-teardown', '--disable-teardown', action='store_true', help = 'Disable VOLTHA teardown')
+    parser.add_argument('-disable-cleanup', '--disable-cleanup', action='store_true', help = 'Dont cleanup cord-tester')
+    parser.add_argument('-test-mode', '--test-mode', action='store_true',
+                        help = 'Directly run the cord-tester run-test phase without setup and cleanup')
+
     parser.set_defaults(func = run_test)
     args = parser.parse_args()
     res = args.func(args)
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)
diff --git a/src/test/voltha/volthaTest.py b/src/test/voltha/volthaTest.py
index 2152910..74bae52 100644
--- a/src/test/voltha/volthaTest.py
+++ b/src/test/voltha/volthaTest.py
@@ -217,6 +217,7 @@
     VOLTHA_OLT_TYPE = 'ponsim_olt'
     VOLTHA_OLT_MAC = '00:0c:e2:31:12:00'
     VOLTHA_IGMP_ITERATIONS = 100
+    VOLTHA_TEARDOWN = True
     voltha = None
     voltha_attrs = None
     success = True
@@ -1677,7 +1678,9 @@
               config_fake = self.VOLTHA_CONFIG_FAKE,
               olt_app = self.olt_app_file)
         assert_not_equal(ret, None)
-        voltha, device_id, switch_map = ret[0], ret[1], ret[2]
+        voltha, device_id, switch_map, preconfigured = ret[0], ret[1], ret[2], ret[3]
+        if self.VOLTHA_TEARDOWN is False:
+              preconfigured = True
         try:
             log_test.info('Adding subscribers through OLT app')
             self.config_olt(switch_map)
@@ -1686,7 +1689,7 @@
             auth_status = self.tls_flow_check(self.INTF_RX_DEFAULT)
             assert_equal(auth_status, True)
         finally:
-            if switch_map is not None:
+            if switch_map is not None and preconfigured is False:
                 if olt_configured is True:
                     self.remove_olt(switch_map)
                 voltha_teardown(voltha, device_id, switch_map, olt_app = self.olt_app_file)