def _delete_virtual_network(self, ns_name, vn_name):
        """
        Delete the virtual network associated with this namespace.
        """
        # First lookup the cache for the entry.
        vn = VirtualNetworkKM.find_by_name_or_uuid(vn_name)
        if not vn:
            return

        proj_fq_name = vnc_kube_config.cluster_project_fq_name(ns_name)
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn.fq_name)
            # Delete/cleanup ipams allocated for this network.
            ipam_refs = vn_obj.get_network_ipam_refs()
            if ipam_refs:
                proj_obj = self._vnc_lib.project_read(fq_name=proj_fq_name)
                for ipam in ipam_refs:
                    ipam_obj = NetworkIpam(
                        name=ipam['to'][-1], parent_obj=proj_obj)
                    vn_obj.del_network_ipam(ipam_obj)
                    self._vnc_lib.virtual_network_update(vn_obj)
        except RefsExistError as e:
            # Delete of custom network when it is still in use is not
            # supported yet. Log deletion attempt and return without deleting VN
            self._logger.error("%s: Cannot delete Network %s . %s"
                               % (self._name, vn_name, str(e)))
            return
        except NoIdError:
            pass

        # Delete the network.
        self._vnc_lib.virtual_network_delete(id=vn.uuid)

        # Delete the network from cache.
        VirtualNetworkKM.delete(vn.uuid)
Exemple #2
0
    def _delete_isolated_ns_virtual_network(self, ns_name, vn_name,
                                            proj_fq_name):
        """
        Delete the virtual network associated with this namespace.
        """
        # First lookup the cache for the entry.
        vn = VirtualNetworkKM.find_by_name_or_uuid(vn_name)
        if not vn:
            return

        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn.fq_name)
            # Delete/cleanup ipams allocated for this network.
            ipam_refs = vn_obj.get_network_ipam_refs()
            if ipam_refs:
                proj_obj = self._vnc_lib.project_read(fq_name=proj_fq_name)
                for ipam in ipam_refs:
                    ipam_obj = NetworkIpam(name=ipam['to'][-1],
                                           parent_obj=proj_obj)
                    vn_obj.del_network_ipam(ipam_obj)
                    self._vnc_lib.virtual_network_update(vn_obj)
        except NoIdError:
            pass

        # Delete the network.
        self._vnc_lib.virtual_network_delete(id=vn.uuid)

        # Delete the network from cache.
        VirtualNetworkKM.delete(vn.uuid)
    def _create_ipam(self, ipam_name, subnets, proj_obj, type='flat-subnet'):
        ipam_obj = NetworkIpam(name=ipam_name, parent_obj=proj_obj)

        ipam_subnets = []
        for subnet in subnets:
            pfx, pfx_len = subnet.split('/')
            ipam_subnet = IpamSubnetType(subnet=SubnetType(pfx, int(pfx_len)))
            ipam_subnets.append(ipam_subnet)
        if not len(ipam_subnets):
            self.logger.error("%s - %s subnet is empty for %s" %
                              (self._name, ipam_name, subnets))

        if type == 'flat-subnet':
            ipam_obj.set_ipam_subnet_method('flat-subnet')
            ipam_obj.set_ipam_subnets(IpamSubnets(ipam_subnets))

        ipam_update = False
        try:
            ipam_uuid = self.vnc_lib.network_ipam_create(ipam_obj)
            ipam_update = True
        except RefsExistError:
            curr_ipam_obj = self.vnc_lib.network_ipam_read(
                fq_name=ipam_obj.get_fq_name())
            ipam_uuid = curr_ipam_obj.get_uuid()
            if type == 'flat-subnet' and not curr_ipam_obj.get_ipam_subnets():
                self.vnc_lib.network_ipam_update(ipam_obj)
                ipam_update = True

        # Cache ipam info.
        NetworkIpamKM.locate(ipam_uuid)

        return ipam_update, ipam_obj, ipam_subnets
        def _get_network_ipam(ipam_name, subnets, subnetting):
            def _new_subnet(cidr):
                split_cidr = cidr.split('/')
                return SubnetType(ip_prefix=split_cidr[0],
                                  ip_prefix_len=split_cidr[1])

            # end _new_subnet
            ipam = NetworkIpam(name=ipam_name,
                               ipam_subnets=IpamSubnets([
                                   IpamSubnetType(
                                       subnet=_new_subnet(sn.get('cidr')),
                                       default_gateway=sn.get('gateway'),
                                       subnet_uuid=str(uuid.uuid1()))
                                   for sn in subnets
                                   if int(sn.get('cidr').split('/')[-1]) < 31
                               ]),
                               ipam_subnet_method='flat-subnet',
                               ipam_subnetting=subnetting)
            return ipam
Exemple #5
0
    def _create_ipv6_ll_ipam_and_vn(self, vnc_api, network_name):
        nw_fq_name = ['default-domain', 'default-project', network_name]
        ipam_fq_name = ['default-domain', 'default-project',
                        '_internal_ipam_ipv6_link_local']
        subnets = VnSubnetsType([(IpamSubnetType(subnet=SubnetType('fe80::',
                                                                   64),
                                                 default_gateway='fe80::1',
                                                 addr_from_start=True,
                                                 subnet_uuid=str(uuid.uuid1())
                                                 )
                                  )]
                                )
        ipam = NetworkIpam(
            name='_internal_ipam_ipv6_link_local',
            fq_name=ipam_fq_name,
            parent_type='project',
            ipam_subnet_method='user-defined-subnet')
        try:
            self._vnc_api.network_ipam_create(ipam)
        except RefsExistError as ex:
            error_msg = 'network IPAM \'ipv6_link_local\' already \
                        exists or other conflict: {}' \
                .format(str(ex))
            self._logger.error(error_msg)
            self._vnc_api.network_ipam_update(ipam)

        network = VirtualNetwork(
            name=network_name,
            fq_name=nw_fq_name,
            parent_type='project',
            address_allocation_mode="user-defined-subnet-only")
        try:
            network.add_network_ipam(ipam, subnets)
            vnc_api.virtual_network_create(network)
        except Exception as ex:
            self._logger.error("virtual network '%s' already exists or "
                               "other conflict: %s" % (network_name, str(ex)))
            vnc_api.virtual_network_update(network)
Exemple #6
0
    def test_lr_v4_subnets(self):
        # Create Domain
        domain = Domain('my-lr-domain')
        self._vnc_lib.domain_create(domain)

        # Create Project
        project = Project('my-lr-proj', domain)
        self._vnc_lib.project_create(project)

        # Create NetworkIpam
        ipam = NetworkIpam('default-network-ipam', project, IpamType("dhcp"))
        self._vnc_lib.network_ipam_create(ipam)

        ipam = self._vnc_lib.network_ipam_read(
            ['my-lr-domain', 'my-lr-proj', 'default-network-ipam'])

        # Create subnets
        ipam_sn_v4_vn1 = IpamSubnetType(subnet=SubnetType('11.1.1.0', 24))
        ipam_sn_v6_vn1 = IpamSubnetType(subnet=SubnetType('fd11::', 120))
        ipam_sn_v4_vn2 = IpamSubnetType(subnet=SubnetType('11.1.2.0', 24))
        ipam_sn_v6_vn2 = IpamSubnetType(subnet=SubnetType('fd12::', 120))

        # Create VN my-vn-1
        vn1 = VirtualNetwork('my-vn-1', project)
        vn1.add_network_ipam(ipam,
                             VnSubnetsType([ipam_sn_v4_vn1, ipam_sn_v6_vn1]))
        self._vnc_lib.virtual_network_create(vn1)
        net_obj1 = self._vnc_lib.virtual_network_read(id=vn1.uuid)

        # Create VN my-vn-2
        vn2 = VirtualNetwork('my-vn-2', project)
        vn2.add_network_ipam(ipam,
                             VnSubnetsType([ipam_sn_v4_vn2, ipam_sn_v6_vn2]))
        self._vnc_lib.virtual_network_create(vn2)
        net_obj2 = self._vnc_lib.virtual_network_read(id=vn2.uuid)

        # Create Logical Router
        lr = LogicalRouter('router-test-v4-%s' % self.id(), project)
        lr_uuid = self._vnc_lib.logical_router_create(lr)

        # Create a Virtual Machine Interface belonging to my-vn-1
        id_perms = IdPermsType(enable=True)
        port_obj1 = VirtualMachineInterface(str(uuid.uuid4()),
                                            parent_obj=project,
                                            id_perms=id_perms)
        port_obj1.uuid = port_obj1.name
        port_obj1.set_virtual_network(vn1)
        port_obj1.set_virtual_machine_interface_device_owner(
            'DEVICE_OWNER_ROUTER_INTF')
        # Assign gateway ip
        ipam_refs = net_obj1.get_network_ipam_refs()
        for ipam_ref in ipam_refs:
            subnets = ipam_ref['attr'].get_ipam_subnets()
            for subnet in subnets:
                cidr = '%s/%s' % (subnet.subnet.get_ip_prefix(),
                                  subnet.subnet.get_ip_prefix_len())
                if IPNetwork(cidr).version == 4:
                    gateway_ip = subnet.get_default_gateway()
        self._vnc_lib.virtual_machine_interface_create(port_obj1)

        # Create v4 Ip object
        ip_obj1 = InstanceIp(name=str(uuid.uuid4()),
                             instance_ip_address=gateway_ip,
                             instance_ip_family='v4')
        ip_obj1.uuid = ip_obj1.name
        ip_obj1.set_virtual_machine_interface(port_obj1)
        ip_obj1.set_virtual_network(net_obj1)
        ip_id1 = self._vnc_lib.instance_ip_create(ip_obj1)

        # Add Router Interface (test being subnet)
        lr.add_virtual_machine_interface(port_obj1)
        self._vnc_lib.logical_router_update(lr)

        # Create a Virtual Machine Interface belonging to my-vn-2
        port_obj2 = VirtualMachineInterface(str(uuid.uuid4()),
                                            parent_obj=project,
                                            id_perms=id_perms)
        port_obj2.uuid = port_obj2.name
        port_obj2.set_virtual_network(vn2)
        port_obj2.set_virtual_machine_interface_device_owner(
            'DEVICE_OWNER_ROUTER_INTF')
        # Assign gateway ip
        ipam_refs = net_obj2.get_network_ipam_refs()
        for ipam_ref in ipam_refs:
            subnets = ipam_ref['attr'].get_ipam_subnets()
            for subnet in subnets:
                cidr = '%s/%s' % (subnet.subnet.get_ip_prefix(),
                                  subnet.subnet.get_ip_prefix_len())
                if IPNetwork(cidr).version == 4:
                    gateway_ip = subnet.get_default_gateway()
        self._vnc_lib.virtual_machine_interface_create(port_obj2)

        # Create v4 Ip object
        ip_obj2 = InstanceIp(name=str(uuid.uuid4()),
                             instance_ip_address=gateway_ip,
                             instance_ip_family='v4')
        ip_obj2.uuid = ip_obj2.name
        ip_obj2.set_virtual_machine_interface(port_obj2)
        ip_obj2.set_virtual_network(net_obj2)
        ip_id2 = self._vnc_lib.instance_ip_create(ip_obj2)

        # Add Router Interface (test being subnet)
        lr.add_virtual_machine_interface(port_obj2)
        self._vnc_lib.logical_router_update(lr)

        # TODO: Schema transformer not integrated in the tests,
        #       hence route-target refs not set yet
        # Verify Route Target Creation
        rt_refs = lr.get_route_target_refs()
        for rt_ref in rt_refs or []:
            rt_obj = self._vnc_lib.route_target_read(id=rt_ref['uuid'])
            ri_refs = rt_obj.get_routing_instance_back_refs()
            for ri_ref in ri_refs:
                ri_obj = self.vnc_lib.routing_instance_read(id=ri_ref['uuid'])
                ri_name = ri_obj.get_display_name()
                if ri_name != 'my-vn-1' and ri_name != 'my-vn-2':
                    pass

        # cleanup
        self._vnc_lib.instance_ip_delete(id=ip_id1)
        self._vnc_lib.instance_ip_delete(id=ip_id2)
        self._vnc_lib.logical_router_delete(id=lr_uuid)
        self._vnc_lib.virtual_machine_interface_delete(id=port_obj1.uuid)
        self._vnc_lib.virtual_machine_interface_delete(id=port_obj2.uuid)
        self._vnc_lib.virtual_network_delete(id=vn1.uuid)
        self._vnc_lib.virtual_network_delete(id=vn2.uuid)
        self._vnc_lib.network_ipam_delete(id=ipam.uuid)
        self._vnc_lib.project_delete(id=project.uuid)
        self._vnc_lib.domain_delete(id=domain.uuid)
Exemple #7
0
 def create_vn_ipam(self, id):
     ipam1_obj = NetworkIpam('ipam' + '-' + id)
     ipam1_uuid = self._vnc_lib.network_ipam_create(ipam1_obj)
     return self._vnc_lib.network_ipam_read(id=ipam1_uuid)
def gen_ipam(project):
    """ Returns a configured vnc_api.gen.resource_client.NetworkIpam """
    ipam = NetworkIpam(name=conf.get('default_net_name'), parent_obj=project)
    return ipam