DB_PASS, DB_HOST, DB_NAME)}
db.configure_db(options)
-def prepopulate_vlan_bindings():
+def create_vlanids():
"""Prepopulates the vlan_bindings table"""
session = db.get_session()
try:
- binding = session.query(l2network_models.VlanBinding).\
- all()
+ vlanid = session.query(l2network_models.VlanID).\
+ one()
raise Exception("Vlan table not empty id for prepopulation")
except exc.NoResultFound:
- start = conf.VLAN_START
- end = conf.VLAN_END
- while start < end:
- binding = l2network_models.VlanBinding(vlanid, "", 0)
- session.add(binding)
+ start = int(conf.VLAN_START)
+ end = int(conf.VLAN_END)
+ while start <= end:
+ vlanid = l2network_models.VlanID(start)
+ session.add(vlanid)
+ start += 1
+ session.flush()
+ return
+
+def get_all_vlanids():
+ session = db.get_session()
+ try:
+ vlanids = session.query(l2network_models.VlanID).\
+ all()
+ return vlanids
+ except exc.NoResultFound:
+ return []
+
+def is_vlanid_used(vlan_id):
+ session = db.get_session()
+ try:
+ vlanid = session.query(l2network_models.VlanID).\
+ filter_by(vlan_id=vlan_id).\
+ one()
+ return vlanid["vlan_used"]
+ except exc.NoResultFound:
+ raise Exception("No vlan found with vlan-id = %s" % vlan_id)
+
+def release_vlanid(vlan_id):
+ session = db.get_session()
+ try:
+ vlanid = session.query(l2network_models.VlanID).\
+ filter_by(vlan_id=vlan_id).\
+ one()
+ vlanid["vlan_used"] = False
+ session.merge(vlanid)
session.flush()
+ return vlanid["vlan_used"]
+ except exc.NoResultFound:
+ raise Exception("Vlan id %s not present in table" % vlan_id)
+
return
+def delete_vlanid(vlan_id):
+ session = db.get_session()
+ try:
+ vlanid = session.query(l2network_models.VlanID).\
+ filter_by(vlan_id=vlan_id).\
+ one()
+ session.delete(vlanid)
+ session.flush()
+ return vlanid
+ except exc.NoResultFound:
+ pass
+
+def reserve_vlanid():
+ session = db.get_session()
+ try:
+ vlanids = session.query(l2network_models.VlanID).\
+ filter_by(vlan_used=False).\
+ all()
+ rvlan = vlanids[0]
+ rvlanid = session.query(l2network_models.VlanID).\
+ filter_by(vlan_id=rvlan["vlan_id"]).\
+ one()
+ rvlanid["vlan_used"] = True
+ session.merge(rvlanid)
+ session.flush()
+ return vlanids[0]["vlan_id"]
+ except exc.NoResultFound:
+ raise Exception("All vlan id's are used")
+
def get_all_vlan_bindings():
"""Lists all the vlan to network associations"""
session = db.get_session()
raise Exception("No portprofile binding found with id = %s" % ppid)
-def add_pp_binding(tenantid, networkid, ppid, default):
+def add_pp_binding(tenantid, portid, ppid, default):
"""Adds a port profile binding"""
session = db.get_session()
try:
raise Exception("Port profile binding with id \"%s\" already \
exists" % ppid)
except exc.NoResultFound:
- binding = l2network_models.PortProfileBinding(tenantid, networkid, \
+ binding = l2network_models.PortProfileBinding(tenantid, portid, \
ppid, default)
session.add(binding)
session.flush()
return binding
-def remove_pp_binding(ppid):
+def remove_pp_binding(portid, ppid):
"""Removes a port profile binding"""
session = db.get_session()
try:
binding = session.query(l2network_models.PortProfileBinding).\
filter_by(portprofile_id=ppid).\
+ filter_by(port_id=portid).\
one()
session.delete(binding)
session.flush()
pass
-def update_pp_binding(ppid, newtenantid=None, newnetworkid=None, \
+def update_pp_binding(ppid, newtenantid=None, newportid=None, \
newdefault=None):
"""Updates port profile binding"""
session = db.get_session()
one()
if newtenantid:
binding["tenant_id"] = newtenantid
- if newnetworkid:
- binding["network_id"] = newnetworkid
+ if newportid:
+ binding["port_id"] = newportid
if newdefault:
binding["default"] = newdefault
session.merge(binding)
local.update(joined)
return local.iteritems()
+class VlanID(BASE, L2NetworkBase):
+ """Represents a vlan_id usage"""
+ __tablename__ = 'vlan_ids'
+
+ vlan_id = Column(Integer, primary_key=True)
+ vlan_used = Column(Boolean)
+
+ def __init__(self, vlan_id):
+ self.vlan_id = vlan_id
+ self.vlan_used = False
+
+ def __repr__(self):
+ return "<VlanBinding(%d,%s)>" % \
+ (self.vlan_id, self.vlan_used)
+
class VlanBinding(BASE, L2NetworkBase):
"""Represents a binding of vlan_id to network_id"""
id = Column(Integer, primary_key=True, autoincrement=True)
tenant_id = Column(String(255))
- network_id = Column(String(255), ForeignKey("networks.uuid"), \
+ port_id = Column(String(255), ForeignKey("ports.uuid"), \
nullable=False)
portprofile_id = Column(String(255), ForeignKey("portprofiles.uuid"), \
nullable=False)
default = Column(Boolean)
- network = relation(models.Network, uselist=False)
+ ports = relation(models.Port)
portprofile = relation(PortProfile, uselist=False)
- def __init__(self, tenant_id, network_id, portprofile_id, default):
+ def __init__(self, tenant_id, port_id, portprofile_id, default):
self.tenant_id = tenant_id
- self.network_id = network_id
+ self.port_id = port_id
self.portprofile_id = portprofile_id
self.default = default
def __repr__(self):
return "<PortProfile Binding(%s,%s,%s,%s)>" % \
- (self.tenant_id, self.network_id, self.portprofile_id, self.default)
+ (self.tenant_id, self.port_id, self.portprofile_id, self.default)