예제 #1
0
    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)
예제 #2
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')
     mead_dict = yaml.safe_load(tosca_fes_all_numa_count)
     toscautils.updateimports(mead_dict)
     tosca = tosca_template.ToscaTemplate(a_file=False,
                                          yaml_dict_tpl=mead_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)
예제 #3
0
    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_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.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.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_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)
예제 #7
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.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
예제 #8
0
def init_tosca_template(tosca_file_path):
    tpl = t.ToscaTemplate(path=tosca_file_path)
    topo = tpl.topology_template
    all_nodes = topo.nodetemplates
    for n in all_nodes:
        if "tosca.nodes.Compute" in str(n.type):
            print("Compute node--", n.name)
            manage_compute_node(n)
        elif "tosca.nodes.network.Network" in str(n.type):
            print("Network node--", n.name)
            manage_network(n)
        else:
            print("Unknown node -- ", n.name)
    return
    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')
예제 #10
0
    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)
예제 #11
0
    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)
예제 #12
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))
예제 #13
0
파일: utils.py 프로젝트: shuwenCai/tacker
def _convert_desired_capacity(inst_level_id, vnfd_dict, vdu):
    aspect_delta_dict = {}
    aspect_vdu_dict = {}
    inst_level_dict = {}
    aspect_id_dict = {}
    vdu_delta_dict = {}
    desired_capacity = 1

    tosca = tosca_template.ToscaTemplate(parsed_params={},
                                         a_file=False,
                                         yaml_dict_tpl=vnfd_dict)
    tosca_policies = tosca.topology_template.policies
    default_inst_level_id = toscautils._extract_policy_info(
        tosca_policies, inst_level_dict, aspect_delta_dict, aspect_id_dict,
        aspect_vdu_dict, vdu_delta_dict)

    if vdu_delta_dict.get(vdu) is None:
        return desired_capacity

    if inst_level_id:
        instantiation_level = inst_level_id
    elif default_inst_level_id:
        instantiation_level = default_inst_level_id
    else:
        return desired_capacity

    al_dict = inst_level_dict.get(instantiation_level)

    if not al_dict:
        return desired_capacity

    for aspect_id, level_num in al_dict.items():
        delta_id = aspect_id_dict.get(aspect_id)

        if delta_id is not None:
            delta_num = \
                aspect_delta_dict.get(aspect_id).get(delta_id)

        vdus = aspect_vdu_dict.get(aspect_id)
        initial_delta = None
        for vdu in vdus:
            initial_delta = vdu_delta_dict.get(vdu)

        if initial_delta is not None:
            desired_capacity = initial_delta + delta_num * level_num

    return desired_capacity
예제 #14
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
예제 #15
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.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
예제 #16
0
    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
예제 #17
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)
예제 #18
0
 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')
예제 #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_mea_with_multiple_vdus(self):
        input_yaml = read_file('sample-tosca-mead-multi-vdu.yaml')
        tosca_dict = yaml.safe_load(input_yaml)
        mead_name = 'sample-tosca-mead-multi-vdu'
        tosca_arg = {
            'mead': {
                'name': mead_name,
                'attributes': {
                    'mead': tosca_dict
                }
            }
        }

        # Create mead with tosca template
        mead_instance = self.client.create_mead(body=tosca_arg)
        self.assertIsNotNone(mead_instance)

        # Create mea with mead_id
        mead_id = mead_instance['mead']['id']
        mea_arg = {
            'mea': {
                'mead_id': mead_id,
                'name': "test_tosca_mea_with_multiple_vdus"
            }
        }
        mea_instance = self.client.create_mea(body=mea_arg)

        mea_id = mea_instance['mea']['id']
        self.wait_until_mea_active(mea_id, constants.MEA_CIRROS_CREATE_TIMEOUT,
                                   constants.ACTIVE_SLEEP_TIME)
        self.assertEqual('ACTIVE',
                         self.client.show_mea(mea_id)['mea']['status'])
        self.validate_mea_instance(mead_instance, mea_instance)

        self.verify_mea_crud_events(mea_id,
                                    evt_constants.RES_EVT_CREATE,
                                    evt_constants.PENDING_CREATE,
                                    cnt=2)
        self.verify_mea_crud_events(mea_id, evt_constants.RES_EVT_CREATE,
                                    evt_constants.ACTIVE)

        # Validate mgmt_url with input yaml file
        mgmt_url = self.client.show_mea(mea_id)['mea']['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 mea_instance with mea_id
        try:
            self.client.delete_mea(mea_id)
        except Exception:
            assert False, "mea Delete of test_mea_with_multiple_vdus failed"

        self.wait_until_mea_delete(mea_id, constants.MEA_CIRROS_DELETE_TIMEOUT)
        self.verify_mea_crud_events(mea_id,
                                    evt_constants.RES_EVT_DELETE,
                                    evt_constants.PENDING_DELETE,
                                    cnt=2)

        # Delete mead_instance
        self.addCleanup(self.client.delete_mead, mead_id)