diff options
author | hongyuzhao <zhao.hongyu@zte.com.cn> | 2019-08-21 11:50:28 +0800 |
---|---|---|
committer | hongyuzhao <zhao.hongyu@zte.com.cn> | 2019-08-21 11:54:41 +0800 |
commit | cc4fa321dc9addf090ac889ad8c9a8297f84be6a (patch) | |
tree | 971dbd2112d675e342693e5af6e0dd533b7b9e08 /lcm/lcm/pub/verifyvnfd/verifyvnfd.py | |
parent | 05dc30dacd4ef0b409a26eac02419d3a604d0548 (diff) |
Add vnfd validity check
Change-Id: I9fbdc1c8be274441abccef7f8ca879228037a8db
Issue-ID: VFC-1490
Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
Diffstat (limited to 'lcm/lcm/pub/verifyvnfd/verifyvnfd.py')
-rw-r--r-- | lcm/lcm/pub/verifyvnfd/verifyvnfd.py | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/lcm/lcm/pub/verifyvnfd/verifyvnfd.py b/lcm/lcm/pub/verifyvnfd/verifyvnfd.py index b4ed0408..6fa5ac2b 100644 --- a/lcm/lcm/pub/verifyvnfd/verifyvnfd.py +++ b/lcm/lcm/pub/verifyvnfd/verifyvnfd.py @@ -13,7 +13,9 @@ # limitations under the License. import json +import re import os +import six import logging import jsonschema from lcm.pub.exceptions import NFLCMException @@ -21,6 +23,29 @@ from lcm.pub.exceptions import NFLCMException logger = logging.getLogger(__name__) +def _format_validation_error(error): + """ + :param error: validation error to format + :type error: jsonchema.exceptions.ValidationError + :returns: string representation of the validation error + :rtype: str + """ + match = re.search("(.+) is a required property", error.message) + if match: + message = 'Error: missing required property {}.'.format( + match.group(1)) + else: + message = 'Error: {}\n'.format(error.message) + if len(error.absolute_path) > 0: + message += 'Path: {}\n'.format( + '.'.join( + [six.text_type(path) + for path in error.absolute_path])) + message += 'Value: {}'.format(json.dumps(error.instance)) + + return message + + def verify(new_vnfd): errors_found = [] vnfd_schema_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "vnf_vnfd_schema.json") @@ -28,8 +53,8 @@ def verify(new_vnfd): vnfd_schema = json.load(fvnfd_schema) vnfd_validator = jsonschema.validators.Draft4Validator(schema=vnfd_schema) for error in vnfd_validator.iter_errors(new_vnfd): - logger.error("vnfd verify fail:%s" % error) - errors_found.append(error) - if len(errors_found) > 0: - raise NFLCMException(errors_found) - return errors_found + logger.error("vnfd verify fail,%s" % _format_validation_error(error)) + errors_found.append(_format_validation_error(error)) + if len(errors_found) > 0: + raise NFLCMException(errors_found) + return True |