from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidNodeTypeError
from toscaparser.common.exception import MissingRequiredFieldError
+from toscaparser.common.exception import MissingRequiredInputError
+from toscaparser.common.exception import MissingRequiredParameterError
from toscaparser.common.exception import UnknownFieldError
+from toscaparser.elements.nodetype import NodeType
+
log = logging.getLogger('tosca')
def requirements(self):
return self.sub_mapping_def.get(self.REQUIREMENTS)
+ @property
+ def node_definition(self):
+ return NodeType(self.node_type, self.custom_defs)
+
def _validate(self):
+ # basic valiation
self._validate_keys()
self._validate_type()
+
+ # SubstitutionMapping class syntax validation
self._validate_inputs()
self._validate_capabilities()
self._validate_requirements()
for key in self.sub_mapping_def.keys():
if key not in self.SECTIONS:
ExceptionCollector.appendException(
- UnknownFieldError(what='Substitution_mappings',
+ UnknownFieldError(what='SubstitutionMappings',
field=key))
def _validate_type(self):
if not node_type:
ExceptionCollector.appendException(
MissingRequiredFieldError(
- what=_('Substitution_mappings used in topology_template'),
+ what=_('SubstitutionMappings used in topology_template'),
required=self.NODE_TYPE))
node_type_def = self.custom_defs.get(node_type)
def _validate_inputs(self):
"""validate the inputs of substitution mappings."""
- # The inputs in service template which defines substutition mappings
- # must be in properties of node template wchich be mapped.
- inputs_names = list(self.sub_mapped_node_template
- .get_properties().keys()
- if self.sub_mapped_node_template else [])
- for name in inputs_names:
- if name not in [input.name for input in self.inputs]:
+ # The inputs in service template which provides substutition mappings
+ # must be in properties of node template which is mapped or provide
+ # defualt value. Currently the input.name is not restrict to be the
+ # same as property name in specification, but they should be equal
+ # for current implementation.
+
+ # Must provide parameters for required properties of node_type
+ # This checking is internal(inside SubstitutionMappings)
+ for propery in self.node_definition.get_properties_def_objects():
+ # Check property which is 'required' and has no 'default' value
+ if propery.required and propery.default is None and \
+ propery.name not in [input.name for input in self.inputs]:
ExceptionCollector.appendException(
- UnknownFieldError(what='SubstitutionMappings',
- field=name))
+ MissingRequiredInputError(
+ what='SubstitutionMappings with node_type:'
+ + self.node_type,
+ input_name=propery.name))
+
+ # Get property names from substituted node tempalte
+ property_names = list(self.sub_mapped_node_template
+ .get_properties().keys()
+ if self.sub_mapped_node_template else [])
+ # Sub_mapped_node_template is None(deploy standaolone), will check
+ # according to node_type
+ if 0 == len(property_names):
+ property_names = list(self.node_definition
+ .get_properties_def().keys())
+ # Provide default value for parameter which is not property of
+ # node with the type node_type, this may not be mandatory for
+ # current implematation, but the specification express it mandatory.
+ # This checking is external(outside SubstitutionMappings)
+ for input in self.inputs:
+ if input.name not in property_names and input.default is None:
+ ExceptionCollector.appendException(
+ MissingRequiredParameterError(
+ what='SubstitutionMappings with node_type:'
+ + self.node_type,
+ input_name=input.name))
def _validate_capabilities(self):
"""validate the capabilities of substitution mappings."""
cap not in list(tpls_capabilities.keys())):
pass
# ExceptionCollector.appendException(
- # UnknownFieldError(what='Substitution_mappings',
+ # UnknownFieldError(what='SubstitutionMappings',
# field=cap))
def _validate_requirements(self):
req not in list(tpls_requirements.keys())):
pass
# ExceptionCollector.appendException(
- # UnknownFieldError(what='Substitution_mappings',
+ # UnknownFieldError(what='SubstitutionMappings',
# field=req))
def _validate_outputs(self):
# for name in outputs_names:
# if name not in [output.name for input in self.outputs]:
# ExceptionCollector.appendException(
- # UnknownFieldError(what='Substitution_mappings',
+ # UnknownFieldError(what='SubstitutionMappings',
# field=name))
import logging
import os
+from copy import deepcopy
from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidTemplateVersion
from toscaparser.common.exception import MissingRequiredFieldError
for fname, tosca_tpl in self.nested_tosca_tpls_with_topology.items():
for nodetemplate in self.nodetemplates:
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=self.parsed_params,
+ path=fname, parsed_params=parsed_params,
yaml_dict_tpl=tosca_tpl,
sub_mapped_node_template=nodetemplate)
if nested_template.has_substitution_mappings():
else:
return False
+ def _get_params_for_nested_template(self, nodetemplate):
+ """Return total params for nested_template."""
+ parsed_params = deepcopy(self.parsed_params) \
+ if self.parsed_params else {}
+ if nodetemplate:
+ for pname in nodetemplate.get_properties():
+ parsed_params.update({pname:
+ nodetemplate.get_property_value(pname)})
+ return parsed_params
+
def get_sub_mapping_node_type(self, tosca_tpl):
"""Return substitution mappings node type."""
if tosca_tpl: