Esempio n. 1
0
    def test_substitution(self):
        """
        Test that substitution of input parameters occurs as expected.

        """
        # Define the original NSD
        nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd()
        nsd.name = "robert"
        nsd.short_name = "bob"

        # Define which parameters may be modified
        nsd.input_parameter_xpath.extend([
            NsdYang.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
                label="NSD Name",
            ),
            NsdYang.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
                label="NSD Short Name",
            ),
        ])

        # Define the input parameters that are intended to be modified
        nsr_config = NsrYang.YangData_Nsr_NsInstanceConfig_Nsr()
        nsr_config.input_parameter.extend([
            NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
                value="robert",
            ),
            NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
                value="bob",
            ),
        ])

        self.substitute_input_parameters(nsd, nsr_config)

        # Verify that both the 'name' and 'short-name' fields are correctly
        # replaced.
        self.assertEqual("robert", nsd.name)
        self.assertEqual("bob", nsd.short_name)
Esempio n. 2
0
    def test_illegal_input_parameter(self):
        """
        In the NSD there is a list of the parameters that are allowed to be
        sbustituted by input parameters. This test checks that when an input
        parameter is provided in the NSR config that is not in the NSD, it is
        not applied.

        """
        # Define the original NSD
        nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd()
        nsd.name = "robert"
        nsd.short_name = "bob"

        # Define which parameters may be modified
        nsd.input_parameter_xpath.append(
            NsdYang.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
                label="NSD Name",
            ))

        # Define the input parameters that are intended to be modified
        nsr_config = NsrYang.YangData_Nsr_NsInstanceConfig_Nsr()
        nsr_config.input_parameter.extend([
            NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
                value="alice",
            ),
            NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
                xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
                value="alice",
            ),
        ])

        self.substitute_input_parameters(nsd, nsr_config)

        # Verify that only the parameter in the input_parameter_xpath list is
        # modified after the input parameters have been applied.
        self.assertEqual("alice", nsd.name)
        self.assertEqual("bob", nsd.short_name)
Esempio n. 3
0
    def output_to_yang(self, use_gi=False, indent=4):
        self.log.debug(_('Converting translated output to yang model.'))

        nsd_cat = None
        nsd_id = str(uuid.uuid1())
        vnfds = []

        if use_gi:
            try:
                nsd_cat = RwNsdYang.YangData_Nsd_NsdCatalog()
                nsd = nsd_cat.nsd.add()
                nsd.id = nsd_id
                nsd.name = self.metadata['name']
                nsd.description = self.description
                nsd.vendor = self.metadata['vendor']
                nsd.short_name = self.metadata['name']
                nsd.version = self.metadata['version']
            except Exception as e:
                self.log.warning(
                    _("Unable to use YANG GI to generate "
                      "descriptors, falling back to alternate "
                      "method: {}").format(e))
                self.log.exception(e)
                use_gi = False

        if not use_gi:
            nsd = {
                'id': nsd_id,
                'name': self.metadata['name'],
                'description': self.description,
                'vendor': self.metadata['vendor'],
                'short-name': self.metadata['name'],
                'version': self.metadata['version'],
            }

        for resource in self.resources:
            # Do the vlds first
            if resource.type == 'vld':
                resource.generate_yang_model(nsd, vnfds, use_gi=use_gi)

        for resource in self.resources:
            # Do the vnfds next
            if resource.type == 'vnfd':
                resource.generate_yang_model(nsd, vnfds, use_gi=use_gi)

        for resource in self.resources:
            # Do the other nodes
            if resource.type != 'vnfd' and resource.type != 'vld':
                resource.generate_yang_model(nsd, vnfds, use_gi=use_gi)

        for group in self.groups:
            group.generate_yang_model(nsd, vnfds, use_gi=use_gi)

        for policy in self.policies:
            policy.generate_yang_model(nsd, vnfds, use_gi=use_gi)

        # Add input params to nsd
        if use_gi:
            for param in self.parameters:
                nsd.input_parameter_xpath.append(
                    NsdYang.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
                        xpath=param.get_xpath(), ))
        else:
            nsd['input-parameter-xpath'] = []
            for param in self.parameters:
                nsd['input-parameter-xpath'].append(
                    {'xpath': param.get_xpath()})

        # Get list of supporting files referred in template
        # Returned format is {desc_id: [{type: type, name: filename}]}
        # TODO (pjoseph): Currently only images and scripts are retrieved.
        # Need to add support to get script names, charms, etc.
        other_files = {}
        for resource in self.resources:
            resource.get_supporting_files(other_files)

        for policy in self.policies:
            policy.get_supporting_files(other_files, desc_id=nsd_id)

        self.log.debug(_("List of other files: {}".format(other_files)))

        # Do the final processing and convert each descriptor to yaml string
        tpl = {}

        # Add the NSD
        if use_gi:
            nsd_pf = self.get_yaml(['nsd', 'rw-nsd'], nsd_cat)
            nsd_id = nsd_cat.nsd[0].id
            nsd_name = nsd_cat.nsd[0].name
        else:
            nsd_id = nsd['id']
            nsd_name = nsd['name']

            # In case of non gi proecssing,
            # - convert all values to string
            # - enclose in a catalog dict
            # - prefix all keys with nsd or vnfd
            # - Convert to YAML string
            nsd_pf = yaml.dump(self.prefix_dict(
                self.add_cat(dict_convert_values_to_str(nsd), self.NSD),
                self.NSD),
                               default_flow_style=False)

        nsd_out = {
            self.NAME: nsd_name,
            self.ID: nsd_id,
            self.YANG: nsd_pf,
        }

        if nsd_id in other_files:
            nsd_out[self.FILES] = other_files[nsd_id]

        tpl[self.NSD] = [nsd_out]

        # Add the VNFDs
        tpl[self.VNFD] = []

        for vnfd in vnfds:
            if use_gi:
                vnfd_pf = self.get_yaml(['vnfd', 'rw-vnfd'], vnfd)
                vnfd_id = vnfd.vnfd[0].id
                vnfd_name = vnfd.vnfd[0].name

            else:
                vnfd_id = vnfd['id']
                vnfd_name = vnfd['name']

                # In case of non gi proecssing,
                # - convert all values to string
                # - enclose in a catalog dict
                # - prefix all keys with nsd or vnfd
                # - Convert to YAML string
                vnfd_pf = yaml.dump(self.prefix_dict(
                    self.add_cat(dict_convert_values_to_str(vnfd), self.VNFD),
                    self.VNFD),
                                    default_flow_style=False)

            vnfd_out = {
                self.NAME: vnfd_name,
                self.ID: vnfd_id,
                self.YANG: vnfd_pf,
            }

            if vnfd_id in other_files:
                vnfd_out[self.FILES] = other_files[vnfd_id]

            tpl[self.VNFD].append(vnfd_out)

        self.log.debug(_("NSD: {0}").format(tpl[self.NSD]))
        self.log.debug(_("VNFDs:"))
        for vnfd in tpl[self.VNFD]:
            self.log.debug(_("{0}").format(vnfd))

        return tpl