VAT-to-PAPI: VPPCounters
[csit.git] / resources / tools / papi / vpp_papi_provider.py
index cec8197..ded3c30 100755 (executable)
@@ -131,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:
@@ -139,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)
@@ -202,6 +214,33 @@ def process_stats(args):
             reply=reply, exc=repr(err)))
 
 
+def process_stats_request(args):
+    """Process the VPP Stats requests.
+
+    :param args: Command line arguments passed to VPP PAPI Provider.
+    :type args: ArgumentParser
+    :returns: JSON formatted string.
+    :rtype: str
+    :raises RuntimeError: If PAPI command error occurs.
+    """
+
+    try:
+        stats = VPPStats(args.socket)
+    except Exception as err:
+        raise RuntimeError('PAPI init failed:\n{err}'.format(err=repr(err)))
+
+    try:
+        json_data = json.loads(args.data)
+    except ValueError as err:
+        raise RuntimeError('Input json string is invalid:\n{err}'.
+                           format(err=repr(err)))
+
+    papi_fn = getattr(stats, json_data["api_name"])
+    reply = papi_fn(**json_data.get("api_args", {}))
+
+    return json.dumps(reply)
+
+
 def main():
     """Main function for the Python API provider.
     """
@@ -210,7 +249,8 @@ def main():
     process_request = dict(
         request=process_json_request,
         dump=process_json_request,
-        stats=process_stats
+        stats=process_stats,
+        stats_request=process_stats_request
     )
 
     parser = argparse.ArgumentParser(