From: rohitagarwalla Date: Tue, 9 Aug 2011 19:40:14 +0000 (-0700) Subject: Adding db connection and l2network plugin database modules X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=648ad36da900dc06e0a25cbf9f8ef2c0356dcd2a;p=openstack-build%2Fneutron-build.git Adding db connection and l2network plugin database modules --- diff --git a/quantum/plugins/cisco/db/db_conn.ini b/quantum/plugins/cisco/db/db_conn.ini new file mode 100644 index 000000000..55066aaeb --- /dev/null +++ b/quantum/plugins/cisco/db/db_conn.ini @@ -0,0 +1,5 @@ +[DATABASE] +name = quantum_l2network +user = root +pass = nova +host = 127.0.0.1 diff --git a/quantum/plugins/cisco/db/l2network_db.py b/quantum/plugins/cisco/db/l2network_db.py new file mode 100644 index 000000000..15861eee3 --- /dev/null +++ b/quantum/plugins/cisco/db/l2network_db.py @@ -0,0 +1,239 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# @author: Rohit Agarwalla, Cisco Systems, Inc. + +from sqlalchemy.orm import exc + +import l2network_models +import quantum.db.api as db +import quantum.db.models as models + + +def get_all_vlan_bindings(): + """Lists all the vlan to network associations""" + session = db.get_session() + try: + bindings = session.query(l2network_models.VlanBinding).\ + all() + return bindings + except exc.NoResultFound: + return [] + + +def get_vlan_binding(netid): + """Lists the vlan given a network_id""" + session = db.get_session() + try: + binding = session.query(l2network_models.VlanBinding).\ + filter_by(network_id=netid).\ + one() + return binding + except exc.NoResultFound: + raise Exception("No network found with net-id = %s" % network_id) + + +def add_vlan_binding(vlanid, vlanname, netid): + """Adds a vlan to network association""" + session = db.get_session() + try: + binding = session.query(l2network_models.VlanBinding).\ + filter_by(vlan_id=vlanid).\ + one() + raise Exception("Vlan with id \"%s\" already exists" % vlanid) + except exc.NoResultFound: + binding = l2network_models.VlanBinding(vlanid, vlanname, netid) + session.add(binding) + session.flush() + return binding + + +def remove_vlan_binding(netid): + """Removes a vlan to network association""" + session = db.get_session() + try: + binding = session.query(l2network_models.VlanBinding).\ + filter_by(network_id=netid).\ + one() + session.delete(binding) + session.flush() + return binding + except exc.NoResultFound: + pass + + +def update_vlan_binding(netid, newvlanid=None, newvlanname=None): + """Updates a vlan to network association""" + session = db.get_session() + try: + binding = session.query(l2network_models.VlanBinding).\ + filter_by(network_id=netid).\ + one() + if newvlanid: + binding.vlan_id = newvlanid + if newvlanname: + binding.vlan_name = newvlanname + session.merge(binding) + session.flush() + return binding + except exc.NoResultFound: + raise Exception("No vlan binding found with network_id = %s" % netid) + + +def get_all_portprofiles(): + """Lists all the port profiles""" + session = db.get_session() + try: + pps = session.query(l2network_models.PortProfile).\ + all() + return pps + except exc.NoResultFound: + return [] + + +def get_portprofile(ppid): + """Lists a port profile""" + session = db.get_session() + try: + pp = session.query(l2network_models.PortProfile).\ + filter_by(uuid=ppid).\ + one() + return pp + except exc.NoResultFound: + raise Exception("No portprofile found with id = %s" % ppid) + + +def add_portprofile(ppname, vlanid, qos): + """Adds a port profile""" + session = db.get_session() + try: + pp = session.query(l2network_models.PortProfile).\ + filter_by(name=ppname).\ + one() + raise Exception("Port profile with name %s already exists" % ppname) + except exc.NoResultFound: + pp = l2network_models.PortProfile(ppname, vlanid, qos) + session.add(pp) + session.flush() + return pp + + +def remove_portprofile(ppid): + """Removes a port profile""" + session = db.get_session() + try: + pp = session.query(l2network_models.PortProfile).\ + filter_by(uuid=ppid).\ + one() + session.delete(pp) + session.flush() + return pp + except exc.NoResultFound: + pass + + +def update_portprofile(ppid, newppname=None, newvlanid=None, newqos=None): + """Updates port profile""" + session = db.get_session() + try: + pp = session.query(l2network_models.PortProfile).\ + filter_by(uuid=ppid).\ + one() + if newppname: + pp.name = newppname + if newvlanid: + pp.vlan_id = newvlanid + if newqos: + pp.qos = newqos + session.merge(pp) + session.flush() + return pp + except exc.NoResultFound: + raise Exception("No port profile with id = %s" % ppid) + + +def get_all_pp_bindings(): + """Lists all the port profiles""" + session = db.get_session() + try: + bindings = session.query(l2network_models.PortProfileBinding).\ + all() + return bindings + except exc.NoResultFound: + return [] + + +def get_pp_binding(ppid): + """Lists a port profile binding""" + session = db.get_session() + try: + binding = session.query(l2network_models.PortProfileBinding).\ + filter_by(portprofile_id=ppid).\ + one() + return binding + except exc.NoResultFound: + raise Exception("No portprofile binding found with id = %s" % ppid) + + +def add_pp_binding(tenantid, networkid, ppid, default): + """Adds a port profile binding""" + session = db.get_session() + try: + binding = session.query(l2network_models.PortProfileBinding).\ + filter_by(portprofile_id=ppid).\ + one() + raise Exception("Port profile binding with id \"%s\" already \ + exists" % ppid) + except exc.NoResultFound: + binding = l2network_models.PortProfileBinding(tenantid, networkid, \ + ppid, default) + session.add(binding) + session.flush() + return binding + + +def remove_pp_binding(ppid): + """Removes a port profile binding""" + session = db.get_session() + try: + binding = session.query(l2network_models.PortProfileBinding).\ + filter_by(portprofile_id=ppid).\ + one() + session.delete(binding) + session.flush() + return binding + except exc.NoResultFound: + pass + + +def update_pp_binding(ppid, newtenantid=None, newnetworkid=None, \ + newdefault=None): + """Updates port profile binding""" + session = db.get_session() + try: + binding = session.query(l2network_models.PortProfileBinding).\ + filter_by(portprofile_id=ppid).\ + one() + if newtenantid: + binding.tenant_id = newtenantid + if newnetworkid: + binding.network_id = newnetworkid + if newdefault: + binding.default = newdefault + session.merge(binding) + session.flush() + return binding + except exc.NoResultFound: + raise Exception("No port profile binding with id = %s" % ppid) diff --git a/quantum/plugins/cisco/db/l2network_models.py b/quantum/plugins/cisco/db/l2network_models.py new file mode 100644 index 000000000..76321c74a --- /dev/null +++ b/quantum/plugins/cisco/db/l2network_models.py @@ -0,0 +1,86 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# @author: Rohit Agarwalla, Cisco Systems, Inc. + +import uuid + +from sqlalchemy import Column, Integer, String, ForeignKey, Boolean +from sqlalchemy.orm import relation + +from quantum.db.models import BASE + + +class VlanBinding(BASE): + """Represents a binding of vlan_id to network_id""" + __tablename__ = 'vlan_bindings' + + vlan_id = Column(Integer, primary_key=True) + vlan_name = Column(String(255)) + network_id = Column(String(255), nullable=False) + #foreign key to networks.uuid + + def __init__(self, vlan_id, vlan_name, network_id): + self.vlan_id = vlan_id + self.vlan_name = vlan_name + self.network_id = network_id + + def __repr__(self): + return "" % \ + (self.vlan_id, self.vlan_name, self.network_id) + + +class PortProfile(BASE): + """Represents L2 network plugin level PortProfile for a network""" + __tablename__ = 'portprofiles' + + uuid = Column(String(255), primary_key=True) + name = Column(String(255)) + vlan_id = Column(Integer) + qos = Column(String(255)) + + def __init__(self, name, vlan_id, qos=None): + self.uuid = uuid.uuid4() + self.name = name + self.vlan_id = vlan_id + self.qos = qos + + def __repr__(self): + return "" % \ + (self.uuid, self.name, self.vlan_id, self.qos) + + +class PortProfileBinding(BASE): + """Represents PortProfile binding to tenant and network""" + __tablename__ = 'portprofile_bindings' + + id = Column(Integer, primary_key=True, autoincrement=True) + tenant_id = Column(String(255)) + + network_id = Column(String(255), nullable=False) + #foreign key to networks.uuid + portprofile_id = Column(String(255), nullable=False) + #foreign key to portprofiles.uuid + default = Column(Boolean) + + def __init__(self, tenant_id, network_id, portprofile_id, default): + self.tenant_id = tenant_id + self.network_id = network_id + self.portprofile_id = portprofile_id + self.default = default + + def __repr__(self): + return "" % \ + (self.tenant_id, self.network_id, self.portprofile_id, self.default)