示例#1
0
    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_get_flavor_dict(self):
     vnfd_dict = yaml.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)
示例#3
0
    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.load(vnfd_yaml)
        LOG.debug(_('vnfd_dict: %s'), inner_vnfd_dict)

        if 'tosca_definitions_version' in 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)
        else:
            KEY_LIST = (('name', 'template_name'),
                        ('description', 'description'))

            vnfd_dict.update(
                dict((key, inner_vnfd_dict[vnfd_key]) for (key, vnfd_key)
                     in KEY_LIST
                     if ((key not in vnfd_dict or
                          vnfd_dict[key] == '') and
                         vnfd_key in inner_vnfd_dict and
                         inner_vnfd_dict[vnfd_key] != '')))

            service_types = inner_vnfd_dict.get(
                'service_properties', {}).get('type', [])
            if service_types:
                vnfd_dict.setdefault('service_types', []).extend(
                    [{'service_type': service_type}
                    for service_type in service_types])
            # TODO(anyone)  - this code assumes one mgmt_driver per VNFD???
            for vdu in inner_vnfd_dict.get('vdus', {}).values():
                mgmt_driver = vdu.get('mgmt_driver')
                if mgmt_driver:
                    vnfd_dict['mgmt_driver'] = mgmt_driver
        LOG.debug(_('vnfd %s'), vnfd)
示例#4
0
    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.load(vnfd_yaml)
        LOG.debug(_('vnfd_dict: %s'), inner_vnfd_dict)

        if 'tosca_definitions_version' in 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)
        else:
            KEY_LIST = (('name', 'template_name'),
                        ('description', 'description'))

            vnfd_dict.update(
                dict((key, inner_vnfd_dict[vnfd_key]) for (key, vnfd_key)
                     in KEY_LIST
                     if ((key not in vnfd_dict or
                          vnfd_dict[key] == '') and
                         vnfd_key in inner_vnfd_dict and
                         inner_vnfd_dict[vnfd_key] != '')))

            service_types = inner_vnfd_dict.get(
                'service_properties', {}).get('type', [])
            if service_types:
                vnfd_dict.setdefault('service_types', []).extend(
                    [{'service_type': service_type}
                    for service_type in service_types])
            # TODO(anyone)  - this code assumes one mgmt_driver per VNFD???
            for vdu in inner_vnfd_dict.get('vdus', {}).values():
                mgmt_driver = vdu.get('mgmt_driver')
                if mgmt_driver:
                    vnfd_dict['mgmt_driver'] = mgmt_driver
        LOG.debug(_('vnfd %s'), vnfd)
    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,
            vnf_instance["vnf"][evt_constants.RES_EVT_CREATED_FLD],
        )
        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.load(str(mgmt_url))

        input_dict = yaml.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)
示例#6
0
    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['imports']
        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)
示例#7
0
    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.load(vnfd_yaml)
        LOG.debug(_("vnfd_dict: %s"), inner_vnfd_dict)

        if "tosca_definitions_version" in 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)
        else:
            KEY_LIST = (("name", "template_name"), ("description", "description"))

            vnfd_dict.update(
                dict(
                    (key, inner_vnfd_dict[vnfd_key])
                    for (key, vnfd_key) in KEY_LIST
                    if (
                        (key not in vnfd_dict or vnfd_dict[key] == "")
                        and vnfd_key in inner_vnfd_dict
                        and inner_vnfd_dict[vnfd_key] != ""
                    )
                )
            )

            service_types = inner_vnfd_dict.get("service_properties", {}).get("type", [])
            if service_types:
                vnfd_dict.setdefault("service_types", []).extend(
                    [{"service_type": service_type} for service_type in service_types]
                )
            # TODO(anyone)  - this code assumes one mgmt_driver per VNFD???
            for vdu in inner_vnfd_dict.get("vdus", {}).values():
                mgmt_driver = vdu.get("mgmt_driver")
                if mgmt_driver:
                    vnfd_dict["mgmt_driver"] = mgmt_driver
        LOG.debug(_("vnfd %s"), vnfd)
示例#8
0
 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.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)
示例#10
0
    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))
示例#11
0
    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))
示例#12
0
    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.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))

        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)
        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.metadata = metadata
示例#13
0
 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.load(tosca_fes_all_numa_count)
     toscautils.updateimports(vnfd_dict)
     tosca = 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)
示例#14
0
 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.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)
示例#15
0
        def generate_hot_from_tosca(vnfd_dict):
            parsed_params = {}
            if ('param_values' in dev_attrs and
                    dev_attrs['param_values'] != ""):
                try:
                    parsed_params = yaml.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))

            toscautils.updateimports(vnfd_dict)

            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))

            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, res_tpl,
                unsupported_res_prop)

            return heat_template_yaml, monitoring_dict
示例#16
0
文件: heat.py 项目: swjang/tacker
        def generate_hot_from_tosca(vnfd_dict):
            parsed_params = {}
            if ('param_values' in dev_attrs and
                    dev_attrs['param_values'] != ""):
                try:
                    parsed_params = yaml.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))

            toscautils.updateimports(vnfd_dict)

            try:
                tosca = 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))

            monitoring_dict = toscautils.get_vdu_monitoring(tosca)
            mgmt_ports = toscautils.get_mgmt_ports(tosca)
            res_tpl = toscautils.get_resources_dict(tosca,
                                                    STACK_FLAVOR_EXTRA)
            toscautils.post_process_template(tosca)
            try:
                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, res_tpl,
                unsupported_res_prop)

            return heat_template_yaml, monitoring_dict
    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)

                    hot = None
                    try:
                        hot = tosca_translator.TOSCATranslator(tosca,
                                                               {}).translate()
                    except:  # noqa
                        pass

                    self.assertIsNotNone(
                        hot,
                        "Heat-translator failed to translate %s" % f)
示例#18
0
    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_url with input yaml file
        mgmt_url = self.client.show_vnf(vnf_id)['vnf']['mgmt_url']
        self.assertIsNotNone(mgmt_url)
        mgmt_dict = yaml.load(str(mgmt_url))

        input_dict = yaml.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)
示例#19
0
class TestToscaUtils(testtools.TestCase):
    tosca_openwrt = _get_template('test_tosca_openwrt.yaml')
    vnfd_dict = yaml.safe_load(tosca_openwrt)
    toscautils.updateimports(vnfd_dict)
    tosca = tosca_template.ToscaTemplate(parsed_params={}, a_file=False,
                          yaml_dict_tpl=vnfd_dict)
    tosca_flavor = _get_template('test_tosca_flavor.yaml')

    def setUp(self):
        super(TestToscaUtils, self).setUp()

    def test_updateimport(self):
        importspath = os.path.abspath('./tacker/vnfm/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):
        tosca2 = tosca_template.ToscaTemplate(parsed_params={}, a_file=False,
                          yaml_dict_tpl=self.vnfd_dict)
        toscautils.post_process_template(tosca2)
        invalidNodes = 0
        for nt in tosca2.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

        self.assertEqual(0, invalidNodes)

        deletedProperties = 0
        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

        self.assertEqual(0, deletedProperties)

        convertedProperties = 0
        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

        self.assertEqual(0, convertedProperties)

    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_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_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.load(str(mgmt_url))

        input_dict = yaml.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)