+def process_stats(args):
+ """Process the VPP Stats.
+
+ :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)))
+
+ json_data = json.loads(args.data)
+
+ reply = list()
+
+ for path in json_data:
+ directory = stats.ls(path)
+ data = stats.dump(directory)
+ reply.append(data)
+
+ 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():
+ """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
+ )
+
+ parser = argparse.ArgumentParser(
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ description=__doc__)
+ parser.add_argument("-m", "--method",
+ required=True,
+ choices=[str(key) for key in process_request.keys()],
+ help="Specifies the VPP API methods: 1. request - "
+ "simple request / reply; 2. dump - dump function;"
+ "3. stats - VPP statistics.")
+ parser.add_argument("-d", "--data",
+ required=True,
+ help="If the method is 'request' or 'dump', data is a "
+ "JSON string (list) containing API name(s) and "
+ "its/their input argument(s). "
+ "If the method is 'stats', data is a JSON string "
+ "containing the list of path(s) to the required "
+ "data.")
+ parser.add_argument("-s", "--socket",
+ default="/var/run/vpp/stats.sock",
+ help="A file descriptor over the VPP stats Unix domain "
+ "socket. It is used only if method=='stats'.")
+
+ args = parser.parse_args()
+
+ return process_request[args.method](args)
+
+