3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 r"""CSIT PAPI Provider
18 TODO: Add description.
23 Request/reply or dump:
25 vpp_papi_provider.py \
27 --data '[{"api_name": "show_version", "api_args": {}}]'
31 vpp_papi_provider.py \
33 --data '[["^/if", "/err/ip4-input", "/sys/node/ip4-input"], ["^/if"]]'
44 CLIENT_NAME = 'csit_papi'
47 # Sphinx creates auto-generated documentation by importing the python source
48 # files and collecting the docstrings from them. The NO_VPP_PAPI flag allows
49 # the vpp_papi_provider.py file to be importable without having to build
50 # the whole vpp api if the user only wishes to generate the test documentation.
53 do_import = False if os.getenv("NO_VPP_PAPI") == "1" else True
59 # Find the directory where the modules are installed. The directory depends
61 # TODO: Find a better way to import papi modules.
64 for root, dirs, files in os.walk('/usr/lib'):
66 if name == 'vpp_papi.py':
67 modules_path = os.path.split(root)[0]
70 sys.path.append(modules_path)
71 from vpp_papi import VPP
72 from vpp_papi.vpp_stats import VPPStats
74 raise RuntimeError('vpp_papi module not found')
77 def _convert_reply(api_r):
78 """Process API reply / a part of API reply for smooth converting to
81 It is used only with 'request' and 'dump' methods.
83 Apply binascii.hexlify() method for string values.
85 TODO: Implement complex solution to process of replies.
87 :param api_r: API reply.
88 :type api_r: Vpp_serializer reply object (named tuple)
89 :returns: Processed API reply / a part of API reply.
92 unwanted_fields = ['count', 'index', 'context']
95 reply_key = repr(api_r).split('(')[0]
97 for item in dir(api_r):
98 if not item.startswith('_') and item not in unwanted_fields:
99 attr_value = getattr(api_r, item)
100 if isinstance(attr_value, list) or isinstance(attr_value, dict):
102 elif hasattr(attr_value, '__int__'):
103 value = int(attr_value)
104 elif hasattr(attr_value, '__str__'):
105 value = binascii.hexlify(str(attr_value))
106 # Next handles parameters not supporting preferred integer or string
107 # representation to get it logged
108 elif hasattr(attr_value, '__repr__'):
109 value = repr(attr_value)
112 reply_value[item] = value
113 reply_dict[reply_key] = reply_value
117 def process_json_request(args):
118 """Process the request/reply and dump classes of VPP API methods.
120 :param args: Command line arguments passed to VPP PAPI Provider.
121 :type args: ArgumentParser
122 :returns: JSON formatted string.
124 :raises RuntimeError: If PAPI command error occurs.
129 except Exception as err:
130 raise RuntimeError('PAPI init failed:\n{err}'.format(err=repr(err)))
134 def process_value(val):
135 if isinstance(val, dict):
137 for val_k, val_v in val.iteritems():
138 val_dict[str(val_k)] = process_value(val_v)
140 elif isinstance(val, unicode):
141 return binascii.unhexlify(val)
142 elif isinstance(val, int):
147 json_data = json.loads(args.data)
148 vpp.connect(CLIENT_NAME)
149 for data in json_data:
150 api_name = data['api_name']
151 api_args_unicode = data['api_args']
152 api_reply = dict(api_name=api_name)
154 for a_k, a_v in api_args_unicode.items():
155 api_args[str(a_k)] = process_value(a_v)
157 papi_fn = getattr(vpp.api, api_name)
158 rep = papi_fn(**api_args)
160 if isinstance(rep, list):
161 converted_reply = list()
163 converted_reply.append(_convert_reply(r))
165 converted_reply = _convert_reply(rep)
167 api_reply['api_reply'] = converted_reply
168 reply.append(api_reply)
169 except (AttributeError, ValueError) as err:
171 raise RuntimeError('PAPI command {api}({args}) input error:\n{err}'.
175 except Exception as err:
177 raise RuntimeError('PAPI command {api}({args}) error:\n{exc}'.
183 return json.dumps(reply)
186 def process_stats(args):
187 """Process the VPP Stats.
189 :param args: Command line arguments passed to VPP PAPI Provider.
190 :type args: ArgumentParser
191 :returns: JSON formatted string.
193 :raises RuntimeError: If PAPI command error occurs.
197 stats = VPPStats(args.socket)
198 except Exception as err:
199 raise RuntimeError('PAPI init failed:\n{err}'.format(err=repr(err)))
201 json_data = json.loads(args.data)
205 for path in json_data:
206 directory = stats.ls(path)
207 data = stats.dump(directory)
211 return json.dumps(reply)
212 except UnicodeDecodeError as err:
213 raise RuntimeError('PAPI reply {reply} error:\n{exc}'.format(
214 reply=reply, exc=repr(err)))
218 """Main function for the Python API provider.
221 # The functions which process different types of VPP Python API methods.
222 process_request = dict(
223 request=process_json_request,
224 dump=process_json_request,
228 parser = argparse.ArgumentParser(
229 formatter_class=argparse.RawDescriptionHelpFormatter,
231 parser.add_argument("-m", "--method",
233 choices=[str(key) for key in process_request.keys()],
234 help="Specifies the VPP API methods: 1. request - "
235 "simple request / reply; 2. dump - dump function;"
236 "3. stats - VPP statistics.")
237 parser.add_argument("-d", "--data",
239 help="If the method is 'request' or 'dump', data is a "
240 "JSON string (list) containing API name(s) and "
241 "its/their input argument(s). "
242 "If the method is 'stats', data is a JSON string "
243 "containing the list of path(s) to the required "
245 parser.add_argument("-s", "--socket",
246 default="/var/run/vpp/stats.sock",
247 help="A file descriptor over the VPP stats Unix domain "
248 "socket. It is used only if method=='stats'.")
250 args = parser.parse_args()
252 return process_request[args.method](args)
255 if __name__ == '__main__':
256 sys.stdout.write(main())