Ejemplo n.º 1
0
    def test_nsd_elements(self):
        """
        Test that a particular element in a list is corerctly retrieved. In
        this case, we are trying to retrieve an NSD from the NSD catalog.

        """
        # Create the initial NSD catalog
        nsd_catalog = NsdYang.YangData_Nsd_NsdCatalog()

        # Create an NSD, set its 'id', and add it to the catalog
        nsd_id = str(uuid.uuid4())
        nsd_catalog.nsd.append(NsdYang.YangData_Nsd_NsdCatalog_Nsd(
            id=nsd_id, ))

        # Retrieve the NSD using and xpath expression
        xpath = '/nsd:nsd-catalog/nsd:nsd[nsd:id={}]'.format(nsd_id)
        nsd = rwxpath.getxattr(nsd_catalog, xpath)

        self.assertEqual(nsd_id, nsd.id)

        # Modified the name of the NSD using an xpath expression
        rwxpath.setxattr(nsd_catalog, xpath + "/nsd:name", "test-name")

        name = rwxpath.getxattr(nsd_catalog, xpath + "/nsd:name")
        self.assertEqual("test-name", name)
Ejemplo n.º 2
0
    def _generate_nsd(self, vnfds, path):
        """Write a nsd to the specified path. """

        model = gi.repository.RwYang.Model.create_libncx()
        model.load_schema_ypbc(rwnsd.get_schema())

        nsd = rwnsd.NetworkServiceDescriptor()
        colony = self._sysinfo.find_by_class(rift.vcs.Colony)
        if colony is None:
            logger.error("Colony not found, could not generate NSD")
            return

        if not isinstance(colony, rift.vcs.NSD):
            ns = rift.vcs.NSD(
                collection=colony,
                name=colony.name,
            )
        else:
            ns = colony

        nsd.name = ns.nsd_name
        nsd.nsd_id = ns.id
        nsd.provider = ns.provider
        nsd.version = ns.version

        # Add all vnf id's to the nsd
        for vnfd in vnfds:
            nsd.constituent_vnfs.add().vnf_id = vnfd.vnfd_id

        # Create a mapping of link name (port_group) to a list
        # of all VNFD Connection points connected to that link
        link_points = collections.defaultdict(list)
        for vnfd in vnfds:
            for conn_point in vnfd.connection_points:
                link_name = conn_point.id.split("_")[2]
                link_points[link_name].append(conn_point.id)

        # Add a virtual link descriptor for each link name and
        # add all the vnf connections points to it.
        vld_id_gen = itertools.count(1)
        for link_name in link_points:
            vld = nsd.vlds.add()
            vld.id = next(vld_id_gen)
            vld.type_yang = "ELAN"
            for vnf_cp_id in link_points[link_name]:
                vld.connection_points.add().id = vnf_cp_id

        with open(path, 'w') as fp:
            fp.write(nsd.to_xml_v2(model))

        logger.info('Wrote NSD XML file %s: %s', path, nsd)
Ejemplo n.º 3
0
    def test_nsd_scalar_fields(self):
        """
        Test that setxattr correctly sets the value specified by an xpath.

        """
        # Define a simple NSD
        nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd()

        # Check that the unset fields are in fact set to None
        self.assertEqual(
            None, rwxpath.getxattr(nsd, "/nsd:nsd-catalog/nsd:nsd/nsd:name"))
        self.assertEqual(
            None,
            rwxpath.getxattr(nsd, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))

        # Set the values of the 'name' and 'short-name' fields
        rwxpath.setxattr(nsd, "/nsd:nsd-catalog/nsd:nsd/nsd:name", "test-name")
        rwxpath.setxattr(nsd, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
                         "test-short-name")

        # Check that the 'name' and 'short-name' fields are correctly set
        self.assertEqual(
            nsd.name, rwxpath.getxattr(nsd,
                                       "/nsd:nsd-catalog/nsd:nsd/nsd:name"))
        self.assertEqual(
            nsd.short_name,
            rwxpath.getxattr(nsd, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))
Ejemplo n.º 4
0
 def test_update_nsd(self):
     nsd_msg = NsdYang.YangData_Nsd_NsdCatalog_Nsd(id=str(uuid.uuid4()),
                                                   name="nsd_name")
     yield from self._loop.run_in_executor(None, self._onboarder.update,
                                           nsd_msg)
     self.assertEqual(self._handler_info.last_request_message, nsd_msg)
     self.assertEqual(self._handler_info.last_descriptor_type, "nsd")
     self.assertEqual(self._handler_info.last_method, "PUT")
Ejemplo n.º 5
0
def configure_nsd(proxy, nsd_xml_hdl):
    nsd_xml = nsd_xml_hdl.read()
    logger.debug("Attempting to deserialize XML into NSD protobuf: %s",
                 nsd_xml)
    nsd = NsdYang.YangData_NsdCatalog_Nsd()
    nsd.from_xml_v2(model, nsd_xml)

    logger.debug("Sending NSD to netconf: %s", nsd)
    proxy.merge_config(nsd.to_xml_v2(model))
Ejemplo n.º 6
0
 def get_msg(self, desc=None):
     if desc is None:
         desc = NsdYang.YangData_Nsd_NsdCatalog_Nsd(id=str(uuid.uuid4()),
                                                    name="nsd_name")
     serializer = OnboardTestCase.DESC_SERIALIZER_MAP['nsd']
     jstr = serializer.to_json_string(desc, project_ns=False)
     self._desc = jstr
     hdl = io.BytesIO(str.encode(jstr))
     return serializer.from_file_hdl(hdl, ".json")
Ejemplo n.º 7
0
    def test_bad_descriptor_type(self):
        nsd_msg = NsdYang.YangData_Nsd_NsdCatalog()
        with self.assertRaises(TypeError):
            yield from self._loop.run_in_executor(None, self._onboarder.update,
                                                  nsd_msg)

        with self.assertRaises(TypeError):
            yield from self._loop.run_in_executor(None,
                                                  self._onboarder.onboard,
                                                  nsd_msg)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
    def test_null_arguments(self):
        """
        If None is passed to the substitutor for either the NSD or the NSR
        config, no exception should be raised.

        """
        nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd()
        nsr_config = NsrYang.YangData_Nsr_NsInstanceConfig_Nsr()

        self.substitute_input_parameters(None, None)
        self.substitute_input_parameters(nsd, None)
        self.substitute_input_parameters(None, nsr_config)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
    def _generate_nsd(self, vnfds, path):
        """Write a nsd to the specified path. """

        model = gi.repository.RwYang.Model.create_libncx()
        model.load_schema_ypbc(rwnsd.get_schema())

        nsd = rwnsd.NetworkServiceDescriptor()
        colony = self._sysinfo.find_by_class(rift.vcs.Colony)
        if colony is None:
            logger.error("Colony not found, could not generate NSD")
            return

        if not isinstance(colony, rift.vcs.NSD):
            ns = rift.vcs.NSD(
                    collection=colony,
                    name=colony.name,
                    )
        else:
            ns = colony

        nsd.name = ns.nsd_name
        nsd.nsd_id = ns.id
        nsd.provider = ns.provider
        nsd.version = ns.version

        # Add all vnf id's to the nsd
        for vnfd in vnfds:
            nsd.constituent_vnfs.add().vnf_id = vnfd.vnfd_id

        # Create a mapping of link name (port_group) to a list
        # of all VNFD Connection points connected to that link
        link_points = collections.defaultdict(list)
        for vnfd in vnfds:
            for conn_point in vnfd.connection_points:
                link_name = conn_point.id.split("_")[2]
                link_points[link_name].append(conn_point.id)

        # Add a virtual link descriptor for each link name and
        # add all the vnf connections points to it.
        vld_id_gen = itertools.count(1)
        for link_name in link_points:
            vld = nsd.vlds.add()
            vld.id = next(vld_id_gen)
            vld.type_yang = "ELAN"
            for vnf_cp_id in link_points[link_name]:
                vld.connection_points.add().id = vnf_cp_id

        with open(path, 'w') as fp:
            fp.write(nsd.to_xml_v2(model))

        logger.info('Wrote NSD XML file %s: %s', path, nsd)
Ejemplo n.º 12
0
    def test_timeout(self):
        # Set the timeout to something minimal to speed up test
        self._onboarder.timeout = .1

        nsd_msg = NsdYang.YangData_Nsd_NsdCatalog_Nsd(id=str(uuid.uuid4()),
                                                      name="nsd_name")

        # Force the request to timeout by running the call synchronously so the
        with self.assertRaises(onboard.OnboardError):
            self._onboarder.onboard(nsd_msg)

        # Force the request to timeout by running the call synchronously so the
        with self.assertRaises(onboard.UpdateError):
            self._onboarder.update(nsd_msg)
Ejemplo n.º 13
0
    def test_bad_port(self):
        # Use a port not used by the instantiated server
        new_port = self._port - 1
        self._onboarder.port = new_port
        nsd_msg = NsdYang.YangData_Nsd_NsdCatalog_Nsd(id=str(uuid.uuid4()),
                                                      name="nsd_name")

        with self.assertRaises(onboard.OnboardError):
            yield from self._loop.run_in_executor(None,
                                                  self._onboarder.onboard,
                                                  nsd_msg)

        with self.assertRaises(onboard.UpdateError):
            yield from self._loop.run_in_executor(None, self._onboarder.update,
                                                  nsd_msg)
Ejemplo n.º 14
0
#

import argparse
import logging
import rift.auto.proxy
import rift.vcs
import sys

from gi.repository import NsdYang, VldYang, VnfdYang, RwYang

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

model = RwYang.Model.create_libncx()
model.load_schema_ypbc(VldYang.get_schema())
model.load_schema_ypbc(NsdYang.get_schema())
model.load_schema_ypbc(VnfdYang.get_schema())


def configure_vld(proxy, vld_xml_hdl):
    vld_xml = vld_xml_hdl.read()
    logger.debug("Attempting to deserialize XML into VLD protobuf: %s", vld_xml)
    vld = VldYang.YangData_Vld_VldCatalog_Vld()
    vld.from_xml_v2(model, vld_xml)

    logger.debug("Sending VLD to netconf: %s", vld)
    proxy.merge_config(vld.to_xml_v2(model))


def configure_vnfd(proxy, vnfd_xml_hdl):
    vnfd_xml = vnfd_xml_hdl.read()
Ejemplo n.º 15
0
def test_nsd_merge(nsd, convertor):
        nsd.config_primitive.add().from_dict(
            {
                "parameter_group": [
                    {
                        "parameter": [
                            {
                                "data_type": "INTEGER",
                                "default_value": "3000",
                                "name": "Vlan ID",
                                "mandatory": True
                            }
                        ],
                        "name": "PE1",
                        "mandatory": False
                    },
                    {
                        "parameter": [
                            {
                                "data_type": "INTEGER",
                                "default_value": "3000",
                                "name": "Vlan ID",
                                "mandatory": True
                            }
                        ],
                        "name": "PE2",
                        "mandatory": False
                    }
                ],
                "parameter": [
                    {
                        "data_type": "INTEGER",
                        "default_value": "10",
                        "name": "Tunnel Key",
                        "mandatory": True,
                    }
                ],
                "name": "Add SP Test Corporation",
                "user_defined_script": "add_corporation.py"
            })

        ip_yaml = """Add SP Test Corporation:
  parameter:
    Tunnel Key: '10'
  parameter_group:
    PE1:
      Vlan ID: '3000'
    PE2:
      Vlan ID: '3000'
"""

        catalog = NsdYang.YangData_Nsd_NsdCatalog()
        expected_nsd = catalog.nsd.add()
        expected_nsd.id = nsd.id
        expected_nsd.config_primitive.add().from_dict(
            {
                "parameter_group": [
                    {
                        "parameter": [
                            {
                                "data_type": "INTEGER",
                                "default_value": "3000",
                                "name": "Vlan ID",
                                "mandatory": True
                            }
                        ],
                        "name": "PE1",
                        "mandatory": False
                    },
                    {
                        "parameter": [
                            {
                                "data_type": "INTEGER",
                                "default_value": "3000",
                                "name": "Vlan ID",
                                "mandatory": True
                            }
                        ],
                        "name": "PE2",
                        "mandatory": False
                    }
                ],
                "parameter": [
                    {
                        "data_type": "INTEGER",
                        "default_value": "10",
                        "name": "Tunnel Key",
                        "mandatory": True,
                    }
                ],
                "name": "Add SP Test Corporation",
                "user_defined_script": "add_corporation.py"
            })

        convertor.merge_nsd_config(nsd, yaml.load(ip_yaml))

        assert nsd.as_dict() == expected_nsd.as_dict()
Ejemplo n.º 16
0
def nsd():
    catalog = NsdYang.YangData_Nsd_NsdCatalog()
    nsd = catalog.nsd.add()
    nsd.id = str(uuid.uuid1())
    return nsd
Ejemplo n.º 17
0
 def from_xml_file_hdl(cls, hdl):
     hdl.seek(0)
     descriptor = NsdYang.YangData_Nsd_NsdCatalog_Nsd()
     descriptor.from_xml_v2(RiftNSD.model, hdl.read())
     return cls(descriptor)
Ejemplo n.º 18
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']
                if 'logo' in self.metadata:
                    nsd.logo = self.metadata['logo']
            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)

        vnf_type_duplicate = []
        vnfd_resources = []
        vnfd_duplicate_resource_list = []
        for resource in self.resources:
            if resource.type == 'vnfd':
                vnfd_resources.append(resource)

        vnfd_resources.sort(key=lambda x: x.member_vnf_id, reverse=False)
        vnf_type_to_vnf_id = {}
        for resource in vnfd_resources:
            if resource.vnf_type not in vnf_type_duplicate:
                resource.generate_yang_model(nsd, vnfds, use_gi=use_gi)
                vnf_type_to_vnf_id[resource.vnf_type] = resource.id
                vnf_type_duplicate.append(resource.vnf_type)
            else:
                vnfd_duplicate_resource_list.append(resource)

        for resource in vnfd_duplicate_resource_list:
            resource.generate_nsd_constiuent(nsd, vnf_type_to_vnf_id[resource.vnf_type])

        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_RwProject_Project_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, desc_id=nsd_id)

        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
Ejemplo n.º 19
0
import rift.vcs
import sys

import gi
gi.require_version('RwYang', '1.0')

# TODO (Philip): Relook at this code

from gi.repository import (NsdYang, VldYang, VnfdYang, RwYang)

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

model = RwYang.Model.create_libyang()
model.load_schema_ypbc(VldYang.get_schema())
model.load_schema_ypbc(NsdYang.get_schema())
model.load_schema_ypbc(VnfdYang.get_schema())


def configure_vld(proxy, vld_xml_hdl):
    vld_xml = vld_xml_hdl.read()
    logger.debug("Attempting to deserialize XML into VLD protobuf: %s",
                 vld_xml)
    vld = VldYang.YangData_RwProject_Project_VldCatalog_Vld()
    vld.from_xml_v2(model, vld_xml)

    logger.debug("Sending VLD to netconf: %s", vld)
    proxy.merge_config(vld.to_xml_v2(model))


def configure_vnfd(proxy, vnfd_xml_hdl):
Ejemplo n.º 20
0
    })
    vnf_config.initial_config_primitive.append(init_config)

    for seq, (intf, cidr) in enumerate(intf_ip_pairs, start=2):
        params = [{"name": "iface-name", "value": intf}]
        if cidr is not None:
            params.append({"name": "cidr", "value": cidr})

        vnf_config.initial_config_primitive.add().from_dict({
            "seq": seq,
            "name": "configure-interface",
            "parameter": params
        })


nsd = NsdYang.YangData_Nsd_NsdCatalog_Nsd()
add_pe_vnf(nsd, 1, [
    ("eth1", "10.10.10.9/30"),
    ("eth2", "10.10.10.1/30"),
    ("eth3", None),
])

add_pe_vnf(nsd, 2, [
    ("eth1", "10.10.10.10/30"),
    ("eth2", "10.10.10.6/30"),
    ("eth3", None),
])

add_pe_vnf(nsd, 3, [
    ("eth1", "10.10.10.2/30"),
    ("eth2", "10.10.10.5/30"),