X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Ftools%2Fpapi%2Fvpp_papi_provider.py;h=676f5491ddb5ba9104a2f97e5a1293871de3dd3c;hb=ac02e01bd4b0625e91a8eb3fa0f13f6f32f74527;hp=ee0d538fba47fea4aa8d939d1c05d39cf115c7cb;hpb=fe1975eb1ac994df1bd759deda7154bb7dd9d7a7;p=csit.git diff --git a/resources/tools/papi/vpp_papi_provider.py b/resources/tools/papi/vpp_papi_provider.py index ee0d538fba..676f5491dd 100755 --- a/resources/tools/papi/vpp_papi_provider.py +++ b/resources/tools/papi/vpp_papi_provider.py @@ -91,25 +91,39 @@ def _convert_reply(api_r): """ unwanted_fields = ['count', 'index', 'context'] + def process_value(val): + """Process value. + + :param val: Value to be processed. + :type val: object + :returns: Processed value. + :rtype: dict or str or int + """ + if isinstance(val, dict): + for val_k, val_v in val.iteritems(): + val[str(val_k)] = process_value(val_v) + return val + elif isinstance(val, list): + for idx, val_l in enumerate(val): + val[idx] = process_value(val_l) + return val + elif hasattr(val, '__int__'): + return int(val) + elif hasattr(val, '__str__'): + return binascii.hexlify(str(val)) + # Next handles parameters not supporting preferred integer or string + # representation to get it logged + elif hasattr(val, '__repr__'): + return repr(val) + else: + return val + reply_dict = dict() reply_key = repr(api_r).split('(')[0] reply_value = dict() for item in dir(api_r): if not item.startswith('_') and item not in unwanted_fields: - 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_value[item] = process_value(getattr(api_r, item)) reply_dict[reply_key] = reply_value return reply_dict @@ -132,11 +146,21 @@ def process_json_request(args): reply = list() def process_value(val): + """Process value. + + :param val: Value to be processed. + :type val: object + :returns: Processed value. + :rtype: dict or str or int + """ 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 + val[str(val_k)] = process_value(val_v) + return val + elif isinstance(val, list): + for idx, val_l in enumerate(val): + val[idx] = process_value(val_l) + return val elif isinstance(val, unicode): return binascii.unhexlify(val) elif isinstance(val, int): @@ -214,6 +238,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. """ @@ -222,7 +273,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(