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 gen_ipam_subnet(ip_prefix, ip_prefix_len, default_gateway): """ Returns a configured vnc_api.gen.resource_xsd.VnSubnetsType Filled with SubnetType and IpamSubnetType stuff. """ subnet = SubnetType(ip_prefix=ip_prefix, ip_prefix_len=ip_prefix_len) ipam_subnet = IpamSubnetType(subnet=subnet, default_gateway=default_gateway) vn_subnet = VnSubnetsType(ipam_subnets=[ipam_subnet]) return vn_subnet
def test_vm_port_not_added_to_lr(self): project = self._vnc_lib.project_read( ['default-domain', 'default-project']) ipam = self._vnc_lib.network_ipam_read( ['default-domain', 'default-project', 'default-network-ipam']) # Create subnets ipam_sn_v4_vn = IpamSubnetType(subnet=SubnetType('11.1.1.0', 24)) # Create VN my-vn vn = VirtualNetwork('%s-vn' % self.id(), project) vn.add_network_ipam(ipam, VnSubnetsType([ipam_sn_v4_vn])) self._vnc_lib.virtual_network_create(vn) net_obj = self._vnc_lib.virtual_network_read(id=vn.uuid) # Create v4 Ip object ip_obj = InstanceIp(name=str(uuid.uuid4()), instance_ip_family='v4') ip_obj.uuid = ip_obj.name # Create VM vm_inst_obj = VirtualMachine(str(uuid.uuid4())) vm_inst_obj.uuid = vm_inst_obj.name self._vnc_lib.virtual_machine_create(vm_inst_obj) id_perms = IdPermsType(enable=True) vm_port_obj = VirtualMachineInterface(str(uuid.uuid4()), vm_inst_obj, id_perms=id_perms) vm_port_obj.uuid = vm_port_obj.name vm_port_obj.set_virtual_network(vn) ip_obj.set_virtual_machine_interface(vm_port_obj) ip_obj.set_virtual_network(net_obj) self._vnc_lib.virtual_machine_interface_create(vm_port_obj) self._vnc_lib.instance_ip_create(ip_obj) # Create Logical Router lr = LogicalRouter('router-test-v4-%s' % self.id(), project) self._vnc_lib.logical_router_create(lr) # Add Router Interface lr.add_virtual_machine_interface(vm_port_obj) with ExpectedException(RefsExistError): self._vnc_lib.logical_router_update(lr) lr.del_virtual_machine_interface(vm_port_obj) # Create Port port_obj = self.create_port(project, net_obj) lr.add_virtual_machine_interface(port_obj) self._vnc_lib.logical_router_update(lr) with ExpectedException(BadRequest): port_obj.add_virtual_machine(vm_inst_obj) self._vnc_lib.virtual_machine_interface_update(port_obj) self._vnc_lib.logical_router_delete(id=lr.uuid)
def test_same_network_not_attached_to_lr(self): project = self._vnc_lib.project_read( ['default-domain', 'default-project']) ipam = self._vnc_lib.network_ipam_read( ['default-domain', 'default-project', 'default-network-ipam']) # Create subnets ipam_sn_v4_vn = IpamSubnetType(subnet=SubnetType('11.1.1.0', 24)) # Create VN my-vn vn = VirtualNetwork('%s-vn' % self.id(), project) vn.add_network_ipam(ipam, VnSubnetsType([ipam_sn_v4_vn])) self._vnc_lib.virtual_network_create(vn) net_obj = self._vnc_lib.virtual_network_read(id=vn.uuid) # Create v4 Ip object ip_obj = InstanceIp(name=str(uuid.uuid4()), instance_ip_family='v4') ip_obj.uuid = ip_obj.name # Create Port port_obj = self.create_port(project, net_obj) # Create Logical Router lr = LogicalRouter('router-test-v4-%s' % self.id(), project) lr.set_logical_router_type('snat-routing') self._vnc_lib.logical_router_create(lr) # Add Router Interface lr.add_virtual_machine_interface(port_obj) self._vnc_lib.logical_router_update(lr) # set router_external net_obj.set_router_external(True) self._vnc_lib.virtual_network_update(net_obj) with ExpectedException(BadRequest): lr.add_virtual_network(net_obj) self._vnc_lib.logical_router_update(lr) lr.del_virtual_network(net_obj) lr.del_virtual_machine_interface(port_obj) self._vnc_lib.logical_router_update(lr) lr.add_virtual_network(net_obj) self._vnc_lib.logical_router_update(lr) # Create Port port_obj = self.create_port(project, net_obj) with ExpectedException(BadRequest): lr.add_virtual_machine_interface(port_obj) self._vnc_lib.logical_router_update(lr) self._vnc_lib.logical_router_delete(id=lr.uuid)
def _create_vn(cls, vn_fq_name, svn_type, hbs_uuid, parent_uuid): # Create a VN attrs = { 'parent_type': 'project', 'parent_uuid': parent_uuid, } ok, result = cls.server.get_resource_class('virtual_network').locate( fq_name=vn_fq_name, **attrs) if not ok: return False, result vn_dict = result vn_uuid = vn_dict['uuid'] attr = ServiceVirtualNetworkType(svn_type) attr_as_dict = attr.__dict__ try: cls.server.internal_request_ref_update('host-based-service', hbs_uuid, 'ADD', 'virtual-network', vn_uuid, attr=attr_as_dict) except HttpError as e: return False, (e.status_code, e.content) # Create a subnet and link it to default ipam # ref update to VN ipam_obj_type = 'network_ipam' ipam_fq_name = [ 'default-domain', 'default-project', 'default-network-ipam' ] ipam_uuid = cls.db_conn.fq_name_to_uuid(ipam_obj_type, ipam_fq_name) subnet = SubnetType(cls.hbf[svn_type]['IP'], cls.hbf[svn_type]['PREFIX_LEN']) attr = VnSubnetsType([IpamSubnetType(subnet=subnet)]) attr_as_dict = json.loads(json.dumps(attr, default=_obj_serializer_all)) try: cls.server.internal_request_ref_update('virtual-network', vn_uuid, 'ADD', 'network-ipam', ipam_uuid, attr=attr_as_dict) except HttpError as e: return False, (e.status_code, e.content) return ok, vn_dict
def _create_subnet_data(vn_subnet): subnets = [vn_subnet] if isinstance(vn_subnet, basestring) else vn_subnet subnet_infos = [] for subnet in subnets: cidr = IPNetwork(subnet) subnet_infos.append( IpamSubnetType( subnet=SubnetType( str(cidr.network), int(cidr.prefixlen), ), default_gateway=str(IPAddress(cidr.last - 1)), subnet_uuid=str(uuid.uuid4()), )) subnet_data = VnSubnetsType(subnet_infos) return subnet_data
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
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)
def _create_dci_lo0_network_ipam(cls, subnet_list): db_conn = cls.db_conn obj_type = 'network-ipam' vn_fq_name = DCI_VN_FQ_NAME vn_id = db_conn.fq_name_to_uuid('virtual_network', vn_fq_name) ok, res = cls.dbe_read(db_conn, 'virtual_network', vn_id, obj_fields=['network_ipam_refs']) if not ok: return ok, res ipam_fq = DCI_IPAM_FQ_NAME # find ipam object if created already ipam_ref = cls._find_dci_ipam(ipam_fq, res.get('network_ipam_refs')) ipam_uuid = None if ipam_ref: try: ipam_uuid = db_conn.fq_name_to_uuid('network_ipam', ipam_fq) except NoIdError as e: return (False, str(e)) api_server = cls.server # create ipam object for the first time if not ipam_uuid: ipam = NetworkIpam(ipam_fq[-1]) ipam_dict = json.dumps(ipam, default=_obj_serializer_all) ipam_obj = None try: ok, ipam_obj = api_server.internal_request_create( obj_type, json.loads(ipam_dict)) except HttpError: return False, (e.status_code, e.content) ipam_obj = ipam_obj.get(obj_type) ipam_uuid = ipam_obj.get('uuid') # build ipam subnets ipam_list = [] if subnet_list and subnet_list.get("subnet"): sub_list = subnet_list.get("subnet") for sub in sub_list or []: ipam_sub = IpamSubnetType(subnet=SubnetType( sub.get("ip_prefix"), sub.get("ip_prefix_len"))) ipam_list.append(ipam_sub) # update ipam attr = VnSubnetsType(ipam_subnets=ipam_list) attr_dict = json.loads(json.dumps(attr, default=_obj_serializer_all)) op = 'ADD' try: api_server.internal_request_ref_update( 'virtual-network', vn_id, op, obj_type, ipam_uuid, ipam_fq, attr=attr_dict) except HttpError as e: return False, (e.status_code, e.content) return True, ''
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)
def _read_and_create_network_ipam(cls, vn_obj, subnets, ipam_fq_name): ipam_list = [] for subnet in subnets or []: ipam_sub = IpamSubnetType(subnet=SubnetType( subnet.get('ip_prefix'), subnet.get('ip_prefix_len')), default_gateway=None, enable_dhcp=False, addr_from_start=False) ipam_list.append(ipam_sub) if len(vn_obj.get('network_ipam_refs', [])) == 0: # create network IPAM kwargs = {'display_name': ipam_fq_name[-1]} kwargs['parent_type'] = 'project' kwargs['ipam_subnet_method'] = 'flat-subnet' kwargs['ipam_subnets'] = IpamSubnets(ipam_list) kwargs['ipam_subnetting'] = False ok, result = cls.server.get_resource_class('network_ipam').\ locate(ipam_fq_name, **kwargs) if not ok: return ok, result else: # Update network IPAM ok, result = cls.server.get_resource_class('network_ipam').\ locate(ipam_fq_name, create_it=False) if not ok: return ok, result ipam_obj = NetworkIpam(name=ipam_fq_name[-1], fq_name=ipam_fq_name, parent_type='project', ipam_subnet_method='flat-subnet', ipam_subnets=IpamSubnets(ipam_list), ipam_subnetting=False) ipam_dict = json.dumps(ipam_obj, default=_obj_serializer_all) try: cls.server.internal_request_update('network-ipam', result['uuid'], json.loads(ipam_dict)) except HttpError as e: return False, (e.status_code, e.content) ipam_uuid = result['uuid'] # Do this only for VN and IPAM creation.Skip for update. Currently we # support only one IPAM if len(vn_obj.get('network_ipam_refs', [])) == 0: try: op = 'ADD' cls.server.internal_request_ref_update( 'virtual-network', vn_obj['uuid'], op, 'network-ipam', ipam_uuid, ipam_fq_name, attr=json.loads( json.dumps(VnSubnetsType([]), default=_obj_serializer_all))) except HttpError as e: return False, (e.status_code, e.content) return True, result