def _create_test_vlan_allocation(self, vlan_id=None, allocated=False): attr = self.get_random_object_fields(vlan_alloc_obj.VlanAllocation) attr.update({ 'vlan_id': vlan_id or random.randint(constants.MIN_VLAN_TAG, constants.MAX_VLAN_TAG), 'physical_network': 'foo', 'allocated': allocated }) _vlan_allocation = vlan_alloc_obj.VlanAllocation(self.context, **attr) _vlan_allocation.create() return _vlan_allocation
def test_reserve_provider_segment(self): segment = { api.NETWORK_TYPE: p_const.TYPE_VLAN, api.PHYSICAL_NETWORK: PROVIDER_NET, api.SEGMENTATION_ID: 101 } alloc = self._get_allocation(self.context, segment) expected = vlan_alloc_obj.VlanAllocation(allocated=False, physical_network=PROVIDER_NET, vlan_id=101) self.assertEqual(expected.__repr__(), alloc.__repr__()) observed = self.driver.reserve_provider_segment(self.context, segment) alloc = self._get_allocation(self.context, observed) self.assertTrue(alloc.allocated)
def _sync_vlan_allocations(self): ctx = context.get_admin_context() with db_api.context_manager.writer.using(ctx): # get existing allocations for all physical networks allocations = dict() allocs = vlanalloc.VlanAllocation.get_objects(ctx) for alloc in allocs: if alloc.physical_network not in allocations: allocations[alloc.physical_network] = list() allocations[alloc.physical_network].append(alloc) # process vlan ranges for each configured physical network for (physical_network, vlan_ranges) in self.network_vlan_ranges.items(): # determine current configured allocatable vlans for # this physical network vlan_ids = set() for vlan_min, vlan_max in vlan_ranges: vlan_ids |= set(moves.range(vlan_min, vlan_max + 1)) # remove from table unallocated vlans not currently # allocatable if physical_network in allocations: for alloc in allocations[physical_network]: try: # see if vlan is allocatable vlan_ids.remove(alloc.vlan_id) except KeyError: # it's not allocatable, so check if its allocated if not alloc.allocated: # it's not, so remove it from table LOG.debug( "Removing vlan %(vlan_id)s on " "physical network " "%(physical_network)s from pool", { 'vlan_id': alloc.vlan_id, 'physical_network': physical_network }) # This UPDATE WHERE statement blocks anyone # from concurrently changing the allocation # values to True while our transaction is # open so we don't accidentally delete # allocated segments. If someone has already # allocated, update_objects will return 0 so we # don't delete. if vlanalloc.VlanAllocation.update_objects( ctx, values={'allocated': False}, allocated=False, vlan_id=alloc.vlan_id, physical_network=physical_network): alloc.delete() del allocations[physical_network] # add missing allocatable vlans to table for vlan_id in sorted(vlan_ids): alloc = vlanalloc.VlanAllocation( ctx, physical_network=physical_network, vlan_id=vlan_id, allocated=False) alloc.create() # remove from table unallocated vlans for any unconfigured # physical networks for allocs in allocations.values(): for alloc in allocs: if not alloc.allocated: LOG.debug( "Removing vlan %(vlan_id)s on physical " "network %(physical_network)s from pool", { 'vlan_id': alloc.vlan_id, 'physical_network': alloc.physical_network }) ctx.session.delete(alloc)