diff options
Diffstat (limited to 'nfvparser/toscaparser/tosca_template.py')
-rw-r--r-- | nfvparser/toscaparser/tosca_template.py | 76 |
1 files changed, 63 insertions, 13 deletions
diff --git a/nfvparser/toscaparser/tosca_template.py b/nfvparser/toscaparser/tosca_template.py index f48078f..b0df424 100644 --- a/nfvparser/toscaparser/tosca_template.py +++ b/nfvparser/toscaparser/tosca_template.py @@ -18,6 +18,9 @@ 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 MissingRequiredInputError +from toscaparser.common.exception import MissingRequiredOutputError +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 @@ -55,17 +58,21 @@ YAML_LOADER = toscaparser.utils.yamlparser.load_yaml class ToscaTemplate(object): exttools = ExtTools() - VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0'] + VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0', + 'tosca_simple_yaml_1_1'] VALID_TEMPLATE_VERSIONS.extend(exttools.get_versions()) - ADDITIONAL_SECTIONS = {'tosca_simple_yaml_1_0': SPECIAL_SECTIONS} + ADDITIONAL_SECTIONS = {'tosca_simple_yaml_1_0': SPECIAL_SECTIONS, + 'tosca_simple_yaml_1_1': SPECIAL_SECTIONS} ADDITIONAL_SECTIONS.update(exttools.get_sections()) '''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_check=False, + debug_mode=False): if sub_mapped_node_template is None: ExceptionCollector.start() self.a_file = a_file @@ -75,6 +82,9 @@ 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_check = no_required_paras_check + self.debug_mode = debug_mode + if path: self.input_path = path self.path = self._get_path(path) @@ -233,16 +243,36 @@ class ToscaTemplate(object): if self._is_sub_mapped_node(nodetemplate, tosca_tpl): parsed_params = self._get_params_for_nested_template( nodetemplate) - nested_template = ToscaTemplate( - path=fname, parsed_params=parsed_params, - yaml_dict_tpl=tosca_tpl, - sub_mapped_node_template=nodetemplate) - if nested_template._has_substitution_mappings(): + + cache_exeptions = deepcopy(ExceptionCollector.exceptions) + cache_exeptions_state = \ + deepcopy(ExceptionCollector.collecting) + nested_template = None + try: + nrpv = self.no_required_paras_check + nested_template = ToscaTemplate( + path=fname, parsed_params=parsed_params, + sub_mapped_node_template=nodetemplate, + no_required_paras_check=nrpv, + debug_mode=self.debug_mode) + except ValidationError as e: + log.error(e.message) + if self.debug_mode: + print(e.message) + else: + raise e + + ExceptionCollector.exceptions = deepcopy(cache_exeptions) + ExceptionCollector.collecting = \ + deepcopy(cache_exeptions_state) + + if nested_template and \ + nested_template._has_substitution_mappings(): # Record the nested templates in top level template self.nested_tosca_templates_with_topology.\ append(nested_template) # Set the substitution toscatemplate for mapped node - nodetemplate.sub_mapping_tosca_template = \ + nodetemplate.substitution_mapped = \ nested_template def _validate_field(self): @@ -268,11 +298,12 @@ class ToscaTemplate(object): what=version, valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS))) else: - if version != 'tosca_simple_yaml_1_0': + if (version != 'tosca_simple_yaml_1_0' and + version != 'tosca_simple_yaml_1_1'): update_definitions(version) def _get_path(self, path): - if path.lower().endswith('.yaml'): + if path.lower().endswith('.yaml') or path.lower().endswith('.yml'): return path elif path.lower().endswith(('.zip', '.csar')): # a CSAR archive @@ -287,18 +318,37 @@ class ToscaTemplate(object): % {'path': path})) def verify_template(self): + if self.no_required_paras_check: + ExceptionCollector.removeException( + MissingRequiredParameterError) + ExceptionCollector.removeException( + MissingRequiredInputError) + ExceptionCollector.removeException( + MissingRequiredOutputError) if ExceptionCollector.exceptionsCaught(): if self.input_path: - raise ValidationError( + exceptions = ValidationError( message=(_('\nThe input "%(path)s" failed validation with ' 'the following error(s): \n\n\t') % {'path': self.input_path}) + '\n\t'.join(ExceptionCollector.getExceptionsReport())) else: - raise ValidationError( + exceptions = ValidationError( message=_('\nThe pre-parsed input failed validation with ' 'the following error(s): \n\n\t') + '\n\t'.join(ExceptionCollector.getExceptionsReport())) + if not self.debug_mode: + raise exceptions + else: + if self.sub_mapped_node_template: + msg = _('======== nested service template ======== ') + + else: + msg = _('======== main service template ======== ') + print(msg) + print(exceptions.message) + log.error(msg) + log.error(exceptions.message) else: if self.input_path: msg = (_('The input "%(path)s" successfully passed ' |