def _handle_nested_tosca_templates_with_topology(self): for filename, tosca_tpl in self.nested_tosca_tpls.items(): topology_tpl = tosca_tpl.get(TOPOLOGY_TEMPLATE) if topology_tpl: custom_types = self._get_all_custom_defs().copy() custom_types.update(tosca_tpl.get( 'node_types', {})) # XXX isn't this redundant? self.nested_topologies[filename] = TopologyTemplate( topology_tpl, custom_types) # if a nodetemplate should be substituted, set its sub_mapping_tosca_template for nodetemplate in self.nodetemplates: if "substitute" not in nodetemplate.directives: continue for topology in self.nested_topologies.values(): if not topology.substitution_mappings: continue if topology.substitution_mappings.type == nodetemplate.type: # the node template's properties treated as inputs inputs = self._get_params_for_nested_template(nodetemplate) # create a new substitution mapping object for the mapped node # XXX SubstitutionMappings is just a simple wrapper around the def dict, only performs validation # and sub_mapping_tosca_template is never unused! nodetemplate.sub_mapping_tosca_template = SubstitutionMappings( topology.substitution_mappings.sub_mapping_def, topology, inputs, topology.outputs, nodetemplate, topology.custom_defs) break
def translate_to_provider(self): new_element_templates, new_extra, template_mapping = translate_to_provider(self) dict_tpl = {} self.template_mapping = template_mapping if new_element_templates.get(NODES): dict_tpl[NODE_TEMPLATES] = new_element_templates[NODES] if new_element_templates.get(RELATIONSHIPS): dict_tpl[RELATIONSHIP_TEMPLATES] = new_element_templates[RELATIONSHIPS] if new_element_templates.get(OUTPUTS): dict_tpl[OUTPUTS] = new_element_templates[OUTPUTS] if self.inputs: dict_tpl[INPUTS] = self.inputs rel_types = {} for k, v in self.definitions.items(): (_, element_type, _) = utils.tosca_type_parse(k) if element_type == RELATIONSHIPS: rel_types[k] = v logging.debug("TOSCA template with non normative types for provider %s was generated: \n%s" % (self.provider, yaml.dump(dict_tpl))) try: topology_tpl = TopologyTemplate(dict_tpl, self.definitions, rel_types=rel_types) except: logging.exception("Failed to parse intermidiate non-normative TOSCA template with OpenStack tosca-parser") sys.exit(1) self.extra_configuration_tool_params = utils.deep_update_dict(self.extra_configuration_tool_params, new_extra) self.node_templates = new_element_templates.get(NODES, {}) self.relationship_templates = new_element_templates.get(RELATIONSHIPS, {}) self.outputs = new_element_templates.get(OUTPUTS, {})
def translate_to_provider(self): new_element_templates, new_artifacts, conditions_set, new_extra = translate_to_provider( self.tosca_elements_map_to_provider(), self.tosca_topology_template) self.used_conditions_set = conditions_set dict_tpl = copy.deepcopy(self.tosca_topology_template.tpl) if new_element_templates.get(NODES): dict_tpl[NODE_TEMPLATES] = new_element_templates[NODES] if new_element_templates.get(RELATIONSHIPS): dict_tpl[RELATIONSHIP_TEMPLATES] = new_element_templates[ RELATIONSHIPS] rel_types = [] for k, v in self.provider_defs.items(): (_, element_type, _) = tosca_type.parse(k) if element_type == RELATIONSHIP_TYPES: rel_types.append(v) topology_tpl = TopologyTemplate(dict_tpl, self.full_provider_defs, rel_types) self.artifacts.extend(new_artifacts) self.extra_configuration_tool_params = deep_update_dict( self.extra_configuration_tool_params, new_extra) return topology_tpl
def setUp(self): TestCase.setUp(self) '''TOSCA template.''' self.tosca_tpl_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/topology_template/subsystem.yaml") self.tpl = YAML_LOADER(self.tosca_tpl_path) self.topo_tpl = self.tpl.get('topology_template') self.imports = self.tpl.get('imports') self.topo = TopologyTemplate(self.topo_tpl, self._get_all_custom_def())
def _get_nodetemplate(tpl_snippet, name, custom_def_snippet=None): tpl = toscaparser.utils.yamlparser.simple_parse(tpl_snippet) nodetemplates = tpl['node_templates'] custom_def = [] if custom_def_snippet: custom_def = toscaparser.utils.yamlparser.simple_parse( custom_def_snippet) topology = TopologyTemplate(tpl, custom_def) if not name: name = list(nodetemplates.keys())[0] return NodeTemplate(name, topology, custom_def)
def setUp(self): TestCase.setUp(self) exception.ExceptionCollector.stop( ) # Added as sometimes negative testcases fails. '''TOSCA template.''' self.tosca_tpl_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/topology_template/transactionsubsystem.yaml") self.tpl = YAML_LOADER(self.tosca_tpl_path) self.topo_tpl = self.tpl.get('topology_template') self.imports = self.tpl.get('imports') self.topo = TopologyTemplate(self.topo_tpl, self._get_all_custom_def())
def test_scenario_scalar_unit_positive(self): tpl = self.tpl_snippet nodetemplates = yamlparser.simple_parse(tpl) topology = TopologyTemplate({'node_templates': nodetemplates}, []) nodetemplate = NodeTemplate('server', topology) props = nodetemplate.get_capability('host').get_properties() prop_name = self.property if props and prop_name in props.keys(): prop = props[prop_name] self.assertIsNone(prop.validate()) resolved = prop.value self.assertEqual(resolved, self.expected)
def test_invalid_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 50.3.6 GHZ disk_size: MB mem_size: 1 QB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) self.assertRaises( ValueError, lambda: TopologyTemplate( {'node_templates': nodetemplates}, self.custom_def))
def test_invalid_type_nodetemplates(self): tpl_snippet = ''' node_templates: - some_node: type: tosca.nodes.Compute ''' policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet)) custom_defs = self._get_custom_types() err = self.assertRaises(exception.TypeMismatchError, lambda: TopologyTemplate(policies, custom_defs)) errormsg = _('node_templates must be of type "dict".') self.assertEqual(errormsg, err.__str__())
def test_invalid_type_relationship_templates(self): tpl_snippet = ''' relationship_templates: - my_connection: type: ConnectsTo ''' policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet)) custom_defs = self._get_custom_types() err = self.assertRaises(exception.TypeMismatchError, lambda: TopologyTemplate(policies, custom_defs)) errormsg = _('relationship_templates must be of type "dict".') self.assertEqual(errormsg, err.__str__())
def test_constraint_for_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 0.05 GHz disk_size: 500 MB mem_size: 1 MB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) self.assertRaises( exception.ValidationError, lambda: TopologyTemplate( {'node_templates': nodetemplates}, self.custom_def))
def _get_nodetemplate(self, tpl_snippet, custom_def_snippet=None, name=None): tpl = yamlparser.simple_parse(tpl_snippet) nodetemplates = tpl['node_templates'] custom_def = [] if custom_def_snippet: custom_def = yamlparser.simple_parse(custom_def_snippet) if not name: name = list(nodetemplates.keys())[0] topology = TopologyTemplate(tpl, custom_def) tpl = NodeTemplate(name, topology, custom_def) return tpl
def test_invalid_type_substitution_mappings(self): tpl_snippet = ''' substitution_mappings: - node_type: MyService properties: num_cpus: cpus ''' policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet)) custom_defs = self._get_custom_types() err = self.assertRaises(exception.TypeMismatchError, lambda: TopologyTemplate(policies, custom_defs)) errormsg = _('substitution_mappings must be of type "dict".') self.assertEqual(errormsg, err.__str__())
def _handle_nested_tosca_templates_with_topology(self): 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): topology_tpl = tosca_tpl.get(TOPOLOGY_TEMPLATE) topology_with_sub_mapping = TopologyTemplate( topology_tpl, self._get_all_custom_defs(), self.relationship_types, self.parsed_params, nodetemplate) if topology_with_sub_mapping.substitution_mappings: # Record nested topo templates in top level template self.nested_tosca_templates_with_topology.\ append(topology_with_sub_mapping) # Set substitution mapping object for mapped node nodetemplate.sub_mapping_tosca_template = \ topology_with_sub_mapping.substitution_mappings
def _topology_template(self): return TopologyTemplate(self._tpl_topology_template(), self._get_all_custom_defs(), self.relationship_types, self.parsed_params)
def get_sub_mapping_node_type(self, tosca_tpl): """Return substitution mappings node type.""" if tosca_tpl: return TopologyTemplate.get_sub_mapping_node_type( tosca_tpl.get(TOPOLOGY_TEMPLATE))
def _topology_template(self): return TopologyTemplate(self._tpl_topology_template(), self._get_all_custom_defs(), self.parsed_params, None)
def get_sub_mapping_node_type(self, tosca_tpl): """Return substitution mappings node type.""" if tosca_tpl: return TopologyTemplate.get_sub_mapping_node_type( tosca_tpl.get(TOPOLOGY_TEMPLATE))