From 74623fee4bf4dec356ee48efde50f3af35691897 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Mon, 2 Feb 2015 09:30:02 -0800 Subject: [PATCH] NSX DB models split, part 2 Move models for mac learning and logical services extensions into the module neutron.plugins.vmware.dbexts.nsx_models This patch is part of a set of minor refactorings in the NSX plugin which will then lead to a semi-full decomposition by the end of the Kilo release cycle. Related-to blueprint core-vendor-decomposition Change-Id: I0faba801b90893d949655e895899dd6aa0da6d96 --- neutron/plugins/vmware/dbexts/lsn_db.py | 51 ++++---------------- neutron/plugins/vmware/dbexts/maclearning.py | 30 +++--------- neutron/plugins/vmware/dbexts/nsx_models.py | 45 +++++++++++++++++ 3 files changed, 62 insertions(+), 64 deletions(-) diff --git a/neutron/plugins/vmware/dbexts/lsn_db.py b/neutron/plugins/vmware/dbexts/lsn_db.py index a4865bdfc..fd6019713 100644 --- a/neutron/plugins/vmware/dbexts/lsn_db.py +++ b/neutron/plugins/vmware/dbexts/lsn_db.py @@ -16,70 +16,39 @@ # from oslo.db import exception as d_exc -from sqlalchemy import Column -from sqlalchemy import ForeignKey from sqlalchemy import orm -from sqlalchemy import String -from neutron.db import models_v2 from neutron.openstack.common import log as logging from neutron.plugins.vmware.common import exceptions as p_exc +from neutron.plugins.vmware.dbexts import nsx_models LOG = logging.getLogger(__name__) -class LsnPort(models_v2.model_base.BASEV2): - - __tablename__ = 'lsn_port' - - lsn_port_id = Column(String(36), primary_key=True) - - lsn_id = Column(String(36), ForeignKey('lsn.lsn_id', ondelete="CASCADE"), - nullable=False) - sub_id = Column(String(36), nullable=False, unique=True) - mac_addr = Column(String(32), nullable=False, unique=True) - - def __init__(self, lsn_port_id, subnet_id, mac_address, lsn_id): - self.lsn_port_id = lsn_port_id - self.lsn_id = lsn_id - self.sub_id = subnet_id - self.mac_addr = mac_address - - -class Lsn(models_v2.model_base.BASEV2): - __tablename__ = 'lsn' - - lsn_id = Column(String(36), primary_key=True) - net_id = Column(String(36), nullable=False) - - def __init__(self, net_id, lsn_id): - self.net_id = net_id - self.lsn_id = lsn_id - - def lsn_add(context, network_id, lsn_id): """Add Logical Service Node information to persistent datastore.""" with context.session.begin(subtransactions=True): - lsn = Lsn(network_id, lsn_id) + lsn = nsx_models.Lsn(network_id, lsn_id) context.session.add(lsn) def lsn_remove(context, lsn_id): """Remove Logical Service Node information from datastore given its id.""" with context.session.begin(subtransactions=True): - context.session.query(Lsn).filter_by(lsn_id=lsn_id).delete() + context.session.query(nsx_models.Lsn).filter_by(lsn_id=lsn_id).delete() def lsn_remove_for_network(context, network_id): """Remove information about the Logical Service Node given its network.""" with context.session.begin(subtransactions=True): - context.session.query(Lsn).filter_by(net_id=network_id).delete() + context.session.query(nsx_models.Lsn).filter_by( + net_id=network_id).delete() def lsn_get_for_network(context, network_id, raise_on_err=True): """Retrieve LSN information given its network id.""" - query = context.session.query(Lsn) + query = context.session.query(nsx_models.Lsn) try: return query.filter_by(net_id=network_id).one() except (orm.exc.NoResultFound, d_exc.DBError): @@ -95,7 +64,7 @@ def lsn_get_for_network(context, network_id, raise_on_err=True): def lsn_port_add_for_lsn(context, lsn_port_id, subnet_id, mac, lsn_id): """Add Logical Service Node Port information to persistent datastore.""" with context.session.begin(subtransactions=True): - lsn_port = LsnPort(lsn_port_id, subnet_id, mac, lsn_id) + lsn_port = nsx_models.LsnPort(lsn_port_id, subnet_id, mac, lsn_id) context.session.add(lsn_port) @@ -103,7 +72,7 @@ def lsn_port_get_for_subnet(context, subnet_id, raise_on_err=True): """Return Logical Service Node Port information given its subnet id.""" with context.session.begin(subtransactions=True): try: - return (context.session.query(LsnPort). + return (context.session.query(nsx_models.LsnPort). filter_by(sub_id=subnet_id).one()) except (orm.exc.NoResultFound, d_exc.DBError): if raise_on_err: @@ -116,7 +85,7 @@ def lsn_port_get_for_mac(context, mac_address, raise_on_err=True): """Return Logical Service Node Port information given its mac address.""" with context.session.begin(subtransactions=True): try: - return (context.session.query(LsnPort). + return (context.session.query(nsx_models.LsnPort). filter_by(mac_addr=mac_address).one()) except (orm.exc.NoResultFound, d_exc.DBError): if raise_on_err: @@ -128,5 +97,5 @@ def lsn_port_get_for_mac(context, mac_address, raise_on_err=True): def lsn_port_remove(context, lsn_port_id): """Remove Logical Service Node port from the given Logical Service Node.""" with context.session.begin(subtransactions=True): - (context.session.query(LsnPort). + (context.session.query(nsx_models.LsnPort). filter_by(lsn_port_id=lsn_port_id).delete()) diff --git a/neutron/plugins/vmware/dbexts/maclearning.py b/neutron/plugins/vmware/dbexts/maclearning.py index 6a5f73acd..6a85162e1 100644 --- a/neutron/plugins/vmware/dbexts/maclearning.py +++ b/neutron/plugins/vmware/dbexts/maclearning.py @@ -13,35 +13,17 @@ # under the License. # -import sqlalchemy as sa -from sqlalchemy import orm from sqlalchemy.orm import exc from neutron.api.v2 import attributes from neutron.db import db_base_plugin_v2 -from neutron.db import model_base -from neutron.db import models_v2 from neutron.openstack.common import log as logging +from neutron.plugins.vmware.dbexts import nsx_models from neutron.plugins.vmware.extensions import maclearning as mac LOG = logging.getLogger(__name__) -class MacLearningState(model_base.BASEV2): - - port_id = sa.Column(sa.String(36), - sa.ForeignKey('ports.id', ondelete="CASCADE"), - primary_key=True) - mac_learning_enabled = sa.Column(sa.Boolean(), nullable=False) - - # Add a relationship to the Port model using the backref attribute. - # This will instruct SQLAlchemy to eagerly load this association. - port = orm.relationship( - models_v2.Port, - backref=orm.backref("mac_learning_state", lazy='joined', - uselist=False, cascade='delete')) - - class MacLearningDbMixin(object): """Mixin class for mac learning.""" @@ -61,8 +43,9 @@ class MacLearningDbMixin(object): def _update_mac_learning_state(self, context, port_id, enabled): try: - query = self._model_query(context, MacLearningState) - state = query.filter(MacLearningState.port_id == port_id).one() + query = self._model_query(context, nsx_models.MacLearningState) + state = query.filter( + nsx_models.MacLearningState.port_id == port_id).one() state.update({mac.MAC_LEARNING: enabled}) except exc.NoResultFound: self._create_mac_learning_state(context, @@ -72,7 +55,8 @@ class MacLearningDbMixin(object): def _create_mac_learning_state(self, context, port): with context.session.begin(subtransactions=True): enabled = port[mac.MAC_LEARNING] - state = MacLearningState(port_id=port['id'], - mac_learning_enabled=enabled) + state = nsx_models.MacLearningState( + port_id=port['id'], + mac_learning_enabled=enabled) context.session.add(state) return self._make_mac_learning_state_dict(state) diff --git a/neutron/plugins/vmware/dbexts/nsx_models.py b/neutron/plugins/vmware/dbexts/nsx_models.py index c2e3bc687..6400b175d 100644 --- a/neutron/plugins/vmware/dbexts/nsx_models.py +++ b/neutron/plugins/vmware/dbexts/nsx_models.py @@ -86,3 +86,48 @@ class NetworkGateway(model_base.BASEV2, models_v2.HasId, backref='networkgateways', cascade='all,delete') network_connections = orm.relationship(NetworkConnection, lazy='joined') + + +class MacLearningState(model_base.BASEV2): + + port_id = sa.Column(sa.String(36), + sa.ForeignKey('ports.id', ondelete="CASCADE"), + primary_key=True) + mac_learning_enabled = sa.Column(sa.Boolean(), nullable=False) + + # Add a relationship to the Port model using the backref attribute. + # This will instruct SQLAlchemy to eagerly load this association. + port = orm.relationship( + models_v2.Port, + backref=orm.backref("mac_learning_state", lazy='joined', + uselist=False, cascade='delete')) + + +class LsnPort(models_v2.model_base.BASEV2): + + __tablename__ = 'lsn_port' + + lsn_port_id = sa.Column(sa.String(36), primary_key=True) + + lsn_id = sa.Column(sa.String(36), + sa.ForeignKey('lsn.lsn_id', ondelete="CASCADE"), + nullable=False) + sub_id = sa.Column(sa.String(36), nullable=False, unique=True) + mac_addr = sa.Column(sa.String(32), nullable=False, unique=True) + + def __init__(self, lsn_port_id, subnet_id, mac_address, lsn_id): + self.lsn_port_id = lsn_port_id + self.lsn_id = lsn_id + self.sub_id = subnet_id + self.mac_addr = mac_address + + +class Lsn(models_v2.model_base.BASEV2): + __tablename__ = 'lsn' + + lsn_id = sa.Column(sa.String(36), primary_key=True) + net_id = sa.Column(sa.String(36), nullable=False) + + def __init__(self, net_id, lsn_id): + self.net_id = net_id + self.lsn_id = lsn_id -- 2.45.2