-#!/usr/bin/env python
+#!/usr/bin/env python2
-# Copyright (c) 2018 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
import argparse
import binascii
-import fnmatch
import json
import os
import sys
-sys.path.append('/tmp/openvpp-testing')
-try:
- from resources.libraries.python.PapiErrors import *
-except:
- raise
-
# Sphinx creates auto-generated documentation by importing the python source
# files and collecting the docstrings from them. The NO_VPP_PAPI flag allows
# the vpp_papi_provider.py file to be importable without having to build
sys.path.append(modules_path)
from vpp_papi import VPP
else:
- raise PapiInitError('vpp_papi module not found')
+ raise RuntimeError('vpp_papi module not found')
# client name
CLIENT_NAME = 'csit_papi'
-def papi_init(vpp_json_dir='/usr/share/vpp/api/'):
+def papi_init():
"""Construct a VPP instance from VPP JSON API files.
:param vpp_json_dir: Directory containing all the JSON API files. If VPP is
:raises PapiJsonFileError: If no api.json file found.
:raises PapiInitError: If PAPI initialization failed.
"""
- # construct a list of all the json api files
- jsonfiles = []
- for root, dirnames, filenames in os.walk(vpp_json_dir):
- for filename in fnmatch.filter(filenames, '*.api.json'):
- jsonfiles.append(os.path.join(vpp_json_dir, filename))
- if not jsonfiles:
- raise PapiJsonFileError(
- 'No json api files found in location {dir}'.format(
- dir=vpp_json_dir))
-
try:
- vpp = VPP(jsonfiles)
+ vpp = VPP()
return vpp
except Exception as err:
- raise PapiInitError('PAPI init failed:\n{exc}'.format(exc=repr(err)))
+ raise RuntimeError('PAPI init failed:\n{err}'.format(err=repr(err)))
def papi_connect(vpp_client, name='vpp_api'):
def papi_run(vpp_client, api_name, api_args):
- """api_name
+ """Run PAPI.
:param vpp_client: VPP instance.
:param api_name: VPP API name.
JSON string.
Apply binascii.hexlify() method for string values.
+
:param api_r: API reply.
:type api_r: Vpp_serializer reply object (named tuple)
:returns: Processed API reply / a part of API reply.
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)
- value = binascii.hexlify(attr_value) \
- if isinstance(attr_value, str) else attr_value
+ # attr_value = getattr(api_r, item)
+ # value = binascii.hexlify(attr_value) \
+ # if isinstance(attr_value, str) else attr_value
+ value = getattr(api_r, item)
reply_value[item] = value
reply_dict[reply_key] = reply_value
return reply_dict
def main():
"""Main function for the Python API provider.
- :raises PapiCommandInputError: If invalid attribute name or invalid value is
- used in API call.
- :raises PapiCommandError: If PAPI command(s) execution failed.
+ :raises RuntimeError: If invalid attribute name or invalid value is
+ used in API call or if PAPI command(s) execution failed.
"""
parser = argparse.ArgumentParser()
help="Directory containing all vpp json api files.")
args = parser.parse_args()
json_string = args.json_data
- vpp_json_dir = args.json_dir
- vpp = papi_init(vpp_json_dir=vpp_json_dir)
+ vpp = papi_init()
reply = list()
json_data = json.loads(json_string)
api_args_unicode = data['api_args']
api_reply = dict(api_name=api_name)
api_args = dict()
- for a_k, a_v in api_args_unicode.iteritems():
+ 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
try:
reply.append(api_reply)
except (AttributeError, ValueError) as err:
papi_disconnect(vpp)
- raise PapiCommandInputError(
- 'PAPI command {api}({args}) input error:\n{exc}'.format(
- api=api_name, args=api_args), exc=repr(err))
+ raise RuntimeError('PAPI command {api}({args}) input error:\n{err}'.
+ format(api=api_name,
+ args=api_args,
+ err=repr(err)))
except Exception as err:
papi_disconnect(vpp)
- raise PapiCommandError(
- 'PAPI command {api}({args}) error:\n{exc}'.format(
- api=api_name, args=api_args), exc=repr(err))
+ raise RuntimeError('PAPI command {api}({args}) error:\n{exc}'.
+ format(api=api_name,
+ args=api_args,
+ exc=repr(err)))
papi_disconnect(vpp)
return json.dumps(reply)