def test_deallocate_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn_obj = VirtualNetwork('%s-vn' % self.id()) vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(6002) vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_create(vn_obj) # VN created, now read back the VN data to check if vxlan_id is set vn_obj = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties = vn_obj.get_virtual_network_properties() if not vn_obj_properties: self.fail("VN properties are not set") vxlan_id = vn_obj_properties.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6002) self.api.virtual_network_delete(id=vn_obj.uuid) self.assertNotEqual(vn_obj.get_fq_name_str() + "_vxlan", mock_zk.get_vn_from_id(vxlan_id)) logger.debug('PASS - test_deallocate_vxlan_id')
def test_deallocate_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn_obj = VirtualNetwork('%s-vn' % self.id()) vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(6002) vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_create(vn_obj) # VN created, now read back the VN data to check if vxlan_id is set vn_obj = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties = vn_obj.get_virtual_network_properties() if not vn_obj_properties: self.fail("VN properties are not set") vxlan_id = vn_obj_properties.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6002) self.api.virtual_network_delete(id=vn_obj.uuid) self.assertNotEqual(vn_obj.get_fq_name_str() + "_vxlan", mock_zk.get_vn_from_id(vxlan_id)) logger.debug('PASS - test_deallocate_vxlan_id')
def test_context_undo_vxlan_id_fail_db_create(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn_obj = VirtualNetwork('%s-vn' % self.id()) vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(6000) vn_obj.set_virtual_network_properties(vn_obj_properties) def stub(*args, **kwargs): return (False, (500, "Fake error")) zk_alloc_count_start = mock_zk._vn_id_allocator.get_alloc_count() with ExpectedException(HttpError): with test_common.flexmocks( [(self._api_server._db_conn, 'dbe_create', stub)]): self.api.virtual_network_create(vn_obj) # make sure allocation counter stays the same zk_alloc_count_current = mock_zk._vn_id_allocator.get_alloc_count() self.assertEqual(zk_alloc_count_start, zk_alloc_count_current)
def test_update_auto_vxlan_id_with_the_same_value(self): """ Test case. 1. Set VxLAN identifier mode to 'automatic'. 2. Create new VirtualNetwork. 3. Set VxLAN identifier mode to 'configured'. 4. Update VirtualNetwork with vxlan network identifier equal to network id. """ gvc_fq_name = [ 'default-global-system-config', 'default-global-vrouter-config' ] vxlan_id_mode = {'auto': 'automatic', 'user': '******'} # Set VxLAN identifier mode to 'automatic' gvc = self.api.global_vrouter_config_read(fq_name=gvc_fq_name) gvc.set_vxlan_network_identifier_mode(vxlan_id_mode['auto']) self.api.global_vrouter_config_update(gvc) gvc = self.api.global_vrouter_config_read(fq_name=gvc_fq_name) # verify vxlan id mode has been set self.assertEqual(gvc.vxlan_network_identifier_mode, vxlan_id_mode['auto']) # Create new VirtualNetwork vn = VirtualNetwork('%s-vn' % self.id()) self.api.virtual_network_create(vn) vn = self.api.virtual_network_read(fq_name=vn.fq_name) # verify vn_network_id has been set vn_network_id = vn.get_virtual_network_network_id() self.assertTrue(vn_network_id > 0) # Set VxLAN identifier mode to 'configured' (user defined) gvc.set_vxlan_network_identifier_mode(vxlan_id_mode['user']) self.api.global_vrouter_config_update(gvc) gvc = self.api.global_vrouter_config_read(fq_name=gvc_fq_name) # verify vxlan id mode has been set self.assertEqual(gvc.vxlan_network_identifier_mode, vxlan_id_mode['user']) # Update VirtualNetwork with vxlan network identifier # equal to network id vn_properties = VirtualNetworkType() vn_properties.set_vxlan_network_identifier(vn_network_id) vn.set_virtual_network_properties(vn_properties) self.api.virtual_network_update(vn) # verify vn_network_id is the same as vxlan_network_id vn = self.api.virtual_network_read(fq_name=vn.fq_name) vxlan_id = vn.get_virtual_network_properties() \ .get_vxlan_network_identifier() self.assertEqual(vn_network_id, vxlan_id)
def set_vxlan_id(self, vxlan_id): props = self.vnc_obj.get_virtual_network_properties() props = props if props else VirtualNetworkType() props.set_vlan_network_identifier(int(vxlan_id)) self.vnc_obj.set_virtual_network_properties(props) self.vnc_api.virtual_network_update(self.vnc_obj) self.update()
def set_forwarding_mode(self, forwarding_mode): props = self.vnc_obj.get_virtual_network_properties() props = props if props else VirtualNetworkType() props.set_forwarding_mode(forwarding_mode) self.vnc_obj.set_virtual_network_properties(props) self.vnc_api.virtual_network_update(self.vnc_obj) self.update()
def create_network(self, name, proj_obj, subnet, ipam_name): vn = VirtualNetwork( name=name, parent_obj=proj_obj, virtual_network_properties=VirtualNetworkType(forwarding_mode='l3'), address_allocation_mode='user-defined-subnet-only') try: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) except NoIdError: # Virtual network does not exist. Create one. vn_uuid = self._vnc_lib.virtual_network_create(vn) vn_obj = self._vnc_lib.virtual_network_read(id=vn_uuid) ipam_obj = self._create_network_ipam(ipam_name, 'flat-subnet', subnet, proj_obj, vn_obj) try: self._vnc_lib.virtual_network_update(vn_obj) except Exception as e: self.logger.error("%s - failed to update virtual network %s %s. %s" % (self._name, vn_obj.uuid, str(vn_obj.fq_name), str(e))) vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn_obj.get_fq_name()) #kube = vnc_kubernetes.VncKubernetes.get_instance() #kube._create_cluster_service_fip_pool(vn_obj, pod_ipam_obj) return vn_obj
def _create_virtual_network(self, vn_name, proj_obj, ipam_obj, \ ipam_update, provider=None, subnets=None, \ type='flat-subnet-only'): vn_exists = False vn = VirtualNetwork(name=vn_name, parent_obj=proj_obj, address_allocation_mode=type) try: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) vn_exists = True except NoIdError: # VN does not exist. Create one. vn_obj = vn if vn_exists: return vn_obj # Attach IPAM to virtual network. # # For flat-subnets, the subnets are specified on the IPAM and # not on the virtual-network to IPAM link. So pass an empty # list of VnSubnetsType. # For user-defined-subnets, use the provided subnets if ipam_update or \ not self._is_ipam_exists(vn_obj, ipam_obj.get_fq_name()): if subnets and type == 'user-defined-subnet-only': vn_obj.add_network_ipam(ipam_obj, subnets) else: vn_obj.add_network_ipam(ipam_obj, VnSubnetsType([])) vn_obj.set_virtual_network_properties( VirtualNetworkType(forwarding_mode='l3')) fabric_snat = False if self.ip_fabric_snat: fabric_snat = True if not vn_exists: if self.ip_fabric_forwarding: if provider: #enable ip_fabric_forwarding vn_obj.add_virtual_network(provider) elif fabric_snat: #enable fabric_snat vn_obj.set_fabric_snat(True) else: #disable fabric_snat vn_obj.set_fabric_snat(False) # Create VN. self._vnc_lib.virtual_network_create(vn_obj) else: # TODO: Handle Network update pass vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn_obj.get_fq_name()) VirtualNetworkKM.locate(vn_obj.uuid) return vn_obj
def _create_isolated_ns_virtual_network(self, ns_name, vn_name, proj_obj, ipam_obj=None, provider=None): """ Create a virtual network for this namespace. """ vn = VirtualNetwork( name=vn_name, parent_obj=proj_obj, virtual_network_properties=VirtualNetworkType(forwarding_mode='l3'), address_allocation_mode='flat-subnet-only') # Add annotatins on this isolated virtual-network. VirtualNetworkKM.add_annotations(self, vn, namespace=ns_name, name=ns_name, isolated='True') try: vn_uuid = self._vnc_lib.virtual_network_create(vn) except RefsExistError: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) vn_uuid = vn_obj.uuid vn = vn_obj # Instance-Ip for pods on this VN, should be allocated from # cluster pod ipam. Attach the cluster pod-ipam object # to this virtual network. vn.add_network_ipam(ipam_obj, VnSubnetsType([])) # enable ip-fabric-forwarding if provider: ip_fabric_forwarding = self._get_ip_fabric_forwarding(ns_name) if ip_fabric_forwarding == True: add_provider = True elif ip_fabric_forwarding == False: add_provider = False else: add_provider = self._args.ip_fabric_forwarding if add_provider: vn.add_virtual_network(provider) else: vn_refs = vn.get_virtual_network_refs() for vn_ref in vn_refs or []: vn_ref_obj = self._vnc_lib.virtual_network_read(id=vn_ref['uuid']) vn.del_virtual_network(vn_ref_obj) # Update VN. self._vnc_lib.virtual_network_update(vn) # Cache the virtual network. VirtualNetworkKM.locate(vn_uuid) return vn
def create_network(self, name, proj_obj): vn = VirtualNetwork( name=name, parent_obj=proj_obj, virtual_network_properties=VirtualNetworkType(forwarding_mode='l3'), address_allocation_mode='flat-subnet-only') try: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) except NoIdError: # Virtual network does not exist. Create one. uid = self._vnc_lib.virtual_network_create(vn) vn_obj = self._vnc_lib.virtual_network_read(id=uid) return vn_obj
def _create_isolated_ns_virtual_network(self, ns_name, vn_name, proj_obj): """ Create a virtual network for this namespace. """ vn = VirtualNetwork(name=vn_name, parent_obj=proj_obj, virtual_network_properties=VirtualNetworkType( forwarding_mode='l3'), address_allocation_mode='flat-subnet-only') # Add annotatins on this isolated virtual-network. VirtualNetworkKM.add_annotations(self, vn, namespace=ns_name, name=ns_name, isolated='True') try: vn_uuid = self._vnc_lib.virtual_network_create(vn) except RefsExistError: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) vn_uuid = vn_obj.uuid # Instance-Ip for pods on this VN, should be allocated from # cluster pod ipam. Attach the cluster pod-ipam object # to this virtual network. ipam_fq_name = vnc_kube_config.pod_ipam_fq_name() ipam_obj = self._vnc_lib.network_ipam_read(fq_name=ipam_fq_name) vn.add_network_ipam(ipam_obj, VnSubnetsType([])) # Update VN. self._vnc_lib.virtual_network_update(vn) try: ip_fabric_vn_obj = self._vnc_lib. \ virtual_network_read(fq_name=self._ip_fabric_fq_name) self._create_attach_policy(proj_obj, ip_fabric_vn_obj, vn) except NoIdError: pass # Cache the virtual network. VirtualNetworkKM.locate(vn_uuid) # Cache network info in namespace entry. self._set_namespace_virtual_network(ns_name, vn.get_fq_name()) return vn_uuid
def update_vn(self, connections=None, uuid=None, prop_kv=None, obj=None): vnc_h = connections.orch.vnc_h if not obj: obj = vnc_h.virtual_network_read(id=uuid) for k,v in prop_kv.items(): if '.' in k: #SubField Match field = k.split('.')[0] subfield = k.split('.')[1] prop = eval('obj.get_'+field)() or VirtualNetworkType() #ToDo setattr(prop, subfield, v) eval('obj.set_'+field)(prop) else: setattr(obj, k, v) try: vnc_h.virtual_network_update(obj) self.logger.info('Updated VN %s'%uuid) return True except PermissionDenied: self.logger.info('Permission Denied to update VN %s, kv %s'%(uuid, prop_kv)) return False
def test_update_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) vn_obj = VirtualNetwork('%s-vn' % self.id()) vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(6003) vn_obj_properties.set_forwarding_mode('l2_l3') vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_create(vn_obj) # VN created, now read back the VN data to check if vxlan_id is set vn_obj_read = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6003) # Created VN. Now Update it with a different vxlan_id vn_obj_properties.set_vxlan_network_identifier(6004) vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_update(vn_obj) vn_obj_read = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6004) self.api.virtual_network_delete(id=vn_obj.uuid) logger.debug('PASS - test_update_vxlan_id')
def create_vn_with_subnets(self, id, vn_name, ipam_obj, subnet, subnetmask=24): vn_obj = VirtualNetwork(vn_name) vn_obj_properties = VirtualNetworkType() vn_obj_properties.set_vxlan_network_identifier(2000 + id) vn_obj_properties.set_forwarding_mode('l2_l3') vn_obj.set_virtual_network_properties(vn_obj_properties) vn_obj.add_network_ipam( ipam_obj, VnSubnetsType([IpamSubnetType(SubnetType(subnet, subnetmask))])) vn_uuid = self._vnc_lib.virtual_network_create(vn_obj) return self._vnc_lib.virtual_network_read(id=vn_uuid)
def test_context_undo_vn_to_vxlan_id_fail_db_update(self): # Enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn_obj = VirtualNetwork('%s-vn' % self.id()) self.api.virtual_network_create(vn_obj) vn_fqname = mock_zk.get_vn_from_id(vn_obj.virtual_network_network_id) vn_id = vn_obj.virtual_network_network_id # Change vn to vxlan type vxlan_id = 6000 vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(vxlan_id) vn_obj_properties.set_forwarding_mode('l2_l3') vn_obj.set_virtual_network_properties(vn_obj_properties) def stub(*args, **kwargs): return (False, (500, "Fake error")) zk_alloc_count_start = mock_zk._vn_id_allocator.get_alloc_count() with ExpectedException(HttpError): with test_common.flexmocks( [(self._api_server._db_conn, 'dbe_update', stub)]): self.api.virtual_network_update(vn_obj) # Make sure vxlan_id was dealocated new_vxlan_fqname = mock_zk.get_vn_from_id(vxlan_id) self.assertEqual(new_vxlan_fqname, None) # Make sure vn id is the same new_vn_id = vn_obj.virtual_network_network_id self.assertEqual(vn_id, new_vn_id) # Make sure fqname is the same fot vn_id update_vn_fqname = mock_zk.get_vn_from_id( vn_obj.virtual_network_network_id) self.assertEqual(vn_fqname, update_vn_fqname) # Make sure allocation counter stays the same zk_alloc_count_current = mock_zk._vn_id_allocator.get_alloc_count() self.assertEqual(zk_alloc_count_start, zk_alloc_count_current)
def test_update_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) vn_obj = VirtualNetwork('%s-vn' % self.id()) vn_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn_obj_properties.set_vxlan_network_identifier(6003) vn_obj_properties.set_forwarding_mode('l2_l3') vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_create(vn_obj) # VN created, now read back the VN data to check if vxlan_id is set vn_obj_read = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6003) # Created VN. Now Update it with a different vxlan_id vn_obj_properties.set_vxlan_network_identifier(6004) vn_obj.set_virtual_network_properties(vn_obj_properties) self.api.virtual_network_update(vn_obj) vn_obj_read = self.api.virtual_network_read(id=vn_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6004) self.api.virtual_network_delete(id=vn_obj.uuid) logger.debug('PASS - test_update_vxlan_id')
def create_vn_with_subnets(self, id, vn_name, ipam_obj, subnet, subnetmask=24): vn_obj = VirtualNetwork(vn_name) vn_obj_properties = VirtualNetworkType() vn_obj_properties.set_vxlan_network_identifier(2000 + id) vn_obj_properties.set_forwarding_mode('l2_l3') vn_obj.set_virtual_network_properties(vn_obj_properties) vn_obj.add_network_ipam(ipam_obj, VnSubnetsType( [IpamSubnetType(SubnetType(subnet, subnetmask))])) vn_uuid = self._vnc_lib.virtual_network_create(vn_obj) vn_obj_rd = self._vnc_lib.virtual_network_read(id=vn_uuid) # make sure RT for vn is created rt = [] try: rt = self._get_route_target(vn_obj_rd) except Exception: pass return vn_obj, self._vnc_lib.virtual_network_read(id=vn_uuid), rt
def test_cannot_allocate_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn1_obj = VirtualNetwork('%s-vn' % self.id()) vn1_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn1_obj_properties.set_vxlan_network_identifier(6001) vn1_obj_properties.set_forwarding_mode('l2_l3') vn1_obj.set_virtual_network_properties(vn1_obj_properties) self.api.virtual_network_create(vn1_obj) # VN created, now read back the VN data to check if vxlan_id is set vn1_obj = self.api.virtual_network_read(id=vn1_obj.uuid) vn1_obj_properties = vn1_obj.get_virtual_network_properties() if not vn1_obj_properties: self.fail("VN properties are not set") vxlan_id = vn1_obj_properties.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6001) # Verified vxlan_id for VN1, now create VN2 with same vxlan_id vn2_obj = VirtualNetwork('%s-vn2' % self.id()) vn2_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn2_obj_properties.set_vxlan_network_identifier(6001) vn2_obj_properties.set_forwarding_mode('l2_l3') vn2_obj.set_virtual_network_properties(vn2_obj_properties) with ExpectedException(BadRequest): self.api.virtual_network_create(vn2_obj) self.assertEqual(vn1_obj.get_fq_name_str() + "_vxlan", mock_zk.get_vn_from_id(vxlan_id)) self.assertGreaterEqual(vxlan_id, VNID_MIN_ALLOC) self.api.virtual_network_delete(id=vn1_obj.uuid) logger.debug('PASS - test_cannot_allocate_vxlan_id')
def test_cannot_update_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) vn1_obj = VirtualNetwork('%s-vn1' % self.id()) vn1_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn1_obj_properties.set_vxlan_network_identifier(6005) vn1_obj_properties.set_forwarding_mode('l2_l3') vn1_obj.set_virtual_network_properties(vn1_obj_properties) self.api.virtual_network_create(vn1_obj) # VN created, create second VN with different vxlan_id vn2_obj = VirtualNetwork('%s-vn2' % self.id()) vn2_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn2_obj_properties.set_vxlan_network_identifier(6006) vn2_obj_properties.set_forwarding_mode('l2_l3') vn2_obj.set_virtual_network_properties(vn2_obj_properties) self.api.virtual_network_create(vn2_obj) # Created Two VNs. Now Update it second VN with 1st VNs VXLAN_ID vn2_obj_properties.set_vxlan_network_identifier(6005) vn2_obj.set_virtual_network_properties(vn2_obj_properties) with ExpectedException(BadRequest): self.api.virtual_network_update(vn2_obj) vn_obj_read = self.api.virtual_network_read(id=vn2_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6006) self.api.virtual_network_delete(id=vn2_obj.uuid) self.api.virtual_network_delete(id=vn1_obj.uuid) logger.debug('PASS - test_cannot_update_vxlan_id')
def test_cannot_allocate_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) mock_zk = self._api_server._db_conn._zk_db vn1_obj = VirtualNetwork('%s-vn' % self.id()) vn1_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn1_obj_properties.set_vxlan_network_identifier(6001) vn1_obj_properties.set_forwarding_mode('l2_l3') vn1_obj.set_virtual_network_properties(vn1_obj_properties) self.api.virtual_network_create(vn1_obj) # VN created, now read back the VN data to check if vxlan_id is set vn1_obj = self.api.virtual_network_read(id=vn1_obj.uuid) vn1_obj_properties = vn1_obj.get_virtual_network_properties() if not vn1_obj_properties: self.fail("VN properties are not set") vxlan_id = vn1_obj_properties.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6001) # Verified vxlan_id for VN1, now create VN2 with same vxlan_id vn2_obj = VirtualNetwork('%s-vn2' % self.id()) vn2_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn2_obj_properties.set_vxlan_network_identifier(6001) vn2_obj_properties.set_forwarding_mode('l2_l3') vn2_obj.set_virtual_network_properties(vn2_obj_properties) with ExpectedException(BadRequest): self.api.virtual_network_create(vn2_obj) self.assertEqual(vn1_obj.get_fq_name_str() + "_vxlan", mock_zk.get_vn_from_id(vxlan_id)) self.assertGreaterEqual(vxlan_id, VNID_MIN_ALLOC) self.api.virtual_network_delete(id=vn1_obj.uuid) logger.debug('PASS - test_cannot_allocate_vxlan_id')
def _create_isolated_ns_virtual_network(self, ns_name, vn_name, vn_type, proj_obj, ipam_obj=None, provider=None, enforce_policy=False): """ Create/Update a virtual network for this namespace. """ vn_exists = False vn = VirtualNetwork(name=vn_name, parent_obj=proj_obj, virtual_network_properties=VirtualNetworkType( forwarding_mode='l3'), address_allocation_mode='flat-subnet-only') try: vn_obj = self._vnc_lib.virtual_network_read( fq_name=vn.get_fq_name()) vn_exists = True except NoIdError: # VN does not exist. Create one. vn_obj = vn # Add annotatins on this isolated virtual-network. VirtualNetworkKM.add_annotations(self, vn, namespace=ns_name, name=ns_name, isolated='True') # Instance-Ip for pods on this VN, should be allocated from # cluster pod ipam. Attach the cluster pod-ipam object # to this virtual network. vn_obj.add_network_ipam(ipam_obj, VnSubnetsType([])) fabric_snat = False if vn_type == 'pod-network': if self._is_ip_fabric_snat_enabled(ns_name): fabric_snat = True if not vn_exists: if provider: # enable ip_fabric_forwarding vn_obj.add_virtual_network(provider) elif fabric_snat: # enable fabric_snat vn_obj.set_fabric_snat(True) else: # disable fabric_snat vn_obj.set_fabric_snat(False) vn_uuid = self._vnc_lib.virtual_network_create(vn_obj) # Cache the virtual network. VirtualNetworkKM.locate(vn_uuid) else: ip_fabric_enabled = False if provider: vn_refs = vn_obj.get_virtual_network_refs() ip_fabric_fq_name = provider.fq_name for vn in vn_refs or []: vn_fq_name = vn['to'] if vn_fq_name == ip_fabric_fq_name: ip_fabric_enabled = True break if not ip_fabric_enabled and fabric_snat: # enable fabric_snat vn_obj.set_fabric_snat(True) else: # disable fabric_snat vn_obj.set_fabric_snat(False) # Update VN. self._vnc_lib.virtual_network_update(vn_obj) vn_uuid = vn_obj.get_uuid() vn_obj = self._vnc_lib.virtual_network_read(id=vn_uuid) # If required, enforce security policy at virtual network level. if enforce_policy: self._vnc_lib.set_tags( vn_obj, self._labels.get_labels_dict( VncSecurityPolicy.cluster_aps_uuid)) return vn_obj
def test_cannot_update_vxlan_id(self): # enable vxlan routing on project proj = self._vnc_lib.project_read( fq_name=["default-domain", "default-project"]) proj.set_vxlan_routing(True) self._vnc_lib.project_update(proj) vn1_obj = VirtualNetwork('%s-vn1' % self.id()) vn1_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn1_obj_properties.set_vxlan_network_identifier(6005) vn1_obj_properties.set_forwarding_mode('l2_l3') vn1_obj.set_virtual_network_properties(vn1_obj_properties) self.api.virtual_network_create(vn1_obj) # VN created, create second VN with different vxlan_id vn2_obj = VirtualNetwork('%s-vn2' % self.id()) vn2_obj_properties = VirtualNetworkType(forwarding_mode='l3') vn2_obj_properties.set_vxlan_network_identifier(6006) vn2_obj_properties.set_forwarding_mode('l2_l3') vn2_obj.set_virtual_network_properties(vn2_obj_properties) self.api.virtual_network_create(vn2_obj) # Created Two VNs. Now Update it second VN with 1st VNs VXLAN_ID vn2_obj_properties.set_vxlan_network_identifier(6005) vn2_obj.set_virtual_network_properties(vn2_obj_properties) with ExpectedException(BadRequest): self.api.virtual_network_update(vn2_obj) vn_obj_read = self.api.virtual_network_read(id=vn2_obj.uuid) vn_obj_properties_read = vn_obj_read.get_virtual_network_properties() if not vn_obj_properties_read: self.fail("VN properties are not set") vxlan_id = vn_obj_properties_read.get_vxlan_network_identifier() self.assertEqual(vxlan_id, 6006) self.api.virtual_network_delete(id=vn2_obj.uuid) self.api.virtual_network_delete(id=vn1_obj.uuid) logger.debug('PASS - test_cannot_update_vxlan_id')