def _validate_type(self): """validate the node_type of substitution mappings.""" if self.node: node = self.topology.node_templates.get(self.node) if not node: ExceptionCollector.appendException( ValidationError(message=_( 'Unknown node "%s" declared on substitution_mappings') % self.node)) self.node_definition = node.type_definition return node_type = self.sub_mapping_def.get(self.NODE_TYPE) if not node_type: ExceptionCollector.appendException( MissingRequiredFieldError( what=_('SubstitutionMappings used in topology_template'), required=self.NODE_TYPE)) return False node_type_def = self.custom_defs.get(node_type) if not node_type_def: ExceptionCollector.appendException( InvalidNodeTypeError(what=node_type)) return False self.node_definition = NodeType(self.node_type, self.custom_defs) return True
def __init__(self, name, template, entity_name, custom_def=None): self.name = name self.entity_tpl = template self.custom_def = custom_def self._validate_field(self.entity_tpl) type = self.entity_tpl.get('type') UnsupportedType.validate_type(type) if entity_name == 'node_type': self.type_definition = NodeType(type, custom_def) \ if type is not None else None if entity_name == 'relationship_type': relationship = template.get('relationship') type = None if relationship and isinstance(relationship, dict): type = relationship.get('type') elif isinstance(relationship, str): type = self.entity_tpl['relationship'] else: type = self.entity_tpl['type'] UnsupportedType.validate_type(type) self.type_definition = RelationshipType(type, None, custom_def) if entity_name == 'policy_type': if not type: msg = (_('Policy definition of "%(pname)s" must have' ' a "type" ' 'attribute.') % dict(pname=name)) ExceptionCollector.appendException(ValidationError(msg)) self.type_definition = PolicyType(type, custom_def) if entity_name == 'group_type': self.type_definition = GroupType(type, custom_def) \ if type is not None else None self._properties = None self._interfaces = None self._requirements = None self._capabilities = None
def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs, sub_mapped_node_template, custom_defs): self.nodetemplates = nodetemplates self.sub_mapping_def = sub_mapping_def TOSCAException.set_context(self.type, "substitution_mapping") self.inputs = inputs or [] self.outputs = outputs or [] self.sub_mapped_node_template = sub_mapped_node_template self.custom_defs = custom_defs or {} self._validate() self.type_definition = NodeType(self.type, custom_defs) self._properties = None self._capabilities = None self._requirements = None self._interfaces = None TOSCAException.reset_context()
def test_normative_type_by_short_name(self): # test template with a short name Compute template = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/test_tosca_normative_type_by_shortname.yaml") tosca_tpl = ToscaTemplate(template) expected_type = "tosca.nodes.Compute" for tpl in tosca_tpl.nodetemplates: self.assertEqual(tpl.type, expected_type) for tpl in tosca_tpl.nodetemplates: compute_type = NodeType(tpl.type) self.assertEqual( sorted(['tosca.capabilities.Container', 'tosca.capabilities.OperatingSystem', 'tosca.capabilities.network.Bindable', 'tosca.capabilities.Scalable']), sorted([c.type for c in compute_type.get_capabilities_objects()]))
def test_normative_type_by_short_name(self): # test template with a short name Compute template = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/test_tosca_normative_type_by_shortname.yaml") tosca_tpl = ToscaTemplate(template) expected_type = "tosca.nodes.Compute" for tpl in tosca_tpl.nodetemplates: self.assertEqual(tpl.type, expected_type) for tpl in tosca_tpl.nodetemplates: compute_type = NodeType(tpl.type) self.assertEqual( sorted(['tosca.capabilities.Container', 'tosca.capabilities.Node', 'tosca.capabilities.OperatingSystem', 'tosca.capabilities.network.Bindable', 'tosca.capabilities.Scalable']), sorted([c.type for c in compute_type.get_capabilities_objects()]))
def build_node_type_fact(node_type: NodeType) -> str: prop_defs = "[" \ + ", ".join([build_property_def(prop_def) for prop_def in node_type.get_properties_def_objects()]) \ + "]" def build_capability_def(cap_def: CapabilityTypeDef): return f"capability({cap_def.name}, '{cap_def.type}')" cap_defs = "[" \ + ", ".join([build_capability_def(cap_def) for cap_def in node_type.get_capabilities_objects()]) \ + "]" def build_type_requirement(req_name, req_def): req_cap = req_def.get("capability", "tosca.capabilities.Root") req_node = req_def.get("node", "tosca.nodes.Root") req_rel = req_def.get("relationship", "tosca.relationships.Root") occ = req_def.get("occurrences") if occ is None: req_occ = "occurrences(1, unbounded)" else: req_occ = f"occurrences({occ[0]}, {'unbounded' if occ[1] == 'UNBOUNDED' else occ[1]})" return f"requirement({req_name}, '{req_cap}', '{req_node}', '{req_rel}', {req_occ})" req_l = [] for req in node_type.requirements: # type: ignore req_name = list(req)[0] # Gets the first key of req req_def = req[req_name] req_l.append(build_type_requirement(req_name, req_def)) requirements = "[" + ", ".join(req_l) + "]" return textwrap.dedent(f""" node_type( '{node_type.type}', '{node_type.parent_type.type if node_type.parent_type is not None else 'none'}', {prop_defs}, {cap_defs}, {requirements} )""")
def __init__(self, name, template, entity_name, custom_def=None): self.name = name self.entity_tpl = template self.custom_def = custom_def self._validate_field(self.entity_tpl) type = self.entity_tpl.get('type') UnsupportedType.validate_type(type) if '__typename' not in template: self._validate_fields(template) if entity_name == 'node_type': self.type_definition = NodeType(type, custom_def) \ if type is not None else None self._validate_directives(self.entity_tpl) if entity_name == 'relationship_type': self.type_definition = RelationshipType(type, custom_def) if entity_name == 'policy_type': if not type: msg = (_('Policy definition of "%(pname)s" must have' ' a "type" ' 'attribute.') % dict(pname=name)) ExceptionCollector.appendException( ValidationError(message=msg)) self.type_definition = PolicyType(type, custom_def) if entity_name == 'group_type': self.type_definition = GroupType(type, custom_def) \ if type is not None else None if entity_name == 'artifact_type': self.type_definition = ArtifactTypeDef(type, custom_def) \ if type is not None else None self._properties = None self._interfaces = None self._requirements = None self._capabilities = None if not self.type_definition: msg = "no type found %s for %s" % (entity_name, template) ExceptionCollector.appendException(ValidationError(message=msg)) return metadata = self.type_definition.get_definition('metadata') if metadata and 'additionalProperties' in metadata: self.additionalProperties = metadata['additionalProperties'] self._properties_tpl = self._validate_properties() for prop in self.get_properties_objects(): prop.validate() self._validate_interfaces()
def __init__(self, name, template, entity_name, custom_def=None): self.name = name self.entity_tpl = template self.custom_def = custom_def self._validate_field(self.entity_tpl) if entity_name == 'node_type': type = self.entity_tpl.get('type') self.type_definition = NodeType(type, custom_def) \ if type is not None else None if entity_name == 'relationship_type': relationship = template.get('relationship') type = None if relationship and isinstance(relationship, dict): type = relationship.get('type') elif isinstance(relationship, str): type = self.entity_tpl['relationship'] else: type = self.entity_tpl['type'] self.type_definition = RelationshipType(type, None, custom_def) self._properties = None self._interfaces = None self._requirements = None self._capabilities = None
def test_interfaces(self): self.assertEqual(compute_type.interfaces, None) root_node = NodeType('tosca.nodes.Root') self.assertIn(ifaces.LIFECYCLE_SHORTNAME, root_node.interfaces)
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from toscaparser.common import exception from toscaparser.elements.artifacttype import ArtifactTypeDef from toscaparser.elements.entity_type import EntityType import toscaparser.elements.interfaces as ifaces from toscaparser.elements.nodetype import NodeType from toscaparser.elements.policytype import PolicyType from toscaparser.tests.base import TestCase compute_type = NodeType('tosca.nodes.Compute') component_type = NodeType('tosca.nodes.SoftwareComponent') network_type = NodeType('tosca.nodes.network.Network') network_port_type = NodeType('tosca.nodes.network.Port') webserver_type = NodeType('tosca.nodes.WebServer') database_type = NodeType('tosca.nodes.Database') artif_root_type = ArtifactTypeDef('tosca.artifacts.Root') artif_file_type = ArtifactTypeDef('tosca.artifacts.File') artif_bash_type = ArtifactTypeDef('tosca.artifacts.Implementation.Bash') artif_python_type = ArtifactTypeDef('tosca.artifacts.Implementation.Python') artif_container_docker_type = ArtifactTypeDef('tosca.artifacts.' 'Deployment.Image.' 'Container.Docker') artif_vm_iso_type = ArtifactTypeDef('tosca.artifacts.' 'Deployment.Image.VM.ISO') artif_vm_qcow2_type = ArtifactTypeDef('tosca.artifacts.'
def node_definition(self): return NodeType(self.node_type, self.custom_defs)
class SubstitutionMappings(object): '''SubstitutionMappings class declaration SubstitutionMappings exports the topology template as an implementation of a Node type. ''' SECTIONS = (NODE, NODE_TYPE, REQUIREMENTS, CAPABILITIES, PROPERTIES, SUBSTITUTION_FILTER, ATTRIBUTES, INTERFACES) = \ ('node', 'node_type', 'requirements', 'capabilities', 'properties', 'substitution_filter', 'attributes', 'interfaces') # XXX added in 1.3: substitution_filter, attributes, interfaces, # XXX currently only supports property name to input name mapping OPTIONAL_OUTPUTS = ['tosca_id', 'tosca_name', 'state'] def __init__(self, sub_mapping_def, topology, inputs, outputs, sub_mapped_node_template, custom_defs): self.topology = topology self.sub_mapping_def = sub_mapping_def self.inputs = {input.name: input for input in inputs} if inputs else {} self.outputs = outputs or [] self.sub_mapped_node_template = sub_mapped_node_template self.custom_defs = custom_defs or {} self.node_definition = None self._capabilities = None self._requirements = None self._properties = None self._validate() @property def type(self): if self.sub_mapping_def: return self.sub_mapping_def.get(self.NODE_TYPE) @property def node_type(self): return self.sub_mapping_def.get(self.NODE_TYPE) @property def node(self): return self.sub_mapping_def.get(self.NODE) @property def capabilities(self): return self.sub_mapping_def.get(self.CAPABILITIES) @property def requirements(self): return self.sub_mapping_def.get(self.REQUIREMENTS) @property def properties(self): if self._properties is None: self._properties = {} mapping = self.sub_mapping_def.get(self.PROPERTIES) if mapping: self._map(mapping, self.inputs, self._properties) return self._properties def _map(self, mapping, source, dest): for propname, value in mapping.items(): # map property from input if isinstance(value, list): input = value[0] else: input = value dest[propname] = source[input] def _validate(self): # Basic validation self._validate_keys() if not self._validate_type(): return # SubstitutionMapping class syntax validation self._validate_inputs() self._validate_capabilities() self._validate_requirements() self._validate_properties() self._validate_outputs() def _validate_keys(self): """validate the keys of substitution mappings.""" for key in self.sub_mapping_def.keys(): if key not in self.SECTIONS: ExceptionCollector.appendException( UnknownFieldError(what=_('SubstitutionMappings'), field=key)) def _validate_type(self): """validate the node_type of substitution mappings.""" if self.node: node = self.topology.node_templates.get(self.node) if not node: ExceptionCollector.appendException( ValidationError(message=_( 'Unknown node "%s" declared on substitution_mappings') % self.node)) self.node_definition = node.type_definition return node_type = self.sub_mapping_def.get(self.NODE_TYPE) if not node_type: ExceptionCollector.appendException( MissingRequiredFieldError( what=_('SubstitutionMappings used in topology_template'), required=self.NODE_TYPE)) return False node_type_def = self.custom_defs.get(node_type) if not node_type_def: ExceptionCollector.appendException( InvalidNodeTypeError(what=node_type)) return False self.node_definition = NodeType(self.node_type, self.custom_defs) return True def _validate_inputs(self): """validate the inputs of substitution mappings. The inputs defined by the topology template have to match the properties of the node type or the substituted node. If there are more inputs than the substituted node has properties, default values must be defined for those inputs. """ # reverse property name to input name mapping reverse_property_mappings = { v: n for n, v in self.sub_mapping_def.get(self.PROPERTIES, {}).items() } all_inputs = set([ reverse_property_mappings.get(input, input) for input in self.inputs ]) required_properties = set([ p.name for p in self.node_definition.get_properties_def_objects() if p.required and p.default is None ]) # Must provide inputs for required properties of node type. for property in required_properties: # Check property which is 'required' and has no 'default' value if property not in all_inputs: ExceptionCollector.appendException( MissingRequiredInputError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=property)) # If the optional properties of node type need to be customized by # substituted node, it also is necessary to define inputs for them, # otherwise they are not mandatory to be defined. customized_parameters = set( self.sub_mapped_node_template.get_properties().keys( ) if self.sub_mapped_node_template else []) all_properties = set(self.node_definition.get_properties_def()) for parameter in customized_parameters - all_inputs: if parameter in all_properties: ExceptionCollector.appendException( MissingRequiredInputError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=parameter)) # Additional inputs are not in the properties of node type must # provide default values. Currently the scenario may not happen # because of parameters validation in nodetemplate, here is a # guarantee. for input in self.inputs.values(): if input.name in all_inputs - all_properties \ and input.default is None: ExceptionCollector.appendException( MissingDefaultValueError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=input.name)) def _validate_capabilities(self): """validate the capabilities of substitution mappings.""" # The capabilites must be in node template which be mapped. tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES) node_capabilities = self.sub_mapped_node_template.get_capabilities() \ if self.sub_mapped_node_template else None for cap in node_capabilities.keys() if node_capabilities else []: if (tpls_capabilities and cap not in list(tpls_capabilities.keys())): pass # ExceptionCollector.appendException( # UnknownFieldError(what='SubstitutionMappings', # field=cap)) def _validate_requirements(self): """validate the requirements of substitution mappings.""" # The requirements must be in node template wchich be mapped. tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS) node_requirements = self.sub_mapped_node_template.requirements \ if self.sub_mapped_node_template else None for req in node_requirements if node_requirements else []: if (tpls_requirements and req not in list(tpls_requirements.keys())): pass # ExceptionCollector.appendException( # UnknownFieldError(what='SubstitutionMappings', # field=req)) def _validate_properties(self): """validate the properties of substitution mappings.""" # The properties in the substitution_mappings must be present # in the node template properties. tpls_properties = self.sub_mapping_def.get(self.PROPERTIES) node_properties = \ self.sub_mapped_node_template.get_properties_objects() \ if self.sub_mapped_node_template else None for req in node_properties if node_properties else []: if (tpls_properties and req not in list(tpls_properties.keys())): pass # ExceptionCollector.appendException( # UnknownFieldError(what='SubstitutionMappings', # field=req)) def _validate_outputs(self): """validate the outputs of substitution mappings. The outputs defined by the topology template have to match the attributes of the node type or the substituted node template, and the observable attributes of the substituted node template have to be defined as attributes of the node type or outputs in the topology template. """ # The outputs defined by the topology template have to match the # attributes of the node type according to the specification, but # it's reasonable that there are more inputs than the node type # has properties, the specification will be amended? for output in self.outputs: if output.name not in self.node_definition.get_attributes_def(): ExceptionCollector.appendException( UnknownOutputError( where=_('SubstitutionMappings with node_type ') + self.node_type, output_name=output.name))
class SubstitutionMappings(object): '''SubstitutionMappings class declaration SubstitutionMappings exports the topology template as an implementation of a Node type. ''' SECTIONS = (NODE_TYPE, REQUIREMENTS, CAPABILITIES, PROPERTIES, INTERFACES) = \ ('node_type', 'requirements', 'capabilities', 'properties', 'interfaces') OPTIONAL_OUTPUTS = ['tosca_id', 'tosca_name', 'state'] def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs, sub_mapped_node_template, custom_defs): self.nodetemplates = nodetemplates self.sub_mapping_def = sub_mapping_def TOSCAException.set_context(self.type, "substitution_mapping") self.inputs = inputs or [] self.outputs = outputs or [] self.sub_mapped_node_template = sub_mapped_node_template self.custom_defs = custom_defs or {} self._validate() self.type_definition = NodeType(self.type, custom_defs) self._properties = None self._capabilities = None self._requirements = None self._interfaces = None TOSCAException.reset_context() @property def type(self): if self.sub_mapping_def: return self.sub_mapping_def.get(self.NODE_TYPE) @classmethod def get_node_type(cls, sub_mapping_def): if isinstance(sub_mapping_def, dict): return sub_mapping_def.get(cls.NODE_TYPE) @property def node_type(self): return self.sub_mapping_def.get(self.NODE_TYPE) @property def capabilities(self): return self.sub_mapping_def.get(self.CAPABILITIES) @property def requirements(self): return self.sub_mapping_def.get(self.REQUIREMENTS) @property def interfaces(self): return self.sub_mapping_def.get(self.INTERFACES) @property def node_definition(self): return NodeType(self.node_type, self.custom_defs) # Needed to support TOSCA Simple YAML 1.2 def get_properties_objects(self): '''Return properties objects for this template.''' if self._properties is None: self._properties = self._create_properties() return self._properties def get_properties(self): '''Return a dictionary of property name-object pairs.''' return {prop.name: prop for prop in self.get_properties_objects()} def get_property_value(self, name): '''Return the value of a given property name.''' props = self.get_properties() if props and name in props.keys(): return props[name].value def _create_properties(self): props = [] properties = self.type_definition.get_value( self.PROPERTIES, self.sub_mapping_def) or {} for name, value in properties.items(): props_def = self.type_definition.get_properties_def() if props_def and name in props_def: prop = Property(name, value, props_def[name].schema, self.custom_defs) props.append(prop) for p in self.type_definition.get_properties_def_objects(): if p.default is not None and p.name not in properties.keys(): prop = Property(p.name, p.default, p.schema, self.custom_defs) props.append(prop) return props def __str__(self): s = "Substitution Mappings:\n" s += "\tproperties: {}\n".format( {prop.name: prop.value for prop in self.get_properties_objects()}) s += "\ttype: {}\n".format(self.type) s += "\trequirements: {}\n".format(self.requirements) s += "\tcapabilites: {}\n".format(self.capabilities) s += "\tinterfaces: {}\n".format(self.interfaces) s += "\tinputs: {}\n".format([str(inp) for inp in self.inputs]) return s def _validate(self): # Basic validation self._validate_keys() self._validate_type() # SubstitutionMapping class syntax validation self._validate_inputs() self._validate_capabilities() self._validate_requirements() self._validate_outputs() def _validate_keys(self): """validate the keys of substitution mappings.""" for key in self.sub_mapping_def.keys(): if key not in self.SECTIONS: ExceptionCollector.appendException( UnknownFieldError(what=_('SubstitutionMappings'), field=key)) def _validate_type(self): """validate the node_type of substitution mappings.""" node_type = self.sub_mapping_def.get(self.NODE_TYPE) if not node_type: ExceptionCollector.appendException( MissingRequiredFieldError( what=_('SubstitutionMappings used in topology_template'), required=self.NODE_TYPE)) node_type_def = self.custom_defs.get(node_type) if not node_type_def: ExceptionCollector.appendException( InvalidNodeTypeError(what=node_type)) def _validate_inputs(self): """validate the inputs of substitution mappings. The inputs defined by the topology template have to match the properties of the node type or the substituted node. If there are more inputs than the substituted node has properties, default values must be defined for those inputs. """ all_inputs = set([input.name for input in self.inputs]) required_properties = set([ p.name for p in self.node_definition.get_properties_def_objects() if p.required and p.default is None ]) # Must provide inputs for required properties of node type. for property in required_properties: # Check property which is 'required' and has no 'default' value if property not in all_inputs: ExceptionCollector.appendException( MissingRequiredInputError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=property)) # If the optional properties of node type need to be customized by # substituted node, it also is necessary to define inputs for them, # otherwise they are not mandatory to be defined. customized_parameters = set( self.sub_mapped_node_template.get_properties().keys( ) if self.sub_mapped_node_template else []) all_properties = set([ p.name for p in self.node_definition.get_properties_def_objects() if not p.required ]) for parameter in customized_parameters - all_inputs: if parameter in all_properties: ExceptionCollector.appendException( MissingRequiredInputError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=parameter)) # Additional inputs are not in the properties of node type must # provide default values. Currently the scenario may not happen # because of parameters validation in nodetemplate, here is a # guarantee. for input in self.inputs: if input.name in all_inputs - all_properties \ and input.default is None: ExceptionCollector.appendException( MissingDefaultValueError( what=_('SubstitutionMappings with node_type ') + self.node_type, input_name=input.name)) def _validate_capabilities(self): """validate the capabilities of substitution mappings.""" # The capabilites must be in node template wchich be mapped. tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES) node_capabiliteys = self.sub_mapped_node_template.get_capabilities() \ if self.sub_mapped_node_template else None for cap in node_capabiliteys.keys() if node_capabiliteys else []: if (tpls_capabilities and cap not in list(tpls_capabilities.keys())): pass # ExceptionCollector.appendException( # UnknownFieldError(what='SubstitutionMappings', # field=cap)) def _validate_requirements(self): """validate the requirements of substitution mappings.""" # The requirements must be in node template which is mapped. tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS) node_requirements = self.sub_mapped_node_template.requirements \ if self.sub_mapped_node_template else None log.debug("tpls req: {}, node req: {}".format(tpls_requirements, node_requirements)) def process_req(req): log.debug("Node requirements: {}".format(req)) if tpls_requirements: keys = [] if isinstance(tpls_requirements, list): for tp in tpls_requirements: keys.extend(list(tp.keys())) else: keys = list(tpls_requirements.keys()) log.debug("Tpl keys: {}".format(keys)) for req_key in req.keys(): if req_key in keys: pass else: log.info("Unknown field Subs: {}".format(req)) ExceptionCollector.appendException( UnknownFieldError(what='SubstitutionMappings', field=req)) if isinstance(node_requirements, dict) or not node_requirements: for req in node_requirements if node_requirements else {}: process_req({req: node_requirements[req]}) elif isinstance(node_requirements, list): for req in node_requirements: process_req(req) else: ExceptionCollector.appendException( UnknownFieldError(what='SubstitutionMappings', field='Requirements is not list or dict')) def _validate_outputs(self): """validate the outputs of substitution mappings. The outputs defined by the topology template have to match the attributes of the node type or the substituted node template, and the observable attributes of the substituted node template have to be defined as attributes of the node type or outputs in the topology template. """ # The outputs defined by the topology template have to match the # attributes of the node type according to the specification, but # it's reasonable that there are more inputs than the node type # has properties, the specification will be amended? for output in self.outputs: if output.name not in self.node_definition.get_attributes_def(): ExceptionCollector.appendException( UnknownOutputError( where=_('SubstitutionMappings with node_type ') + self.node_type, output_name=output.name)) # The observable attributes of the substituted node template # have to be defined as attributes of the node type or outputs in # the topology template, the attributes in tosca.node.root are # optional. for attribute in self.node_definition.get_attributes_def(): if attribute not in [output.name for output in self.outputs] \ and attribute not in self.OPTIONAL_OUTPUTS: ExceptionCollector.appendException( MissingRequiredOutputError( what=_('SubstitutionMappings with node_type ') + self.node_type, output_name=attribute))