Exemplo n.º 1
0
        def generate_hot_from_tosca(vnfd_dict):
            parsed_params = dev_attrs.pop('param_values', {})

            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
Exemplo n.º 2
0
 def test_get_flavor_dict(self):
     vnfd_dict = yaml.load(self.tosca_flavor)
     toscautils.updateimports(vnfd_dict)
     tosca = 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)
Exemplo n.º 3
0
 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 = 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)
Exemplo n.º 4
0
 def test_get_flavor_dict(self):
     vnfd_dict = yaml.load(self.tosca_flavor)
     toscautils.updateimports(vnfd_dict)
     tosca = 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)
Exemplo n.º 5
0
    def test_create_delete_tosca_vnf_with_multiple_vdus(self):
        data = dict()
        input_yaml = read_file('sample-tosca-vnfd-multi-vdu.yaml')
        data['tosca'] = input_yaml
        toscal = data['tosca']
        tosca_arg = {'vnfd': {'attributes': {'vnfd': toscal}}}

        # 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(
            self.client.show_vnf(vnf_id)['vnf']['status'], 'ACTIVE')
        self.validate_vnf_instance(vnfd_instance, vnf_instance)

        # 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 = ToscaTemplate(parsed_params={},
                              a_file=False,
                              yaml_dict_tpl=input_dict)

        vdus = toscautils.findvdus(tosca)

        self.assertEqual(len(mgmt_dict.keys()), len(vdus))
        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"

        # Delete vnfd_instance
        self.addCleanup(self.client.delete_vnfd, vnfd_id)
        self.addCleanup(self.wait_until_vnf_delete, vnf_id,
                        constants.VNF_CIRROS_DELETE_TIMEOUT)
Exemplo n.º 6
0
    def create_device_template_pre(self, plugin, context, device_template):
        device_template_dict = device_template['device_template']
        vnfd_yaml = device_template_dict['attributes'].get('vnfd')
        if vnfd_yaml is None:
            return

        vnfd_dict = yaml.load(vnfd_yaml)
        LOG.debug(_('vnfd_dict: %s'), vnfd_dict)

        if 'tosca_definitions_version' in vnfd_dict:
            # Prepend the tacker_defs.yaml import file with the full
            # path to the file
            toscautils.updateimports(vnfd_dict)

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

            if ('description' not in device_template_dict or
                    device_template_dict['description'] == ''):
                device_template_dict['description'] = vnfd_dict.get(
                    'description', '')
            if (('name' not in device_template_dict or
                    not len(device_template_dict['name'])) and
                    'metadata' in vnfd_dict):
                device_template_dict['name'] = vnfd_dict['metadata'].get(
                    'template_name', '')

            device_template_dict['mgmt_driver'] = toscautils.get_mgmt_driver(
                tosca)
        else:
            KEY_LIST = (('name', 'template_name'),
                        ('description', 'description'))

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

            service_types = vnfd_dict.get('service_properties', {}).get('type',
                                                                        [])
            if service_types:
                device_template_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 vnfd_dict.get('vdus', {}).values():
                mgmt_driver = vdu.get('mgmt_driver')
                if mgmt_driver:
                    device_template_dict['mgmt_driver'] = mgmt_driver
        LOG.debug(_('device_template %s'), device_template)
Exemplo n.º 7
0
    def create_device_template_pre(self, plugin, context, device_template):
        device_template_dict = device_template['device_template']
        vnfd_yaml = device_template_dict['attributes'].get('vnfd')
        if vnfd_yaml is None:
            return

        vnfd_dict = yaml.load(vnfd_yaml)
        LOG.debug(_('vnfd_dict: %s'), vnfd_dict)

        if 'tosca_definitions_version' in vnfd_dict:
            # Prepend the tacker_defs.yaml import file with the full
            # path to the file
            toscautils.updateimports(vnfd_dict)

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

            if ('description' not in device_template_dict
                    or device_template_dict['description'] == ''):
                device_template_dict['description'] = vnfd_dict.get(
                    'description', '')
            if (('name' not in device_template_dict
                 or not len(device_template_dict['name']))
                    and 'metadata' in vnfd_dict):
                device_template_dict['name'] = vnfd_dict['metadata'].get(
                    'template_name', '')

            device_template_dict['mgmt_driver'] = toscautils.get_mgmt_driver(
                tosca)
        else:
            KEY_LIST = (('name', 'template_name'), ('description',
                                                    'description'))

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

            service_types = vnfd_dict.get('service_properties',
                                          {}).get('type', [])
            if service_types:
                device_template_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 vnfd_dict.get('vdus', {}).values():
                mgmt_driver = vdu.get('mgmt_driver')
                if mgmt_driver:
                    device_template_dict['mgmt_driver'] = mgmt_driver
        LOG.debug(_('device_template %s'), device_template)
    def test_create_delete_tosca_vnf_with_multiple_vdus(self):
        data = dict()
        input_yaml = read_file('sample-tosca-vnfd-multi-vdu.yaml')
        data['tosca'] = input_yaml
        toscal = data['tosca']
        vnfd_name = 'sample-tosca-vnfd-multi-vdu'
        tosca_arg = {'vnfd': {'name': vnfd_name,
                              'attributes': {'vnfd': toscal}}}

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

        # 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 = 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"

        # Delete vnfd_instance
        self.addCleanup(self.client.delete_vnfd, vnfd_id)
        self.addCleanup(self.wait_until_vnf_delete, vnf_id,
            constants.VNF_CIRROS_DELETE_TIMEOUT)
Exemplo n.º 9
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)
Exemplo n.º 10
0
 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 = 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)
Exemplo n.º 11
0
    def create(self, plugin, context, device, auth_attr):
        LOG.debug(_('device %s'), device)
        attributes = device['device_template']['attributes'].copy()
        vnfd_yaml = attributes.pop('vnfd', None)
        fields = dict((key, attributes.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in attributes)
        for key in ('files', 'parameters'):
            if key in attributes:
                fields[key] = jsonutils.loads(attributes.pop(key))

        # overwrite parameters with given dev_attrs for device creation
        dev_attrs = device['attributes'].copy()
        fields.update(dict((key, dev_attrs.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in dev_attrs))
        for key in ('files', 'parameters'):
            if key in dev_attrs:
                fields.setdefault(key, {}).update(
                    jsonutils.loads(dev_attrs.pop(key)))

        region_name = device.get('placement_attr', {}).get('region_name', None)
        heatclient_ = HeatClient(auth_attr, region_name)
        unsupported_res_prop = self.fetch_unsupported_resource_prop(
            heatclient_)

        LOG.debug('vnfd_yaml %s', vnfd_yaml)
        if vnfd_yaml is not None:
            vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml)
            LOG.debug('vnfd_dict %s', vnfd_dict)

            monitoring_dict = {'vdus': {}}

            if 'tosca_definitions_version' in vnfd_dict:
                parsed_params = dev_attrs.pop('param_values', {})

                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)
            else:
                assert 'template' not in fields
                assert 'template_url' not in fields
                template_dict = yaml.load(HEAT_TEMPLATE_BASE)
                outputs_dict = {}
                template_dict['outputs'] = outputs_dict

                if 'get_input' in vnfd_yaml:
                    self._process_parameterized_input(dev_attrs, vnfd_dict)

                KEY_LIST = (('description', 'description'), )
                for (key, vnfd_key) in KEY_LIST:
                    if vnfd_key in vnfd_dict:
                        template_dict[key] = vnfd_dict[vnfd_key]

                for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items():
                    template_dict.setdefault('resources', {})[vdu_id] = {
                        "type": "OS::Nova::Server"
                    }
                    resource_dict = template_dict['resources'][vdu_id]
                    KEY_LIST = (('image', 'vm_image'),
                                ('flavor', 'instance_type'))
                    resource_dict['properties'] = {}
                    properties = resource_dict['properties']
                    for (key, vdu_key) in KEY_LIST:
                        properties[key] = vdu_dict[vdu_key]
                    if 'network_interfaces' in vdu_dict:
                        self._process_vdu_network_interfaces(vdu_id,
                         vdu_dict, properties, template_dict,
                         unsupported_res_prop)
                    if ('user_data' in vdu_dict and
                            'user_data_format' in vdu_dict):
                        properties['user_data_format'] = vdu_dict[
                            'user_data_format']
                        properties['user_data'] = vdu_dict['user_data']
                    elif ('user_data' in vdu_dict or
                            'user_data_format' in vdu_dict):
                        raise vnfm.UserDataFormatNotFound()
                    if 'placement_policy' in vdu_dict:
                        if 'availability_zone' in vdu_dict['placement_policy']:
                            properties['availability_zone'] = vdu_dict[
                                'placement_policy']['availability_zone']
                    if 'config' in vdu_dict:
                        properties['config_drive'] = True
                        metadata = properties.setdefault('metadata', {})
                        metadata.update(vdu_dict['config'])
                        for key, value in metadata.items():
                            metadata[key] = value[:255]

                    monitoring_policy = vdu_dict.get('monitoring_policy',
                                                     'noop')
                    failure_policy = vdu_dict.get('failure_policy', 'noop')

                    # Convert the old monitoring specification to the new
                    # network.  This should be removed after Mitaka
                    if (monitoring_policy == 'ping' and
                            failure_policy == 'respawn'):
                        vdu_dict['monitoring_policy'] = {
                            'ping': {'actions': {'failure': 'respawn'}}}
                        vdu_dict.pop('failure_policy')

                    if monitoring_policy != 'noop':
                        monitoring_dict['vdus'][vdu_id] = \
                            vdu_dict['monitoring_policy']

                    # to pass necessary parameters to plugin upwards.
                    for key in ('service_type',):
                        if key in vdu_dict:
                            device.setdefault(
                                'attributes', {})[vdu_id] = jsonutils.dumps(
                                    {key: vdu_dict[key]})

                    heat_template_yaml = yaml.dump(template_dict)

            fields['template'] = heat_template_yaml
            if not device['attributes'].get('heat_template'):
                device['attributes']['heat_template'] = \
                    heat_template_yaml

            if monitoring_dict.keys():
                    device['attributes']['monitoring_policy'] = \
                        jsonutils.dumps(monitoring_dict)

        if 'stack_name' not in fields:
            name = (__name__ + '_' + self.__class__.__name__ + '-' +
                    device['id'])
            if device['attributes'].get('failure_count'):
                name += ('-%s') % str(device['attributes']['failure_count'])
            fields['stack_name'] = name

        # service context is ignored
        LOG.debug(_('service_context: %s'), device.get('service_context', []))

        LOG.debug(_('fields: %s'), fields)
        LOG.debug(_('template: %s'), fields['template'])
        stack = heatclient_.create(fields)
        return stack['stack']['id']
Exemplo n.º 12
0
    def create(self, plugin, context, device, auth_attr):
        LOG.debug(_('device %s'), device)
        attributes = device['device_template']['attributes'].copy()
        vnfd_yaml = attributes.pop('vnfd', None)
        fields = dict((key, attributes.pop(key))
                      for key in ('stack_name', 'template_url', 'template')
                      if key in attributes)
        for key in ('files', 'parameters'):
            if key in attributes:
                fields[key] = jsonutils.loads(attributes.pop(key))

        # overwrite parameters with given dev_attrs for device creation
        dev_attrs = device['attributes'].copy()
        fields.update(
            dict((key, dev_attrs.pop(key))
                 for key in ('stack_name', 'template_url', 'template')
                 if key in dev_attrs))
        for key in ('files', 'parameters'):
            if key in dev_attrs:
                fields.setdefault(key, {}).update(
                    jsonutils.loads(dev_attrs.pop(key)))

        region_name = device.get('placement_attr', {}).get('region_name', None)
        heatclient_ = HeatClient(auth_attr, region_name)
        unsupported_res_prop = self.fetch_unsupported_resource_prop(
            heatclient_)

        LOG.debug('vnfd_yaml %s', vnfd_yaml)
        if vnfd_yaml is not None:
            vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml)
            LOG.debug('vnfd_dict %s', vnfd_dict)

            monitoring_dict = {'vdus': {}}

            if 'tosca_definitions_version' in vnfd_dict:
                parsed_params = dev_attrs.pop('param_values', {})

                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)
            else:
                assert 'template' not in fields
                assert 'template_url' not in fields
                template_dict = yaml.load(HEAT_TEMPLATE_BASE)
                outputs_dict = {}
                template_dict['outputs'] = outputs_dict

                if 'get_input' in vnfd_yaml:
                    self._process_parameterized_input(dev_attrs, vnfd_dict)

                KEY_LIST = (('description', 'description'), )
                for (key, vnfd_key) in KEY_LIST:
                    if vnfd_key in vnfd_dict:
                        template_dict[key] = vnfd_dict[vnfd_key]

                for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items():
                    template_dict.setdefault('resources', {})[vdu_id] = {
                        "type": "OS::Nova::Server"
                    }
                    resource_dict = template_dict['resources'][vdu_id]
                    KEY_LIST = (('image', 'vm_image'), ('flavor',
                                                        'instance_type'))
                    resource_dict['properties'] = {}
                    properties = resource_dict['properties']
                    for (key, vdu_key) in KEY_LIST:
                        properties[key] = vdu_dict[vdu_key]
                    if 'network_interfaces' in vdu_dict:
                        self._process_vdu_network_interfaces(
                            vdu_id, vdu_dict, properties, template_dict,
                            unsupported_res_prop)
                    if ('user_data' in vdu_dict
                            and 'user_data_format' in vdu_dict):
                        properties['user_data_format'] = vdu_dict[
                            'user_data_format']
                        properties['user_data'] = vdu_dict['user_data']
                    elif ('user_data' in vdu_dict
                          or 'user_data_format' in vdu_dict):
                        raise vnfm.UserDataFormatNotFound()
                    if 'placement_policy' in vdu_dict:
                        if 'availability_zone' in vdu_dict['placement_policy']:
                            properties['availability_zone'] = vdu_dict[
                                'placement_policy']['availability_zone']
                    if 'config' in vdu_dict:
                        properties['config_drive'] = True
                        metadata = properties.setdefault('metadata', {})
                        metadata.update(vdu_dict['config'])
                        for key, value in metadata.items():
                            metadata[key] = value[:255]
                    if 'key_name' in vdu_dict:
                        properties['key_name'] = vdu_dict['key_name']

                    monitoring_policy = vdu_dict.get('monitoring_policy',
                                                     'noop')
                    failure_policy = vdu_dict.get('failure_policy', 'noop')

                    # Convert the old monitoring specification to the new
                    # network.  This should be removed after Mitaka
                    if (monitoring_policy == 'ping'
                            and failure_policy == 'respawn'):
                        vdu_dict['monitoring_policy'] = {
                            'ping': {
                                'actions': {
                                    'failure': 'respawn'
                                }
                            }
                        }
                        vdu_dict.pop('failure_policy')

                    if monitoring_policy != 'noop':
                        monitoring_dict['vdus'][vdu_id] = \
                            vdu_dict['monitoring_policy']

                    # to pass necessary parameters to plugin upwards.
                    for key in ('service_type', ):
                        if key in vdu_dict:
                            device.setdefault('attributes',
                                              {})[vdu_id] = jsonutils.dumps(
                                                  {key: vdu_dict[key]})

                    heat_template_yaml = yaml.dump(template_dict)

            fields['template'] = heat_template_yaml
            if not device['attributes'].get('heat_template'):
                device['attributes']['heat_template'] = \
                    heat_template_yaml

            if monitoring_dict.keys():
                device['attributes']['monitoring_policy'] = \
                    jsonutils.dumps(monitoring_dict)

        if 'stack_name' not in fields:
            name = (__name__ + '_' + self.__class__.__name__ + '-' +
                    device['id'])
            if device['attributes'].get('failure_count'):
                name += ('-RESPAWN-%s') % str(
                    device['attributes']['failure_count'])
            fields['stack_name'] = name

        # service context is ignored
        LOG.debug(_('service_context: %s'), device.get('service_context', []))

        LOG.debug(_('fields: %s'), fields)
        LOG.debug(_('template: %s'), fields['template'])
        stack = heatclient_.create(fields)
        return stack['stack']['id']
Exemplo n.º 13
0
class TestToscaUtils(testtools.TestCase):
    tosca_openwrt = _get_template('test_tosca_openwrt.yaml')
    vnfd_dict = yaml.load(tosca_openwrt)
    toscautils.updateimports(vnfd_dict)
    tosca = 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/vm/tosca/lib/')
        file1 = importspath + '/tacker_defs.yaml'
        file2 = importspath + '/tacker_nfv_defs.yaml'
        expected_imports = [file1, file2]
        self.assertEqual(self.vnfd_dict['imports'], expected_imports)

    def test_get_mgmt_driver(self):
        expected_mgmt_driver = 'openwrt'
        mgmt_driver = toscautils.get_mgmt_driver(self.tosca)
        self.assertEqual(mgmt_driver, expected_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(monitoring, expected_monitoring)

    def test_get_mgmt_ports(self):
        expected_mgmt_ports = {'mgmt_ip-VDU1': 'CP1'}
        mgmt_ports = toscautils.get_mgmt_ports(self.tosca)
        self.assertEqual(mgmt_ports, expected_mgmt_ports)

    def test_post_process_template(self):
        tosca2 = 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(invalidNodes, 0)

        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(deletedProperties, 0)

        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(convertedProperties, 0)

    def test_post_process_heat_template(self):
        tosca1 = ToscaTemplate(parsed_params={},
                               a_file=False,
                               yaml_dict_tpl=self.vnfd_dict)
        toscautils.post_process_template(tosca1)
        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.load(heat_tpl)
        expecteddict = yaml.load(expected_heat_tpl)
        self.assertEqual(heatdict, expecteddict)

    def test_findvdus(self):
        vdus = toscautils.findvdus(self.tosca)

        self.assertEqual(len(vdus), 1)

        for vdu in vdus:
            self.assertEqual(
                vdu.type_definition.is_derived_from(toscautils.TACKERVDU),
                True)

    def test_get_flavor_dict(self):
        vnfd_dict = yaml.load(self.tosca_flavor)
        toscautils.updateimports(vnfd_dict)
        tosca = 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.load(
            _get_template('hot_flavor_and_capabilities.yaml'))
        expected_dict = yaml.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(dummy_heat_dict, expected_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.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)

    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 = 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.load(
            _get_template('hot_image_before_processed_image.yaml'))
        expected_dict = yaml.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(dummy_heat_dict, expected_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.load(_get_template('hot_tosca_openwrt.yaml'))
        expected_heat_dict = yaml.load(
            _get_template('hot_tosca_openwrt_kilo.yaml'))
        toscautils.convert_unsupported_res_prop(dummy_heat_dict,
                                                unsupported_res_prop_dict)
        self.assertEqual(dummy_heat_dict, expected_heat_dict)