def _test_samples(self, files): if files: for f in self._get_list_of_sample(files): with open(f, 'r') as _f: yaml_dict = None try: yaml_dict = yamlparser.simple_ordered_parse(_f.read()) except: # noqa pass self.assertIsNotNone(yaml_dict, "Yaml parser failed to parse %s" % f) utils.updateimports(yaml_dict) tosca = None try: tosca = tosca_template.ToscaTemplate( a_file=False, yaml_dict_tpl=yaml_dict) except: # noqa pass self.assertIsNotNone(tosca, "Tosca parser failed to parse %s" % f) utils.post_process_template(tosca) hot = None try: hot = tosca_translator.TOSCATranslator(tosca, {}).translate() except: # noqa pass self.assertIsNotNone( hot, "Heat-translator failed to translate %s" % f)
def test_check_for_substitution_mappings(self): tosca_sb_map = _get_template('../../../../../etc/samples/test-nsd-' 'vnfd1.yaml') param = { 'substitution_mappings': { 'VL2': { 'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net0', 'vendor': 'tacker' } }, 'VL1': { 'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net_mgmt', 'vendor': 'tacker' } }, 'requirements': { 'virtualLink2': 'VL2', 'virtualLink1': 'VL1' } } } template = yaml.safe_load(tosca_sb_map) toscautils.updateimports(template) toscautils.check_for_substitution_mappings(template, param) self.assertNotIn('substitution_mappings', param)
def add_alarm_url_to_vnf(self, context, vnf_dict): vnfd_yaml = vnf_dict['vnfd']['attributes'].get('vnfd', '') vnfd_dict = yaml.safe_load(vnfd_yaml) if not (vnfd_dict and vnfd_dict.get('tosca_definitions_version')): return try: toscautils.updateimports(vnfd_dict) tosca_vnfd = ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) polices = vnfd_dict['topology_template'].get('policies', []) for policy_dict in polices: name, policy = list(policy_dict.items())[0] if policy['type'] in constants.POLICY_ALARMING: alarm_url =\ self._vnf_alarm_monitor.update_vnf_with_alarm( self, context, vnf_dict, policy) vnf_dict['attributes']['alarming_policy'] = vnf_dict['id'] vnf_dict['attributes'].update(alarm_url) elif policy['type'] in constants.POLICY_RESERVATION: alarm_url = \ self._vnf_reservation_monitor.update_vnf_with_reservation( self, context, vnf_dict, policy) vnf_dict['attributes']['reservation_policy'] = vnf_dict['id'] vnf_dict['attributes'].update(alarm_url) maintenance_vdus = toscautils.find_maintenance_vdus(tosca_vnfd) maintenance = \ self._vnf_maintenance_monitor.update_vnf_with_maintenance( vnf_dict, maintenance_vdus) vnf_dict['attributes'].update( {'maintenance': jsonutils.dumps(maintenance['vdus'])}) vnf_dict['attributes']['maintenance_url'] = maintenance['url']
def loader(self): """Load TOSCA template and start parsing""" try: parserd_params = None toscautils.updateimports(self.vnfd_dict) tosca = tosca_template.\ ToscaTemplate(parsed_params=parserd_params, a_file=False, yaml_dict_tpl=self.vnfd_dict) except Exception as e: LOG.debug("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) # Initiate a list tosca_kube_object which are defined from VDU tosca_kube_objects = [] vdus = toscautils.findvdus(tosca) for node_template in vdus: vdu_name = node_template.name tosca_kube_obj = self.tosca_to_kube_mapping(node_template) # Find network name in which VDU is attached tosca_kube_obj.network_name = self.find_networks(tosca, vdu_name) # If connection_point is True, Tacker will manage its service ip tosca_kube_obj.mgmt_connection_point = \ self.check_mgmt_cp(tosca, vdu_name) # Find scaling policy that is used for this VDU, different to # VM-based VNF, there are no alarm policies. tosca_kube_obj.scaling_object = \ self.get_scaling_policy(tosca, vdu_name) tosca_kube_objects.append(tosca_kube_obj) return tosca_kube_objects
def _parse_template_input(self, vnfd): vnfd_dict = vnfd['vnfd'] vnfd_yaml = vnfd_dict['attributes'].get('vnfd') if vnfd_yaml is None: return inner_vnfd_dict = yaml.safe_load(vnfd_yaml) LOG.debug('vnfd_dict: %s', inner_vnfd_dict) # Prepend the tacker_defs.yaml import file with the full # path to the file toscautils.updateimports(inner_vnfd_dict) try: tosca = ToscaTemplate(a_file=False, yaml_dict_tpl=inner_vnfd_dict) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) if ('description' not in vnfd_dict or vnfd_dict['description'] == ''): vnfd_dict['description'] = inner_vnfd_dict.get( 'description', '') if (('name' not in vnfd_dict or not len(vnfd_dict['name'])) and 'metadata' in inner_vnfd_dict): vnfd_dict['name'] = inner_vnfd_dict['metadata'].get( 'template_name', '') vnfd_dict['mgmt_driver'] = toscautils.get_mgmt_driver( tosca) LOG.debug('vnfd %s', vnfd)
def test_tacker_conf_heat_extra_specs_all_numa_count(self): tosca_fes_all_numa_count = _get_template( 'tosca_flavor_all_numa_count.yaml') vnfd_dict = yaml.safe_load(tosca_fes_all_numa_count) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 8, "disk": 10, "ram": 4096, "extra_specs": { 'hw:cpu_policy': 'dedicated', 'hw:mem_page_size': 'any', 'hw:cpu_sockets': 2, 'hw:cpu_threads': 2, 'hw:numa_nodes': 2, 'hw:cpu_cores': 2, 'hw:cpu_threads_policy': 'avoid', 'aggregate_instance_extra_specs:nfv': 'true' } } } actual_flavor_dict = toscautils.get_flavor_dict( tosca, {"aggregate_instance_extra_specs:nfv": "true"}) self.assertEqual(expected_flavor_dict, actual_flavor_dict)
def test_get_flavor_dict(self): vnfd_dict = yaml.safe_load(self.tosca_flavor) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = {"VDU1": {"vcpus": 2, "disk": 10, "ram": 512}} actual_flavor_dict = toscautils.get_flavor_dict(tosca) self.assertEqual(expected_flavor_dict, actual_flavor_dict)
def _parse_template_input(self, context, nsd): nsd_dict = nsd['nsd'] nsd_yaml = nsd_dict['attributes'].get('nsd') inner_nsd_dict = yaml.safe_load(nsd_yaml) nsd['vnfds'] = dict() LOG.debug('nsd_dict: %s', inner_nsd_dict) vnfm_plugin = manager.TackerManager.get_service_plugins()['VNFM'] vnfd_imports = inner_nsd_dict.get('imports') if not vnfd_imports: LOG.error('VNFD import section is missing') raise nfvo.ToscaParserFailed( error_msg_details='VNFD import section is missing') inner_nsd_dict['imports'] = [] new_files = [] for vnfd_name in vnfd_imports: vnfd = vnfm_plugin.get_vnfd(context, vnfd_name) # Copy VNF types and VNF names sm_dict = yaml.safe_load(vnfd['attributes']['vnfd'])[ 'topology_template'][ 'substitution_mappings'] nsd['vnfds'][sm_dict['node_type']] = vnfd['name'] # Ugly Hack to validate the child templates # TODO(tbh): add support in tosca-parser to pass child # templates as dict fd, temp_path = mkstemp() with open(temp_path, 'w') as fp: fp.write(vnfd['attributes']['vnfd']) os.close(fd) new_files.append(temp_path) inner_nsd_dict['imports'].append(temp_path) # Prepend the tacker_defs.yaml import file with the full # path to the file toscautils.updateimports(inner_nsd_dict) try: ToscaTemplate(a_file=False, yaml_dict_tpl=inner_nsd_dict) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise nfvo.ToscaParserFailed(error_msg_details=str(e)) finally: for file_path in new_files: os.remove(file_path) inner_nsd_dict['imports'] = vnfd_imports if ('description' not in nsd_dict or nsd_dict['description'] == ''): nsd_dict['description'] = inner_nsd_dict.get( 'description', '') if (('name' not in nsd_dict or not len(nsd_dict['name'])) and 'metadata' in inner_nsd_dict): nsd_dict['name'] = inner_nsd_dict['metadata'].get( 'template_name', '') LOG.debug('nsd %s', nsd)
def test_get_flavor_dict_with_wrong_cpu(self): tosca_fes = _get_template('tosca_flavor_with_wrong_cpu.yaml') vnfd_dict = yaml.safe_load(tosca_fes) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) self.assertRaises(vnfm.CpuAllocationInvalidValues, toscautils.get_flavor_dict, tosca)
def _parse_template_input(self, context, nsd): nsd_dict = nsd['nsd'] nsd_yaml = nsd_dict['attributes'].get('nsd') inner_nsd_dict = yaml.safe_load(nsd_yaml) nsd['vnfds'] = dict() LOG.debug('nsd_dict: %s', inner_nsd_dict) vnfm_plugin = manager.TackerManager.get_service_plugins()['VNFM'] vnfd_imports = inner_nsd_dict.get('imports') if not vnfd_imports: LOG.error('VNFD import section is missing') raise nfvo.ToscaParserFailed( error_msg_details='VNFD import section is missing') inner_nsd_dict['imports'] = [] new_files = [] for vnfd_name in vnfd_imports: vnfd = vnfm_plugin.get_vnfd(context, vnfd_name) # Copy VNF types and VNF names sm_dict = yaml.safe_load(vnfd['attributes']['vnfd'])[ 'topology_template'][ 'substitution_mappings'] nsd['vnfds'][sm_dict['node_type']] = vnfd['name'] # Ugly Hack to validate the child templates # TODO(tbh): add support in tosca-parser to pass child # templates as dict fd, temp_path = mkstemp() with open(temp_path, 'w') as fp: fp.write(vnfd['attributes']['vnfd']) os.close(fd) new_files.append(temp_path) inner_nsd_dict['imports'].append(temp_path) # Prepend the tacker_defs.yaml import file with the full # path to the file toscautils.updateimports(inner_nsd_dict) try: ToscaTemplate(a_file=False, yaml_dict_tpl=inner_nsd_dict) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise nfvo.ToscaParserFailed(error_msg_details=str(e)) finally: for file_path in new_files: os.remove(file_path) inner_nsd_dict['imports'] = vnfd_imports if ('description' not in nsd_dict or nsd_dict['description'] == ''): nsd_dict['description'] = inner_nsd_dict.get( 'description', '') if (('name' not in nsd_dict or not len(nsd_dict['name'])) and 'metadata' in inner_nsd_dict): nsd_dict['name'] = inner_nsd_dict['metadata'].get( 'template_name', '') LOG.debug('nsd %s', nsd)
def test_get_flavor_dict_with_wrong_cpu(self): tosca_fes = _get_template( 'tosca_flavor_with_wrong_cpu.yaml') vnfd_dict = yaml.safe_load(tosca_fes) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) self.assertRaises(vnfm.CpuAllocationInvalidValues, toscautils.get_flavor_dict, tosca)
def _generate_hot_from_tosca(self, vnfd_dict, dev_attrs): parsed_params = {} if 'param_values' in dev_attrs and dev_attrs['param_values'] != "": try: parsed_params = yaml.safe_load(dev_attrs['param_values']) except Exception as e: LOG.debug("Params not Well Formed: %s", str(e)) raise vnfm.ParamYAMLNotWellFormed(error_msg_details=str(e)) svcmonitoring_dict, vnfd_dicttemp = toscautils.get_vdu_servicemonitoring( vnfd_dict) toscautils.updateimports(vnfd_dicttemp) if 'substitution_mappings' in str(vnfd_dicttemp): toscautils.check_for_substitution_mappings(vnfd_dicttemp, parsed_params) try: tosca = tosca_template.ToscaTemplate(parsed_params=parsed_params, a_file=False, yaml_dict_tpl=vnfd_dicttemp) except Exception as e: LOG.debug("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) metadata = toscautils.get_vdu_metadata(tosca) monitoring_dict = toscautils.get_vdu_monitoring(tosca) mgmt_ports = toscautils.get_mgmt_ports(tosca) res_tpl = toscautils.get_resources_dict(tosca, self.STACK_FLAVOR_EXTRA) toscautils.post_process_template(tosca) try: translator = tosca_translator.TOSCATranslator(tosca, parsed_params) heat_template_yaml = translator.translate() except Exception as e: LOG.debug("heat-translator error: %s", str(e)) raise vnfm.HeatTranslatorFailed(error_msg_details=str(e)) heat_template_yaml = toscautils.post_process_heat_template( heat_template_yaml, mgmt_ports, metadata, res_tpl, self.unsupported_props) self.heat_template_yaml = heat_template_yaml self.monitoring_dict = monitoring_dict self.svcmonitoring_dict = svcmonitoring_dict self.metadata = metadata
def test_post_process_template(self): tosca_post_process_tpl = _get_template( 'test_tosca_post_process_template.yaml') vnfd_dict = yaml.safe_load(tosca_post_process_tpl) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=vnfd_dict) toscautils.post_process_template(tosca) invalidNodes = 0 deletedProperties = 0 convertedValues = 0 convertedProperties = 0 for nt in tosca.nodetemplates: if (nt.type_definition.is_derived_from(toscautils.MONITORING) or nt.type_definition.is_derived_from(toscautils.FAILURE) or nt.type_definition.is_derived_from( toscautils.PLACEMENT)): invalidNodes += 1 if nt.type in toscautils.delpropmap.keys(): for prop in toscautils.delpropmap[nt.type]: for p in nt.get_properties_objects(): if prop == p.name: deletedProperties += 1 if nt.type in toscautils.convert_prop_values: for prop in toscautils.convert_prop_values[nt.type].keys(): convertmap = toscautils.convert_prop_values[nt.type][prop] for p in nt.get_properties_objects(): if (prop == p.name and p.value in convertmap.keys()): convertedValues += 1 if nt.type in toscautils.convert_prop: for prop in toscautils.convert_prop[nt.type].keys(): for p in nt.get_properties_objects(): if prop == p.name: convertedProperties += 1 if nt.name == 'VDU1': vdu1_hints = nt.get_properties().get('scheduler_hints') vdu1_rsv = vdu1_hints.value.get('reservation') self.assertEqual(0, invalidNodes) self.assertEqual(0, deletedProperties) self.assertEqual(0, convertedValues) self.assertEqual(0, convertedProperties) self.assertEqual(vdu1_rsv, '459e94c9-efcd-4320-abf5-8c18cd82c331')
def test_create_delete_tosca_vnf_with_multiple_vdus(self): vnf_name = 'test_tosca_vnf_with_multiple_vdus' vnfd_file = 'sample-tosca-vnfd-multi-vdu.yaml' vnfd_instance, vnf_instance, tosca_dict = self.vnfd_and_vnf_create( vnfd_file, vnf_name) vnf_id = vnf_instance['vnf']['id'] self.wait_until_vnf_active(vnf_id, constants.VNF_CIRROS_CREATE_TIMEOUT, constants.ACTIVE_SLEEP_TIME) self.assertEqual('ACTIVE', self.client.show_vnf(vnf_id)['vnf']['status']) self.validate_vnf_instance(vnfd_instance, vnf_instance) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.PENDING_CREATE, cnt=2) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.ACTIVE) # Validate mgmt_ip_address with input yaml file mgmt_ip_address = self.client.show_vnf( vnf_id)['vnf']['mgmt_ip_address'] self.assertIsNotNone(mgmt_ip_address) mgmt_dict = yaml.safe_load(str(mgmt_ip_address)) toscautils.updateimports(tosca_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=tosca_dict) vdus = toscautils.findvdus(tosca) self.assertEqual(len(vdus), len(mgmt_dict.keys())) for vdu in vdus: self.assertIsNotNone(mgmt_dict[vdu.name]) self.assertEqual(True, utils.is_valid_ipv4(mgmt_dict[vdu.name])) # Delete vnf_instance with vnf_id try: self.client.delete_vnf(vnf_id) except Exception: assert False, "vnf Delete of test_vnf_with_multiple_vdus failed" self.wait_until_vnf_delete(vnf_id, constants.VNF_CIRROS_DELETE_TIMEOUT) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_DELETE, evt_constants.PENDING_DELETE, cnt=2)
def test_check_for_substitution_mappings(self): tosca_sb_map = _get_template('../../../../../etc/samples/test-nsd-' 'vnfd1.yaml') param = {'substitution_mappings': { 'VL2': {'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net0', 'vendor': 'tacker'}}, 'VL1': {'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net_mgmt', 'vendor': 'tacker'}}, 'requirements': {'virtualLink2': 'VL2', 'virtualLink1': 'VL1'}}} template = yaml.safe_load(tosca_sb_map) toscautils.updateimports(template) toscautils.check_for_substitution_mappings(template, param) self.assertNotIn('substitution_mappings', param)
def test_get_flavor_dict(self): vnfd_dict = yaml.safe_load(self.tosca_flavor) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 2, "disk": 10, "ram": 512 } } actual_flavor_dict = toscautils.get_flavor_dict(tosca) self.assertEqual(expected_flavor_dict, actual_flavor_dict)
def test_post_process_template(self): tosca_post_process_tpl = _get_template( 'test_tosca_post_process_template.yaml') vnfd_dict = yaml.safe_load(tosca_post_process_tpl) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=vnfd_dict) toscautils.post_process_template(tosca) invalidNodes = 0 deletedProperties = 0 convertedValues = 0 convertedProperties = 0 for nt in tosca.nodetemplates: if (nt.type_definition.is_derived_from(toscautils.MONITORING) or nt.type_definition.is_derived_from(toscautils.FAILURE) or nt.type_definition.is_derived_from(toscautils.PLACEMENT)): invalidNodes += 1 if nt.type in toscautils.delpropmap.keys(): for prop in toscautils.delpropmap[nt.type]: for p in nt.get_properties_objects(): if prop == p.name: deletedProperties += 1 if nt.type in toscautils.convert_prop_values: for prop in toscautils.convert_prop_values[nt.type].keys(): convertmap = toscautils.convert_prop_values[nt.type][prop] for p in nt.get_properties_objects(): if (prop == p.name and p.value in convertmap.keys()): convertedValues += 1 if nt.type in toscautils.convert_prop: for prop in toscautils.convert_prop[nt.type].keys(): for p in nt.get_properties_objects(): if prop == p.name: convertedProperties += 1 if nt.name == 'VDU1': vdu1_hints = nt.get_properties().get('scheduler_hints') vdu1_rsv = vdu1_hints.value.get('reservation') self.assertEqual(0, invalidNodes) self.assertEqual(0, deletedProperties) self.assertEqual(0, convertedValues) self.assertEqual(0, convertedProperties) self.assertEqual(vdu1_rsv, '459e94c9-efcd-4320-abf5-8c18cd82c331')
def validate_tosca(self, template): if "tosca_definitions_version" not in template: raise nfvo.ToscaParserFailed( error_msg_details='tosca_definitions_version missing in ' 'template') LOG.debug('template yaml: %s', template) toscautils.updateimports(template) try: tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=template) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise nfvo.ToscaParserFailed(error_msg_details=str(e))
def validate_tosca(self, template): if "tosca_definitions_version" not in template: raise nfvo.ToscaParserFailed( error_msg_details='tosca_definitions_version missing in ' 'template' ) LOG.debug('template yaml: %s', template) toscautils.updateimports(template) try: tosca_template.ToscaTemplate( a_file=False, yaml_dict_tpl=template) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise nfvo.ToscaParserFailed(error_msg_details=str(e))
def test_get_flavor_dict_extra_specs_all_numa_count(self): tosca_fes_all_numa_count = _get_template( 'tosca_flavor_all_numa_count.yaml') vnfd_dict = yaml.safe_load(tosca_fes_all_numa_count) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 8, "disk": 10, "ram": 4096, "extra_specs": { 'hw:cpu_policy': 'dedicated', 'hw:mem_page_size': 'any', 'hw:cpu_sockets': 2, 'hw:cpu_threads': 2, 'hw:numa_nodes': 2, 'hw:cpu_cores': 2, 'hw:cpu_threads_policy': 'avoid' } } } actual_flavor_dict = toscautils.get_flavor_dict(tosca) self.assertEqual(expected_flavor_dict, actual_flavor_dict)
def _parse_template_input(self, vnfd): vnfd_dict = vnfd['vnfd'] vnfd_yaml = vnfd_dict['attributes'].get('vnfd') if vnfd_yaml is None: return inner_vnfd_dict = yaml.safe_load(vnfd_yaml) LOG.debug('vnfd_dict: %s', inner_vnfd_dict) # Prepend the tacker_defs.yaml import file with the full # path to the file toscautils.updateimports(inner_vnfd_dict) try: tosca = ToscaTemplate(a_file=False, yaml_dict_tpl=inner_vnfd_dict) except Exception as e: LOG.exception("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) if ('description' not in vnfd_dict or vnfd_dict['description'] == ''): vnfd_dict['description'] = inner_vnfd_dict.get( 'description', '') if (('name' not in vnfd_dict or not len(vnfd_dict['name'])) and 'metadata' in inner_vnfd_dict): vnfd_dict['name'] = inner_vnfd_dict['metadata'].get( 'template_name', '') vnfd_dict['mgmt_driver'] = toscautils.get_mgmt_driver( tosca) if vnfd_dict['mgmt_driver'] not in cfg.CONF.tacker.mgmt_driver: LOG.error("Invalid mgmt_driver in TOSCA template") raise vnfm.InvalidMgmtDriver( mgmt_driver_name=vnfd_dict['mgmt_driver']) LOG.debug('vnfd %s', vnfd)
def _test_samples(self, files): if files: for f in self._get_list_of_sample(files): with open(f, 'r') as _f: yaml_dict = None try: yaml_dict = yamlparser.simple_ordered_parse(_f.read()) except: # noqa pass self.assertIsNotNone( yaml_dict, "Yaml parser failed to parse %s" % f) utils.updateimports(yaml_dict) tosca = None try: tosca = tosca_template.ToscaTemplate( a_file=False, yaml_dict_tpl=yaml_dict) except: # noqa pass self.assertIsNotNone( tosca, "Tosca parser failed to parse %s" % f) utils.post_process_template(tosca) hot = None try: hot = tosca_translator.TOSCATranslator(tosca, {}).translate() except: # noqa pass self.assertIsNotNone( hot, "Heat-translator failed to translate %s" % f)
def _generate_hot_from_tosca(self, vnfd_dict, dev_attrs, inst_req_info=None, grant_info=None): parsed_params = {} if 'param_values' in dev_attrs and dev_attrs['param_values'] != "": try: parsed_params = yaml.safe_load(dev_attrs['param_values']) except Exception as e: LOG.debug("Params not Well Formed: %s", str(e)) raise vnfm.ParamYAMLNotWellFormed(error_msg_details=str(e)) appmonitoring_dict = \ toscautils.get_vdu_applicationmonitoring(vnfd_dict) block_storage_details = toscautils.get_block_storage_details(vnfd_dict) toscautils.updateimports(vnfd_dict) if 'substitution_mappings' in str(vnfd_dict): toscautils.check_for_substitution_mappings(vnfd_dict, parsed_params) try: tosca = tosca_template.ToscaTemplate(parsed_params=parsed_params, a_file=False, yaml_dict_tpl=vnfd_dict) except Exception as e: LOG.debug("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) unique_id = uuidutils.generate_uuid() metadata = toscautils.get_vdu_metadata(tosca, unique_id=unique_id) for policy in tosca.policies: if policy.entity_tpl['type'] == constants.POLICY_RESERVATION: metadata = toscautils.get_metadata_for_reservation( tosca, metadata) break alarm_resources = toscautils.pre_process_alarm_resources( self.vnf, tosca, metadata, unique_id=unique_id) monitoring_dict = toscautils.get_vdu_monitoring(tosca) mgmt_ports = toscautils.get_mgmt_ports(tosca) res_tpl = toscautils.get_resources_dict(tosca, self.STACK_FLAVOR_EXTRA) toscautils.post_process_template(tosca) scaling_policy_names = toscautils.get_scaling_policy(tosca) try: translator = tosca_translator.TOSCATranslator(tosca, parsed_params) heat_template_yaml = translator.translate() nested_resource_names = toscautils.get_nested_resources_name( heat_template_yaml) if nested_resource_names: for nested_resource_name in nested_resource_names: sub_heat_tmpl_name = \ toscautils.get_sub_heat_tmpl_name(nested_resource_name) sub_heat_template_yaml =\ translator.translate_to_yaml_files_dict( sub_heat_tmpl_name) nested_resource_yaml = \ sub_heat_template_yaml[nested_resource_name] LOG.debug("nested_resource_yaml: %s", nested_resource_yaml) self.nested_resources[nested_resource_name] = \ nested_resource_yaml except Exception as e: LOG.debug("heat-translator error: %s", str(e)) raise vnfm.HeatTranslatorFailed(error_msg_details=str(e)) if self.nested_resources: nested_tpl = toscautils.update_nested_scaling_resources( self.nested_resources, mgmt_ports, metadata, res_tpl, self.unsupported_props, grant_info=grant_info, inst_req_info=inst_req_info) self.fields['files'] = nested_tpl for nested_resource_name in nested_tpl.keys(): self.vnf['attributes'][nested_resource_name] =\ nested_tpl[nested_resource_name] mgmt_ports.clear() if scaling_policy_names: scaling_group_dict = toscautils.get_scaling_group_dict( heat_template_yaml, scaling_policy_names) self.vnf['attributes']['scaling_group_names'] =\ jsonutils.dump_as_bytes(scaling_group_dict) if self.vnf['attributes'].get('maintenance', None): toscautils.add_maintenance_resources(tosca, res_tpl) heat_template_yaml = toscautils.post_process_heat_template( heat_template_yaml, mgmt_ports, metadata, alarm_resources, res_tpl, block_storage_details, self.unsupported_props, unique_id=unique_id, inst_req_info=inst_req_info, grant_info=grant_info, tosca=tosca) try: for nested_resource_name in self.nested_resources.keys(): self.nested_resources[nested_resource_name] = \ toscautils.post_process_heat_template_for_scaling( self.nested_resources[nested_resource_name], mgmt_ports, metadata, alarm_resources, res_tpl, block_storage_details, self.unsupported_props, unique_id=unique_id, inst_req_info=inst_req_info, grant_info=grant_info, tosca=tosca) except Exception as e: LOG.debug("post_process_heat_template_for_scaling " "error: %s", str(e)) raise self.heat_template_yaml = heat_template_yaml self.monitoring_dict = monitoring_dict self.metadata = metadata self.appmonitoring_dict = appmonitoring_dict
def test_create_delete_tosca_vnf_with_multiple_vdus(self): input_yaml = read_file('sample-tosca-vnfd-multi-vdu.yaml') tosca_dict = yaml.safe_load(input_yaml) vnfd_name = 'sample-tosca-vnfd-multi-vdu' tosca_arg = {'vnfd': {'name': vnfd_name, 'attributes': {'vnfd': tosca_dict}}} # Create vnfd with tosca template vnfd_instance = self.client.create_vnfd(body=tosca_arg) self.assertIsNotNone(vnfd_instance) # Create vnf with vnfd_id vnfd_id = vnfd_instance['vnfd']['id'] vnf_arg = {'vnf': {'vnfd_id': vnfd_id, 'name': "test_tosca_vnf_with_multiple_vdus"}} vnf_instance = self.client.create_vnf(body=vnf_arg) vnf_id = vnf_instance['vnf']['id'] self.wait_until_vnf_active(vnf_id, constants.VNF_CIRROS_CREATE_TIMEOUT, constants.ACTIVE_SLEEP_TIME) self.assertEqual('ACTIVE', self.client.show_vnf(vnf_id)['vnf']['status']) self.validate_vnf_instance(vnfd_instance, vnf_instance) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.PENDING_CREATE, cnt=2) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.ACTIVE) # Validate mgmt_url with input yaml file mgmt_url = self.client.show_vnf(vnf_id)['vnf']['mgmt_url'] self.assertIsNotNone(mgmt_url) mgmt_dict = yaml.safe_load(str(mgmt_url)) input_dict = yaml.safe_load(input_yaml) toscautils.updateimports(input_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=input_dict) vdus = toscautils.findvdus(tosca) self.assertEqual(len(vdus), len(mgmt_dict.keys())) for vdu in vdus: self.assertIsNotNone(mgmt_dict[vdu.name]) self.assertEqual(True, utils.is_valid_ipv4(mgmt_dict[vdu.name])) # Delete vnf_instance with vnf_id try: self.client.delete_vnf(vnf_id) except Exception: assert False, "vnf Delete of test_vnf_with_multiple_vdus failed" self.wait_until_vnf_delete(vnf_id, constants.VNF_CIRROS_DELETE_TIMEOUT) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_DELETE, evt_constants.PENDING_DELETE, cnt=2) # Delete vnfd_instance self.addCleanup(self.client.delete_vnfd, vnfd_id)
def test_create_delete_tosca_vnf_with_multiple_vdus(self): input_yaml = read_file('sample-tosca-vnfd-multi-vdu.yaml') tosca_dict = yaml.safe_load(input_yaml) vnfd_name = 'sample-tosca-vnfd-multi-vdu' tosca_arg = {'vnfd': {'name': vnfd_name, 'attributes': {'vnfd': tosca_dict}}} # Create vnfd with tosca template vnfd_instance = self.client.create_vnfd(body=tosca_arg) self.assertIsNotNone(vnfd_instance) # Create vnf with vnfd_id vnfd_id = vnfd_instance['vnfd']['id'] vnf_arg = {'vnf': {'vnfd_id': vnfd_id, 'name': "test_tosca_vnf_with_multiple_vdus"}} vnf_instance = self.client.create_vnf(body=vnf_arg) vnf_id = vnf_instance['vnf']['id'] self.wait_until_vnf_active(vnf_id, constants.VNF_CIRROS_CREATE_TIMEOUT, constants.ACTIVE_SLEEP_TIME) self.assertEqual('ACTIVE', self.client.show_vnf(vnf_id)['vnf']['status']) self.validate_vnf_instance(vnfd_instance, vnf_instance) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.PENDING_CREATE, cnt=2) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.ACTIVE) # Validate mgmt_ip_address with input yaml file mgmt_ip_address = self.client.show_vnf(vnf_id)['vnf'][ 'mgmt_ip_address'] self.assertIsNotNone(mgmt_ip_address) mgmt_dict = yaml.safe_load(str(mgmt_ip_address)) input_dict = yaml.safe_load(input_yaml) toscautils.updateimports(input_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=input_dict) vdus = toscautils.findvdus(tosca) self.assertEqual(len(vdus), len(mgmt_dict.keys())) for vdu in vdus: self.assertIsNotNone(mgmt_dict[vdu.name]) self.assertEqual(True, utils.is_valid_ipv4(mgmt_dict[vdu.name])) # Delete vnf_instance with vnf_id try: self.client.delete_vnf(vnf_id) except Exception: assert False, "vnf Delete of test_vnf_with_multiple_vdus failed" self.wait_until_vnf_delete(vnf_id, constants.VNF_CIRROS_DELETE_TIMEOUT) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_DELETE, evt_constants.PENDING_DELETE, cnt=2) # Delete vnfd_instance self.addCleanup(self.client.delete_vnfd, vnfd_id)
def test_create_delete_tosca_vnfc(self): input_yaml = read_file('sample_tosca_vnfc.yaml') tosca_dict = yaml.safe_load(input_yaml) path = os.path.abspath( os.path.join(os.path.dirname(__file__), "../../etc/samples")) vnfd_name = 'sample-tosca-vnfc' tosca_dict['topology_template']['node_templates' ]['firewall_vnfc' ]['interfaces' ]['Standard']['create'] = path \ + '/install_vnfc.sh' tosca_arg = { 'vnfd': { 'name': vnfd_name, 'attributes': { 'vnfd': tosca_dict } } } # Create vnfd with tosca template vnfd_instance = self.client.create_vnfd(body=tosca_arg) self.assertIsNotNone(vnfd_instance) # Create vnf with vnfd_id vnfd_id = vnfd_instance['vnfd']['id'] self.addCleanup(self.client.delete_vnfd, vnfd_id) vnf_arg = {'vnf': {'vnfd_id': vnfd_id, 'name': "test_tosca_vnfc"}} vnf_instance = self.client.create_vnf(body=vnf_arg) vnf_id = vnf_instance['vnf']['id'] self.wait_until_vnf_active(vnf_id, constants.VNFC_CREATE_TIMEOUT, constants.ACTIVE_SLEEP_TIME) self.assertEqual('ACTIVE', self.client.show_vnf(vnf_id)['vnf']['status']) self.validate_vnf_instance(vnfd_instance, vnf_instance) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.PENDING_CREATE, cnt=2) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.ACTIVE) # Validate mgmt_ip_address with input yaml file mgmt_ip_address = self.client.show_vnf( vnf_id)['vnf']['mgmt_ip_address'] self.assertIsNotNone(mgmt_ip_address) mgmt_dict = yaml.safe_load(str(mgmt_ip_address)) input_dict = yaml.safe_load(input_yaml) toscautils.updateimports(input_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=input_dict) vdus = toscautils.findvdus(tosca) self.assertEqual(len(vdus), len(mgmt_dict.keys())) for vdu in vdus: self.assertIsNotNone(mgmt_dict[vdu.name]) self.assertEqual(True, utils.is_valid_ipv4(mgmt_dict[vdu.name])) # Check the status of SoftwareDeployment heat_stack_id = self.client.show_vnf(vnf_id)['vnf']['instance_id'] resource_types = self.h_client.resources resources = resource_types.list(stack_id=heat_stack_id) for resource in resources: resource = resource.to_dict() if resource['resource_type'] == \ SOFTWARE_DEPLOYMENT: self.assertEqual('CREATE_COMPLETE', resource['resource_status']) break # Delete vnf_instance with vnf_id try: self.client.delete_vnf(vnf_id) except Exception: assert False, "vnf Delete of test_vnf_with_multiple_vdus failed" self.wait_until_vnf_delete(vnf_id, constants.VNF_CIRROS_DELETE_TIMEOUT) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_DELETE, evt_constants.PENDING_DELETE, cnt=2)
def _generate_hot_from_tosca(self, vnfd_dict, dev_attrs): parsed_params = {} if 'param_values' in dev_attrs and dev_attrs['param_values'] != "": try: parsed_params = yaml.safe_load(dev_attrs['param_values']) except Exception as e: LOG.debug("Params not Well Formed: %s", str(e)) raise vnfm.ParamYAMLNotWellFormed(error_msg_details=str(e)) appmonitoring_dict = \ toscautils.get_vdu_applicationmonitoring(vnfd_dict) block_storage_details = toscautils.get_block_storage_details( vnfd_dict) toscautils.updateimports(vnfd_dict) if 'substitution_mappings' in str(vnfd_dict): toscautils.check_for_substitution_mappings( vnfd_dict, parsed_params ) try: tosca = tosca_template.ToscaTemplate(parsed_params=parsed_params, a_file=False, yaml_dict_tpl=vnfd_dict) except Exception as e: LOG.debug("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) unique_id = uuidutils.generate_uuid() metadata = toscautils.get_vdu_metadata(tosca, unique_id=unique_id) for policy in tosca.policies: if policy.entity_tpl['type'] == constants.POLICY_RESERVATION: metadata = toscautils.get_metadata_for_reservation( tosca, metadata) break alarm_resources = toscautils.pre_process_alarm_resources( self.vnf, tosca, metadata, unique_id=unique_id) monitoring_dict = toscautils.get_vdu_monitoring(tosca) mgmt_ports = toscautils.get_mgmt_ports(tosca) nested_resource_name = toscautils.get_nested_resources_name(tosca) sub_heat_tmpl_name = toscautils.get_sub_heat_tmpl_name(tosca) res_tpl = toscautils.get_resources_dict(tosca, self.STACK_FLAVOR_EXTRA) toscautils.post_process_template(tosca) scaling_policy_names = toscautils.get_scaling_policy(tosca) try: translator = tosca_translator.TOSCATranslator(tosca, parsed_params) heat_template_yaml = translator.translate() if nested_resource_name: sub_heat_template_yaml =\ translator.translate_to_yaml_files_dict(sub_heat_tmpl_name) nested_resource_yaml =\ sub_heat_template_yaml[nested_resource_name] LOG.debug("nested_resource_yaml: %s", nested_resource_yaml) self.nested_resources[nested_resource_name] =\ nested_resource_yaml except Exception as e: LOG.debug("heat-translator error: %s", str(e)) raise vnfm.HeatTranslatorFailed(error_msg_details=str(e)) if self.nested_resources: nested_tpl = toscautils.update_nested_scaling_resources( self.nested_resources, mgmt_ports, metadata, res_tpl, self.unsupported_props) self.fields['files'] = nested_tpl self.vnf['attributes'][nested_resource_name] =\ nested_tpl[nested_resource_name] mgmt_ports.clear() if scaling_policy_names: scaling_group_dict = toscautils.get_scaling_group_dict( heat_template_yaml, scaling_policy_names) self.vnf['attributes']['scaling_group_names'] =\ jsonutils.dump_as_bytes(scaling_group_dict) heat_template_yaml = toscautils.post_process_heat_template( heat_template_yaml, mgmt_ports, metadata, alarm_resources, res_tpl, block_storage_details, self.unsupported_props, unique_id=unique_id) self.heat_template_yaml = heat_template_yaml self.monitoring_dict = monitoring_dict self.metadata = metadata self.appmonitoring_dict = appmonitoring_dict
class TestToscaUtils(testtools.TestCase): tosca_openwrt = _get_template('test_tosca_openwrt.yaml') vnfd_dict = yaml.safe_load(tosca_openwrt) toscautils.updateimports(vnfd_dict) def setUp(self): super(TestToscaUtils, self).setUp() self.tosca = tosca_template.ToscaTemplate( parsed_params={}, a_file=False, yaml_dict_tpl=self.vnfd_dict) self.tosca_flavor = _get_template('test_tosca_flavor.yaml') def test_updateimport(self): importspath = os.path.abspath('./tacker/tosca/lib/') file1 = importspath + '/tacker_defs.yaml' file2 = importspath + '/tacker_nfv_defs.yaml' expected_imports = [file1, file2] self.assertEqual(expected_imports, self.vnfd_dict['imports']) def test_get_mgmt_driver(self): expected_mgmt_driver = 'openwrt' mgmt_driver = toscautils.get_mgmt_driver(self.tosca) self.assertEqual(expected_mgmt_driver, mgmt_driver) def test_get_vdu_monitoring(self): expected_monitoring = {'vdus': {'VDU1': {'ping': { 'actions': {'failure': 'respawn'}, 'name': 'ping', 'parameters': {'count': 3, 'interval': 10}, 'monitoring_params': {'count': 3, 'interval': 10}}}}} monitoring = toscautils.get_vdu_monitoring(self.tosca) self.assertEqual(expected_monitoring, monitoring) def test_get_mgmt_ports(self): expected_mgmt_ports = {'mgmt_ip-VDU1': 'CP1'} mgmt_ports = toscautils.get_mgmt_ports(self.tosca) self.assertEqual(expected_mgmt_ports, mgmt_ports) def test_post_process_template(self): tosca_post_process_tpl = _get_template( 'test_tosca_post_process_template.yaml') vnfd_dict = yaml.safe_load(tosca_post_process_tpl) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=vnfd_dict) toscautils.post_process_template(tosca) invalidNodes = 0 deletedProperties = 0 convertedValues = 0 convertedProperties = 0 for nt in tosca.nodetemplates: if (nt.type_definition.is_derived_from(toscautils.MONITORING) or nt.type_definition.is_derived_from(toscautils.FAILURE) or nt.type_definition.is_derived_from(toscautils.PLACEMENT)): invalidNodes += 1 if nt.type in toscautils.delpropmap: for prop in toscautils.delpropmap[nt.type]: for p in nt.get_properties_objects(): if prop == p.name: deletedProperties += 1 if nt.type in toscautils.convert_prop_values: for prop in toscautils.convert_prop_values[nt.type]: convertmap = toscautils.convert_prop_values[nt.type][prop] for p in nt.get_properties_objects(): if (prop == p.name and p.value in convertmap): convertedValues += 1 if nt.type in toscautils.convert_prop: for prop in toscautils.convert_prop[nt.type]: for p in nt.get_properties_objects(): if prop == p.name: convertedProperties += 1 if nt.name == 'VDU1': vdu1_hints = nt.get_properties().get('scheduler_hints') vdu1_rsv = vdu1_hints.value.get('reservation') self.assertEqual(0, invalidNodes) self.assertEqual(0, deletedProperties) self.assertEqual(0, convertedValues) self.assertEqual(0, convertedProperties) self.assertEqual(vdu1_rsv, '459e94c9-efcd-4320-abf5-8c18cd82c331') def test_post_process_heat_template(self): tosca1 = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=self.vnfd_dict) toscautils.post_process_template(tosca1) translator = tosca_translator.TOSCATranslator(tosca1, {}) heat_template_yaml = translator.translate() expected_heat_tpl = _get_template('hot_tosca_openwrt.yaml') mgmt_ports = toscautils.get_mgmt_ports(self.tosca) heat_tpl = toscautils.post_process_heat_template( heat_template_yaml, mgmt_ports, {}, {}, {}) heatdict = yaml.safe_load(heat_tpl) expecteddict = yaml.safe_load(expected_heat_tpl) self.assertEqual(expecteddict, heatdict) def test_findvdus(self): vdus = toscautils.findvdus(self.tosca) self.assertEqual(1, len(vdus)) for vdu in vdus: self.assertEqual(True, vdu.type_definition.is_derived_from( toscautils.TACKERVDU)) def test_get_flavor_dict(self): vnfd_dict = yaml.safe_load(self.tosca_flavor) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 2, "disk": 10, "ram": 512 } } actual_flavor_dict = toscautils.get_flavor_dict(tosca) self.assertEqual(expected_flavor_dict, actual_flavor_dict) def test_add_resources_tpl_for_flavor(self): dummy_heat_dict = yaml.safe_load(_get_template( 'hot_flavor_and_capabilities.yaml')) expected_dict = yaml.safe_load(_get_template('hot_flavor.yaml')) dummy_heat_res = { "flavor": { "VDU1": { "vcpus": 2, "ram": 512, "disk": 10 } } } toscautils.add_resources_tpl(dummy_heat_dict, dummy_heat_res) self.assertEqual(expected_dict, dummy_heat_dict) def test_get_flavor_dict_extra_specs_all_numa_count(self): tosca_fes_all_numa_count = _get_template( 'tosca_flavor_all_numa_count.yaml') vnfd_dict = yaml.safe_load(tosca_fes_all_numa_count) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 8, "disk": 10, "ram": 4096, "extra_specs": { 'hw:cpu_policy': 'dedicated', 'hw:mem_page_size': 'any', 'hw:cpu_sockets': 2, 'hw:cpu_threads': 2, 'hw:numa_nodes': 2, 'hw:cpu_cores': 2, 'hw:cpu_threads_policy': 'avoid' } } } actual_flavor_dict = toscautils.get_flavor_dict(tosca) self.assertEqual(expected_flavor_dict, actual_flavor_dict) def test_get_flavor_dict_with_wrong_cpu(self): tosca_fes = _get_template( 'tosca_flavor_with_wrong_cpu.yaml') vnfd_dict = yaml.safe_load(tosca_fes) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) self.assertRaises(vnfm.CpuAllocationInvalidValues, toscautils.get_flavor_dict, tosca) def test_tacker_conf_heat_extra_specs_all_numa_count(self): tosca_fes_all_numa_count = _get_template( 'tosca_flavor_all_numa_count.yaml') vnfd_dict = yaml.safe_load(tosca_fes_all_numa_count) toscautils.updateimports(vnfd_dict) tosca = tosca_template.ToscaTemplate(a_file=False, yaml_dict_tpl=vnfd_dict) expected_flavor_dict = { "VDU1": { "vcpus": 8, "disk": 10, "ram": 4096, "extra_specs": { 'hw:cpu_policy': 'dedicated', 'hw:mem_page_size': 'any', 'hw:cpu_sockets': 2, 'hw:cpu_threads': 2, 'hw:numa_nodes': 2, 'hw:cpu_cores': 2, 'hw:cpu_threads_policy': 'avoid', 'aggregate_instance_extra_specs:nfv': 'true' } } } actual_flavor_dict = toscautils.get_flavor_dict( tosca, {"aggregate_instance_extra_specs:nfv": "true"}) self.assertEqual(expected_flavor_dict, actual_flavor_dict) def test_add_resources_tpl_for_image(self): dummy_heat_dict = yaml.safe_load(_get_template( 'hot_image_before_processed_image.yaml')) expected_dict = yaml.safe_load(_get_template( 'hot_image_after_processed_image.yaml')) dummy_heat_res = { "image": { "VDU1": { "location": "http://URL/v1/openwrt.qcow2", "container_format": "bare", "disk_format": "raw" } } } toscautils.add_resources_tpl(dummy_heat_dict, dummy_heat_res) self.assertEqual(expected_dict, dummy_heat_dict) def test_convert_unsupported_res_prop_kilo_ver(self): unsupported_res_prop_dict = {'OS::Neutron::Port': { 'port_security_enabled': 'value_specs', }, } dummy_heat_dict = yaml.safe_load(_get_template( 'hot_tosca_openwrt.yaml')) expected_heat_dict = yaml.safe_load(_get_template( 'hot_tosca_openwrt_kilo.yaml')) toscautils.convert_unsupported_res_prop(dummy_heat_dict, unsupported_res_prop_dict) self.assertEqual(expected_heat_dict, dummy_heat_dict) def test_check_for_substitution_mappings(self): tosca_sb_map = _get_template('../../../../../etc/samples/test-nsd-' 'vnfd1.yaml') param = {'substitution_mappings': { 'VL2': {'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net0', 'vendor': 'tacker'}}, 'VL1': {'type': 'tosca.nodes.nfv.VL', 'properties': { 'network_name': 'net_mgmt', 'vendor': 'tacker'}}, 'requirements': {'virtualLink2': 'VL2', 'virtualLink1': 'VL1'}}} template = yaml.safe_load(tosca_sb_map) toscautils.updateimports(template) toscautils.check_for_substitution_mappings(template, param) self.assertNotIn('substitution_mappings', param) def test_get_block_storage_details(self): tosca_vol = _get_template('tosca_block_storage.yaml') vnfd_dict = yaml.safe_load(tosca_vol) expected_dict = { 'volumes': { 'VB1': { 'image': 'cirros-0.4.0-x86_64-disk', 'size': '1' } }, 'volume_attachments': { 'CB1': { 'instance_uuid': {'get_resource': 'VDU1'}, 'mountpoint': '/dev/vdb', 'volume_id': {'get_resource': 'VB1'}} } } volume_details = toscautils.get_block_storage_details(vnfd_dict) self.assertEqual(expected_dict, volume_details)
def _generate_hot_from_tosca(self, vnfd_dict, dev_attrs): parsed_params = {} if 'param_values' in dev_attrs and dev_attrs['param_values'] != "": try: parsed_params = yaml.safe_load(dev_attrs['param_values']) except Exception as e: LOG.debug("Params not Well Formed: %s", str(e)) raise vnfm.ParamYAMLNotWellFormed(error_msg_details=str(e)) appmonitoring_dict = \ toscautils.get_vdu_applicationmonitoring(vnfd_dict) block_storage_details = toscautils.get_block_storage_details(vnfd_dict) toscautils.updateimports(vnfd_dict) if 'substitution_mappings' in str(vnfd_dict): toscautils.check_for_substitution_mappings(vnfd_dict, parsed_params) try: tosca = tosca_template.ToscaTemplate(parsed_params=parsed_params, a_file=False, yaml_dict_tpl=vnfd_dict) except Exception as e: LOG.debug("tosca-parser error: %s", str(e)) raise vnfm.ToscaParserFailed(error_msg_details=str(e)) metadata = toscautils.get_vdu_metadata(tosca) alarm_resources =\ toscautils.pre_process_alarm_resources(self.vnf, tosca, metadata) monitoring_dict = toscautils.get_vdu_monitoring(tosca) mgmt_ports = toscautils.get_mgmt_ports(tosca) nested_resource_name = toscautils.get_nested_resources_name(tosca) sub_heat_tmpl_name = toscautils.get_sub_heat_tmpl_name(tosca) res_tpl = toscautils.get_resources_dict(tosca, self.STACK_FLAVOR_EXTRA) toscautils.post_process_template(tosca) scaling_policy_names = toscautils.get_scaling_policy(tosca) try: translator = tosca_translator.TOSCATranslator(tosca, parsed_params) heat_template_yaml = translator.translate() if nested_resource_name: sub_heat_template_yaml =\ translator.translate_to_yaml_files_dict(sub_heat_tmpl_name) nested_resource_yaml =\ sub_heat_template_yaml[nested_resource_name] LOG.debug("nested_resource_yaml: %s", nested_resource_yaml) self.nested_resources[nested_resource_name] =\ nested_resource_yaml except Exception as e: LOG.debug("heat-translator error: %s", str(e)) raise vnfm.HeatTranslatorFailed(error_msg_details=str(e)) if self.nested_resources: nested_tpl = toscautils.update_nested_scaling_resources( self.nested_resources, mgmt_ports, metadata, res_tpl, self.unsupported_props) self.fields['files'] = nested_tpl self.vnf['attributes'][nested_resource_name] =\ nested_tpl[nested_resource_name] mgmt_ports.clear() if scaling_policy_names: scaling_group_dict = toscautils.get_scaling_group_dict( heat_template_yaml, scaling_policy_names) self.vnf['attributes']['scaling_group_names'] =\ jsonutils.dumps(scaling_group_dict) heat_template_yaml = toscautils.post_process_heat_template( heat_template_yaml, mgmt_ports, metadata, alarm_resources, res_tpl, block_storage_details, self.unsupported_props) self.heat_template_yaml = heat_template_yaml self.monitoring_dict = monitoring_dict self.metadata = metadata self.appmonitoring_dict = appmonitoring_dict
def test_create_delete_tosca_vnfc(self): input_yaml = read_file('sample_tosca_vnfc.yaml') tosca_dict = yaml.safe_load(input_yaml) path = os.path.abspath(os.path.join( os.path.dirname(__file__), "../../etc/samples")) vnfd_name = 'sample-tosca-vnfc' tosca_dict['topology_template']['node_templates' ]['firewall_vnfc' ]['interfaces' ]['Standard']['create'] = path \ + '/install_vnfc.sh' tosca_arg = {'vnfd': {'name': vnfd_name, 'attributes': {'vnfd': tosca_dict}}} # Create vnfd with tosca template vnfd_instance = self.client.create_vnfd(body=tosca_arg) self.assertIsNotNone(vnfd_instance) # Create vnf with vnfd_id vnfd_id = vnfd_instance['vnfd']['id'] vnf_arg = {'vnf': {'vnfd_id': vnfd_id, 'name': "test_tosca_vnfc"}} vnf_instance = self.client.create_vnf(body=vnf_arg) vnf_id = vnf_instance['vnf']['id'] self.wait_until_vnf_active(vnf_id, constants.VNFC_CREATE_TIMEOUT, constants.ACTIVE_SLEEP_TIME) self.assertEqual('ACTIVE', self.client.show_vnf(vnf_id)['vnf']['status']) self.validate_vnf_instance(vnfd_instance, vnf_instance) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.PENDING_CREATE, cnt=2) self.verify_vnf_crud_events( vnf_id, evt_constants.RES_EVT_CREATE, evt_constants.ACTIVE) # Validate mgmt_ip_address with input yaml file mgmt_ip_address = self.client.show_vnf(vnf_id)['vnf'][ 'mgmt_ip_address'] self.assertIsNotNone(mgmt_ip_address) mgmt_dict = yaml.safe_load(str(mgmt_ip_address)) input_dict = yaml.safe_load(input_yaml) toscautils.updateimports(input_dict) tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=input_dict) vdus = toscautils.findvdus(tosca) self.assertEqual(len(vdus), len(mgmt_dict.keys())) for vdu in vdus: self.assertIsNotNone(mgmt_dict[vdu.name]) self.assertEqual(True, utils.is_valid_ipv4(mgmt_dict[vdu.name])) # Check the status of SoftwareDeployment heat_stack_id = self.client.show_vnf(vnf_id)['vnf']['instance_id'] resource_types = self.h_client.resources resources = resource_types.list(stack_id=heat_stack_id) for resource in resources: resource = resource.to_dict() if resource['resource_type'] == \ SOFTWARE_DEPLOYMENT: self.assertEqual('CREATE_COMPLETE', resource['resource_status']) break # Delete vnf_instance with vnf_id try: self.client.delete_vnf(vnf_id) except Exception: assert False, "vnf Delete of test_vnf_with_multiple_vdus failed" self.wait_until_vnf_delete(vnf_id, constants.VNF_CIRROS_DELETE_TIMEOUT) self.verify_vnf_crud_events(vnf_id, evt_constants.RES_EVT_DELETE, evt_constants.PENDING_DELETE, cnt=2) # Delete vnfd_instance self.addCleanup(self.client.delete_vnfd, vnfd_id)