CSIT-1468: InterfaceUtil migration from VAT to PAPI
[csit.git] / resources / tools / papi / vpp_papi_provider.py
index 299cd2c..ee0d538 100755 (executable)
@@ -96,7 +96,20 @@ def _convert_reply(api_r):
     reply_value = dict()
     for item in dir(api_r):
         if not item.startswith('_') and item not in unwanted_fields:
-            reply_value[item] = getattr(api_r, item)
+            attr_value = getattr(api_r, item)
+            if isinstance(attr_value, list) or isinstance(attr_value, dict):
+                value = attr_value
+            elif hasattr(attr_value, '__int__'):
+                value = int(attr_value)
+            elif hasattr(attr_value, '__str__'):
+                value = binascii.hexlify(str(attr_value))
+            # Next handles parameters not supporting preferred integer or string
+            # representation to get it logged
+            elif hasattr(attr_value, '__repr__'):
+                value = repr(attr_value)
+            else:
+                value = attr_value
+            reply_value[item] = value
     reply_dict[reply_key] = reply_value
     return reply_dict
 
@@ -118,6 +131,19 @@ def process_json_request(args):
 
     reply = list()
 
+    def process_value(val):
+        if isinstance(val, dict):
+            val_dict = dict()
+            for val_k, val_v in val.iteritems():
+                val_dict[str(val_k)] = process_value(val_v)
+            return val_dict
+        elif isinstance(val, unicode):
+            return binascii.unhexlify(val)
+        elif isinstance(val, int):
+            return val
+        else:
+            return str(val)
+
     json_data = json.loads(args.data)
     vpp.connect(CLIENT_NAME)
     for data in json_data:
@@ -126,8 +152,7 @@ def process_json_request(args):
         api_reply = dict(api_name=api_name)
         api_args = dict()
         for a_k, a_v in api_args_unicode.items():
-            value = binascii.unhexlify(a_v) if isinstance(a_v, unicode) else a_v
-            api_args[str(a_k)] = value if isinstance(value, int) else str(value)
+            api_args[str(a_k)] = process_value(a_v)
         try:
             papi_fn = getattr(vpp.api, api_name)
             rep = papi_fn(**api_args)
@@ -182,7 +207,11 @@ def process_stats(args):
         data = stats.dump(directory)
         reply.append(data)
 
-    return json.dumps(reply)
+    try:
+        return json.dumps(reply)
+    except UnicodeDecodeError as err:
+        raise RuntimeError('PAPI reply {reply} error:\n{exc}'.format(
+            reply=reply, exc=repr(err)))
 
 
 def main():