From: Ghe Rivero Date: Tue, 13 Dec 2011 12:14:22 +0000 (+0100) Subject: Improved VlanMap. X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=7a7e8e2bc14d9cc17f02b782a83f2125008f16da;p=openstack-build%2Fneutron-build.git Improved VlanMap. Simplified logic and less time spent creating vlans. Change-Id: I90fda09faa1869b38993aa0aeed64d813d29afa9 --- diff --git a/quantum/plugins/openvswitch/ovs_quantum_plugin.py b/quantum/plugins/openvswitch/ovs_quantum_plugin.py index 6889fa377..4b09b1218 100644 --- a/quantum/plugins/openvswitch/ovs_quantum_plugin.py +++ b/quantum/plugins/openvswitch/ovs_quantum_plugin.py @@ -41,32 +41,39 @@ LOG.getLogger("ovs_quantum_plugin") class VlanMap(object): vlans = {} + net_ids = {} + free_vlans = set() def __init__(self): - for x in xrange(2, 4094): - self.vlans[x] = None + self.vlans.clear() + self.net_ids.clear() + self.free_vlans = set(xrange(2, 4094)) - def set(self, vlan_id, network_id): + def set_vlan(self, vlan_id, network_id): self.vlans[vlan_id] = network_id + self.net_ids[network_id] = vlan_id def acquire(self, network_id): - for x in xrange(2, 4094): - if self.vlans[x] is None: - self.vlans[x] = network_id - # LOG.debug("VlanMap::acquire %s -> %s" % (x, network_id)) - return x - raise Exception("No free vlans..") + if len(self.free_vlans): + vlan = self.free_vlans.pop() + self.set_vlan(vlan, network_id) + # LOG.debug("VlanMap::acquire %s -> %s", x, network_id) + return vlan + else: + raise Exception("No free vlans..") def get(self, vlan_id): - return self.vlans[vlan_id] + return self.vlans.get(vlan_id, None) def release(self, network_id): - for x in self.vlans.keys(): - if self.vlans[x] == network_id: - self.vlans[x] = None - # LOG.debug("VlanMap::release %s" % (x)) - return - LOG.error("No vlan found with network \"%s\"" % network_id) + vlan = self.net_ids.get(network_id, None) + if vlan is not None: + self.free_vlans.add(vlan) + del self.vlans[vlan] + del self.net_ids[network_id] + # LOG.debug("VlanMap::release %s", vlan) + else: + LOG.error("No vlan found with network \"%s\"", network_id) class OVSQuantumPlugin(QuantumPluginBase): @@ -97,7 +104,7 @@ class OVSQuantumPlugin(QuantumPluginBase): vlan_id, network_id = x # LOG.debug("Adding already populated vlan %s -> %s" # % (vlan_id, network_id)) - self.vmap.set(vlan_id, network_id) + self.vmap.set_vlan(vlan_id, network_id) def get_all_networks(self, tenant_id): nets = [] diff --git a/quantum/plugins/openvswitch/tests/unit/test_vlan_map.py b/quantum/plugins/openvswitch/tests/unit/test_vlan_map.py index c4a6d8190..f0dde727c 100644 --- a/quantum/plugins/openvswitch/tests/unit/test_vlan_map.py +++ b/quantum/plugins/openvswitch/tests/unit/test_vlan_map.py @@ -34,3 +34,12 @@ class VlanMapTest(unittest.TestCase): vlan_id = self.vmap.acquire("foobar") self.vmap.release("foobar") self.assertTrue(self.vmap.get(vlan_id) is None) + + def testAddRelease4kVlans(self): + vlan_id = None + for id in range(2, 4000): + vlan_id = self.vmap.acquire(id) + self.assertTrue(vlan_id == id) + for id in range(2, 4000): + self.vmap.release(id) + self.assertTrue(self.vmap.get(id) is None)