Option to run cord-tester setup in foreground.
Delete relay configuration instead of restarting onos after tests.
Make sure prerequisites can be run on other ubuntu distros

Change-Id: Ic04a5dab41b013088cda651afe32abae8030c8d8
diff --git a/src/test/dhcprelay/dhcprelayTest.py b/src/test/dhcprelay/dhcprelayTest.py
index ea0ff47..9b8e7c4 100644
--- a/src/test/dhcprelay/dhcprelayTest.py
+++ b/src/test/dhcprelay/dhcprelayTest.py
@@ -68,6 +68,7 @@
     total_failure = 0
     #just in case we want to reset ONOS to default network cfg after relay tests
     onos_restartable = bool(int(os.getenv('ONOS_RESTART', 0)))
+    configs = {}
 
     @classmethod
     def setUpClass(cls):
@@ -131,9 +132,11 @@
     @classmethod
     def dhcp_relay_cleanup(cls):
         ##reset the ONOS port configuration back to default
-        if cls.onos_restartable is True:
-            log.info('Cleaning up dhcp relay config by restarting ONOS with default network cfg')
-            return cord_test_onos_restart(config = {})
+        for config in cls.configs.items():
+            OnosCtrl.delete(config)
+        # if cls.onos_restartable is True:
+        #     log.info('Cleaning up dhcp relay config by restarting ONOS with default network cfg')
+        #     return cord_test_onos_restart(config = {})
 
     @classmethod
     def onos_load_config(cls, config):
@@ -158,6 +161,7 @@
             interface_list.append(interface_map)
 
         cls.onos_load_config(interface_dict)
+        cls.configs['interface_config'] = interface_dict
 
     @classmethod
     def onos_dhcp_relay_load(cls, server_ip, server_mac):
@@ -171,6 +175,7 @@
                              }
                      }
         cls.onos_load_config(dhcp_dict)
+        cls.configs['relay_config'] = dhcp_dict
 
     @classmethod
     def get_host_ip(cls, port):
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 4efe7e4..1db4c2f 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -714,8 +714,8 @@
 
     #Finally start the test server and daemonize
     try:
-        cord_test_server_start(daemonize = True, cord_test_host = ip, cord_test_port = port,
-                               onos_cord = onos_cord)
+        cord_test_server_start(daemonize = not args.foreground, cord_test_host = ip, cord_test_port = port,
+                               onos_cord = onos_cord, foreground = args.foreground)
     except socket.error, e:
         #the test agent address could be remote or already running. Exit gracefully
         sys.exit(0)
@@ -961,7 +961,8 @@
     parser_setup.add_argument('-i', '--identity-file', default=identity_file_default,
                               type=str, help='ssh identity file to access compute nodes from test container')
     parser_setup.add_argument('-n', '--onos-instances', default=1, type=int,
-                            help='Specify number of test onos instances to spawn')
+                              help='Specify number of test onos instances to spawn')
+    parser_setup.add_argument('-f', '--foreground', action='store_true', help='Run in foreground')
     parser_setup.set_defaults(func=setupCordTester)
 
     parser_xos = subparser.add_parser('xos', help='Building xos into cord tester environment')
diff --git a/src/test/setup/prerequisites.sh b/src/test/setup/prerequisites.sh
index efa374c..1bfb6dd 100755
--- a/src/test/setup/prerequisites.sh
+++ b/src/test/setup/prerequisites.sh
@@ -1,6 +1,7 @@
 #!/usr/bin/env bash
 apt-get update
 on_cord=0
+release=$(lsb_release -cs)
 if [ "$1" = "--cord" ]; then
     echo "Skipping installation of Docker and ONOS"
     on_cord=1
@@ -9,7 +10,7 @@
     apt-get -y install apt-transport-https ca-certificates
     apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
     if [ ! -f /etc/apt/sources.list.d/docker.list ]; then
-        echo deb https://apt.dockerproject.org/repo ubuntu-trusty main |  tee /etc/apt/sources.list.d/docker.list
+        echo deb https://apt.dockerproject.org/repo ubuntu-$release main |  tee /etc/apt/sources.list.d/docker.list
     fi
     apt-get update
     apt-get purge lxc-docker || true
diff --git a/src/test/utils/CordTestServer.py b/src/test/utils/CordTestServer.py
index c882f58..fe676fe 100644
--- a/src/test/utils/CordTestServer.py
+++ b/src/test/utils/CordTestServer.py
@@ -123,8 +123,11 @@
         return 'DONE'
 
 @nottest
-def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST,
-                           cord_test_port = CORD_TEST_PORT, onos_cord = None):
+def cord_test_server_start(daemonize = True,
+                           cord_test_host = CORD_TEST_HOST,
+                           cord_test_port = CORD_TEST_PORT,
+                           onos_cord = None,
+                           foreground=False):
     server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
     server.register_instance(CordTestServer())
     CordTestServer.onos_cord = onos_cord
@@ -135,10 +138,16 @@
             reinitContainerClients()
             server.serve_forever()
     else:
-        task = threading.Thread(target = server.serve_forever)
-        ##terminate when main thread exits
-        task.daemon = True
-        task.start()
+        if foreground:
+            try:
+                server.serve_forever()
+            except KeyboardInterrupt:
+                return server
+        else:
+            task = threading.Thread(target = server.serve_forever)
+            ##terminate when main thread exits
+            task.daemon = True
+            task.start()
     return server
 
 @nottest
diff --git a/src/test/utils/OnosCtrl.py b/src/test/utils/OnosCtrl.py
index 5e6dcdf..83cdedc 100644
--- a/src/test/utils/OnosCtrl.py
+++ b/src/test/utils/OnosCtrl.py
@@ -56,7 +56,7 @@
         self.auth = ('karaf', 'karaf')
 
     @classmethod
-    def config(cls, config,controller=None):
+    def config(cls, config, controller=None):
         if config is not None:
             json_data = json.dumps(config)
 	    if controller is None:
@@ -69,6 +69,20 @@
             return resp.ok, resp.status_code
         return False, 400
 
+    @classmethod
+    def delete(cls, config, controller=None):
+        if config:
+            json_data = json.dumps(config)
+	    if controller is None:
+	        print('default Onos config url is %s'%cls.cfg_url)
+                resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data)
+	    else:
+		cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
+		print('non-default Onos config url is %s'%cfg_url)
+	        resp = requests.delete(cfg_url, auth = cls.auth, data = json_data)
+            return resp.ok, resp.status_code
+        return False, 400
+
     def activate(self):
         resp = requests.post(self.app_url + '/active', auth = self.auth)
         return resp.ok, resp.status_code