From 7407c6c4cb09a80ec35a48716d4436ff98aba23f Mon Sep 17 00:00:00 2001 From: shangxdy Date: Tue, 29 Aug 2017 12:45:34 +0800 Subject: Add parameter validation in design time Add validation for SDC from ONAP when use parse in design time: ignore parameter validation and only print warning message. JIRA: PARSER-142 Change-Id: I9372f20816f2acbda83c5f1e4f673f2ffc3c2e94 Signed-off-by: shangxdy --- tosca2heat/tosca-parser/toscaparser/common/exception.py | 7 +++++++ tosca2heat/tosca-parser/toscaparser/shell.py | 15 +++++++++++---- tosca2heat/tosca-parser/toscaparser/tosca_template.py | 12 ++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/tosca2heat/tosca-parser/toscaparser/common/exception.py b/tosca2heat/tosca-parser/toscaparser/common/exception.py index 13ccabd..9fb331d 100644 --- a/tosca2heat/tosca-parser/toscaparser/common/exception.py +++ b/tosca2heat/tosca-parser/toscaparser/common/exception.py @@ -205,6 +205,13 @@ class ExceptionCollector(object): else: raise exception + @staticmethod + def removeException(exception_type): + if ExceptionCollector.collecting: + for i, e in enumerate(ExceptionCollector.exceptions): + if isinstance(e, exception_type): + del ExceptionCollector.exceptions[i] + @staticmethod def exceptionsCaught(): return len(ExceptionCollector.exceptions) > 0 diff --git a/tosca2heat/tosca-parser/toscaparser/shell.py b/tosca2heat/tosca-parser/toscaparser/shell.py index 1d98f1a..0256dfc 100644 --- a/tosca2heat/tosca-parser/toscaparser/shell.py +++ b/tosca2heat/tosca-parser/toscaparser/shell.py @@ -49,23 +49,30 @@ class ParserShell(object): required=True, help=_('YAML template or CSAR file to parse.')) + parser.add_argument('-nrpv', dest='no_required_paras_valid', + action='store_true', default=False, + help=_('Ignore input parameter validation ' + 'when parse template.')) + return parser def main(self, argv): parser = self.get_parser(argv) (args, extra_args) = parser.parse_known_args(argv) path = args.template_file + nrpv = args.no_required_paras_valid if os.path.isfile(path): - self.parse(path) + self.parse(path, no_required_paras_valid=nrpv) elif toscaparser.utils.urlutils.UrlUtils.validate_url(path): - self.parse(path, False) + self.parse(path, False, no_required_paras_valid=nrpv) else: raise ValueError(_('"%(path)s" is not a valid file.') % {'path': path}) - def parse(self, path, a_file=True): + def parse(self, path, a_file=True, no_required_paras_valid=False): output = None - tosca = ToscaTemplate(path, None, a_file) + tosca = ToscaTemplate(path, None, a_file, + no_required_paras_valid=no_required_paras_valid) version = tosca.version if tosca.version: diff --git a/tosca2heat/tosca-parser/toscaparser/tosca_template.py b/tosca2heat/tosca-parser/toscaparser/tosca_template.py index 9e84f3d..3301999 100644 --- a/tosca2heat/tosca-parser/toscaparser/tosca_template.py +++ b/tosca2heat/tosca-parser/toscaparser/tosca_template.py @@ -18,6 +18,7 @@ from copy import deepcopy from toscaparser.common.exception import ExceptionCollector from toscaparser.common.exception import InvalidTemplateVersion from toscaparser.common.exception import MissingRequiredFieldError +from toscaparser.common.exception import MissingRequiredParameterError from toscaparser.common.exception import UnknownFieldError from toscaparser.common.exception import ValidationError from toscaparser.elements.entity_type import update_definitions @@ -65,7 +66,8 @@ class ToscaTemplate(object): '''Load the template data.''' def __init__(self, path=None, parsed_params=None, a_file=True, - yaml_dict_tpl=None, sub_mapped_node_template=None): + yaml_dict_tpl=None, sub_mapped_node_template=None, + no_required_paras_valid=False): if sub_mapped_node_template is None: ExceptionCollector.start() self.a_file = a_file @@ -75,6 +77,7 @@ class ToscaTemplate(object): self.sub_mapped_node_template = sub_mapped_node_template self.nested_tosca_tpls_with_topology = {} self.nested_tosca_templates_with_topology = [] + self.no_required_paras_valid = no_required_paras_valid if path: self.input_path = path self.path = self._get_path(path) @@ -236,7 +239,8 @@ class ToscaTemplate(object): nested_template = ToscaTemplate( path=fname, parsed_params=parsed_params, yaml_dict_tpl=tosca_tpl, - sub_mapped_node_template=nodetemplate) + sub_mapped_node_template=nodetemplate, + no_required_paras_valid=self.no_required_paras_valid) if nested_template._has_substitution_mappings(): # Record the nested templates in top level template self.nested_tosca_templates_with_topology.\ @@ -288,6 +292,10 @@ class ToscaTemplate(object): def verify_template(self): if ExceptionCollector.exceptionsCaught(): + if self.no_required_paras_valid: + ExceptionCollector.removeException( + MissingRequiredParameterError) + if self.input_path: raise ValidationError( message=(_('\nThe input "%(path)s" failed validation with ' -- cgit 1.2.3-korg