[NFVBENCH-168] Improve config properties managed after a REST call
[nfvbench.git] / nfvbench / nfvbenchd.py
index ae89e7a..07f1eea 100644 (file)
@@ -15,7 +15,7 @@
 #
 
 import json
-import Queue
+import queue
 from threading import Thread
 import uuid
 
@@ -23,13 +23,12 @@ from flask import Flask
 from flask import jsonify
 from flask import request
 
-from summarizer import NFVBenchSummarizer
+from .summarizer import NFVBenchSummarizer
 
-from log import LOG
-from utils import byteify
-from utils import RunLock
+from .log import LOG
+from .utils import RunLock
 
-from __init__ import __version__
+from .__init__ import __version__
 
 STATUS_OK = 'OK'
 STATUS_ERROR = 'ERROR'
@@ -48,7 +47,7 @@ def result_json(status, message, request_id=None):
 
 
 def load_json(data):
-    return json.loads(json.dumps(data), object_hook=byteify)
+    return json.loads(json.dumps(data))
 
 
 def get_uuid():
@@ -57,7 +56,7 @@ def get_uuid():
 
 class Ctx(object):
     MAXLEN = 5
-    run_queue = Queue.Queue()
+    run_queue = queue.Queue()
     busy = False
     result = None
     results = {}
@@ -101,16 +100,15 @@ class Ctx(object):
                 res = Ctx.results[request_id]
             except KeyError:
                 return None
-
+            # pylint: disable=unsubscriptable-object
             if Ctx.result and request_id == Ctx.result['request_id']:
                 Ctx.result = None
-
-            return res
-        else:
-            res = Ctx.result
-            if res:
-                Ctx.result = None
             return res
+            # pylint: enable=unsubscriptable-object
+        res = Ctx.result
+        if res:
+            Ctx.result = None
+        return res
 
     @staticmethod
     def is_busy():
@@ -159,20 +157,18 @@ def setup_flask():
                 return jsonify(res)
             # result for given request_id not found
             return jsonify(result_json(STATUS_NOT_FOUND, not_found_msg, request_id))
-        else:
-            if Ctx.is_busy():
-                # task still pending, return with request_id
-                return jsonify(result_json(STATUS_PENDING,
-                                           pending_msg,
-                                           Ctx.get_current_request_id()))
-
-            res = Ctx.get_result()
-            if res:
-                return jsonify(res)
-            return jsonify(not_busy_json)
+        if Ctx.is_busy():
+            # task still pending, return with request_id
+            return jsonify(result_json(STATUS_PENDING,
+                                       pending_msg,
+                                       Ctx.get_current_request_id()))
 
-    return app
+        res = Ctx.get_result()
+        if res:
+            return jsonify(res)
+        return jsonify(not_busy_json)
 
+    return app
 
 class WebServer(object):
     """This class takes care of the web server. Caller should simply create an instance
@@ -200,7 +196,7 @@ class WebServer(object):
             # print config
             try:
                 # remove unfilled values as we do not want them to override default values with None
-                config = {k: v for k, v in config.items() if v is not None}
+                config = {k: v for k, v in list(config.items()) if v is not None}
                 with RunLock():
                     if self.fluent_logger:
                         self.fluent_logger.start_new_run()
@@ -214,6 +210,8 @@ class WebServer(object):
             try:
                 summary = NFVBenchSummarizer(results['result'], self.fluent_logger)
                 LOG.info(str(summary))
+                if 'json' in config and 'result' in results and results['status']:
+                    self.nfvbench_runner.save(results['result'])
             except KeyError:
                 # in case of error, 'result' might be missing
                 if 'error_message' in results: