+def process_stats(args):
+ """Process the VPP Stats.
+
+ The reply contains single item covering all paths.
+
+ :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(f"PAPI init failed:\n{err!r}")
+
+ paths = json.loads(args.data)
+ directory = stats.ls(paths)
+ reply = [stats.dump(directory)]
+
+ try:
+ return json.dumps(reply)
+ except UnicodeDecodeError as err:
+ raise RuntimeError(f"PAPI reply {reply} error:\n{err!r}")
+
+
+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(f"PAPI init failed:\n{err!r}")
+
+ try:
+ json_data = json.loads(args.data)
+ except ValueError as err:
+ raise RuntimeError(f"Input json string is invalid:\n{err!r}")
+
+ papi_fn = getattr(stats, json_data[u"api_name"])
+ reply = papi_fn(**json_data.get(u"api_args", {}))
+
+ return json.dumps(reply)
+
+
+def main():
+ """Main function for the Python API provider.
+ """
+
+ # The functions which process different types of VPP Python API methods.
+ process_request = dict(
+ request=process_json_request,
+ dump=process_json_request,
+ stats=process_stats,
+ stats_request=process_stats_request
+ )
+
+ parser = argparse.ArgumentParser(
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ description=__doc__
+ )
+ parser.add_argument(
+ u"-m", u"--method", required=True,
+ choices=[str(key) for key in process_request.keys()],
+ help=u"Specifies the VPP API methods: "
+ u"1. request - simple request / reply; "
+ u"2. dump - dump function;"
+ u"3. stats - VPP statistics."
+ )
+ parser.add_argument(
+ u"-d", u"--data", required=True,
+ help=u"If the method is 'request' or 'dump', data is a JSON string "
+ u"(list) containing API name(s) and its/their input argument(s). "
+ u"If the method is 'stats', data is a JSON string containing t"
+ u"he list of path(s) to the required data."
+ )
+ parser.add_argument(
+ u"-s", u"--socket", default=u"/var/run/vpp/stats.sock",
+ help=u"A file descriptor over the VPP stats Unix domain socket. "
+ u"It is used only if method=='stats'."
+ )
+
+ args = parser.parse_args()
+
+ return process_request[args.method](args)
+
+
+if __name__ == u"__main__":