A proof of concept web configuration server for cord-tester using flask (thin python webframework)
With this, you can pretty much load the test case configure before running cord-tester.
Then run the cord-tester and finally restore.
To try:
Start the web server first:
./webserver-run.py
Then run a curl get to retrieve test case config.
./webserver-get.curl
Then post the test case config (example for cluster test):
./webserver-post.curl
If you want to restore, answer y and it would restore the config back.
This allows us to load test case configuration profiles before running automation with the config.
And finally restore the config on automation complete.

Change-Id: I90c603856761fbe2ee9c0fad23d10626899d35d3
diff --git a/src/test/setup/flask-requirements.txt b/src/test/setup/flask-requirements.txt
new file mode 100644
index 0000000..62e669e
--- /dev/null
+++ b/src/test/setup/flask-requirements.txt
@@ -0,0 +1,12 @@
+flask
+flask-login
+flask-openid
+flask-mail
+flask-sqlalchemy
+sqlalchemy-migrate
+flask-whooshalchemy
+flask-wtf
+flask-babel
+guess_language
+flipflop
+coverage
diff --git a/src/test/setup/webserver-get.curl b/src/test/setup/webserver-get.curl
new file mode 100755
index 0000000..bdaf7f8
--- /dev/null
+++ b/src/test/setup/webserver-get.curl
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+# example cord test web get to retrieve config from voltha test case
+
+curl  -H "Content-Type: application/json" -X GET -d '{"test_case" : "voltha"}' http://localhost:5000/get
\ No newline at end of file
diff --git a/src/test/setup/webserver-post.curl b/src/test/setup/webserver-post.curl
new file mode 100755
index 0000000..2ec2763
--- /dev/null
+++ b/src/test/setup/webserver-post.curl
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+# example cord webserver post request to update config for cluster test
+
+cat > /tmp/testconfig.json <<EOF
+{
+"test_case" : "cluster",
+ "config" :
+ {
+      "TLS_TIMEOUT" : 10,
+      "ITERATIONS" : 50
+ }
+}
+EOF
+curl -H "Content-Type: application/json" -X POST -d @/tmp/testconfig.json http://localhost:5000/update
+rm -f /tmp/testconfig.json
+read -p "Do you want to restore the config?" ans
+case "$ans" in
+  [Yy]*)
+         echo "Restoring test case config"
+         curl -H "Content-Type: application/json" -X POST -d '{ "test_case" : "cluster" }' http://localhost:5000/restore
+         ;;
+esac
diff --git a/src/test/setup/webserver-run.py b/src/test/setup/webserver-run.py
new file mode 100755
index 0000000..51f4d64
--- /dev/null
+++ b/src/test/setup/webserver-run.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+from webserver import app
+app.run(debug=True, port = 5000)
diff --git a/src/test/setup/webserver/__init__.py b/src/test/setup/webserver/__init__.py
new file mode 100644
index 0000000..2613a3d
--- /dev/null
+++ b/src/test/setup/webserver/__init__.py
@@ -0,0 +1,3 @@
+from flask import Flask
+app = Flask(__name__)
+from webserver import cordTestConfig
diff --git a/src/test/setup/webserver/cordTestConfig.py b/src/test/setup/webserver/cordTestConfig.py
new file mode 100644
index 0000000..33bdc1e
--- /dev/null
+++ b/src/test/setup/webserver/cordTestConfig.py
@@ -0,0 +1,91 @@
+from webserver import app
+from flask import request, jsonify
+import httplib
+import json
+import os
+import sys
+
+class CordTesterWebConfig(object):
+    our_path = os.path.dirname(os.path.realpath(__file__))
+    test_base = os.path.realpath(os.path.join(our_path, '..', '..'))
+
+    def __init__(self, test_case):
+        self.test_case = test_case
+        self.test_path = None
+        self.test_config = None
+        test_path = os.path.join(self.test_base, self.test_case)
+        if os.path.isdir(test_path):
+            self.test_path = test_path
+            self.test_config = os.path.join(self.test_path, '{}Test.json'.format(self.test_case))
+
+    def update(self, config):
+        cur_config = {}
+        if self.test_config:
+            if os.access(self.test_config, os.F_OK):
+                with open(self.test_config, 'r') as f:
+                    cur_config = json.load(f)
+                os.rename(self.test_config, '{}.save'.format(self.test_config))
+            for k, v in config.iteritems():
+                cur_config[k] = v
+                with open(self.test_config, 'w') as f:
+                    json.dump(cur_config, f, indent = 4)
+            return True
+        return False
+
+    def restore(self):
+        if self.test_config:
+            if os.access(self.test_config, os.F_OK):
+                restore_file = '{}.save'.format(self.test_config)
+                if os.access(restore_file, os.F_OK):
+                    os.rename(restore_file, self.test_config)
+                return True
+        return False
+
+    def get(self):
+        cur_config = {}
+        if self.test_config:
+            if os.access(self.test_config, os.F_OK):
+                with open(self.test_config) as f:
+                    cur_config = json.load(f)
+        return cur_config
+
+@app.route('/')
+@app.route('/index')
+def index():
+    return 'Welcome to Cord Tester configuration page'
+
+@app.route('/get')
+def get():
+    data = request.get_json(force = True)
+    test_case = data.get('test_case', None)
+    if test_case:
+        cordWeb = CordTesterWebConfig(test_case)
+        config = cordWeb.get()
+        return jsonify(config)
+    return ('', httplib.NOT_FOUND)
+
+@app.route('/update', methods = ['POST'])
+def update():
+    data = request.get_json(force = True)
+    test_case = data.get('test_case', None)
+    config = data.get('config', None)
+    response = ('', httplib.NOT_FOUND)
+    if test_case:
+        cordWeb = CordTesterWebConfig(test_case)
+        status = cordWeb.update(config)
+        if status:
+            response = ('', httplib.OK)
+
+    return response
+
+@app.route('/restore', methods = ['POST'])
+def restore():
+    data = request.get_json(force = True)
+    test_case = data.get('test_case', None)
+    response = ('', httplib.NOT_FOUND)
+    if test_case:
+        cordWeb = CordTesterWebConfig(test_case)
+        status = cordWeb.restore()
+        if status:
+            response = ('', httplib.OK)
+    return response