]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
NSX DB models split, part 2
authorSalvatore Orlando <salv.orlando@gmail.com>
Mon, 2 Feb 2015 17:30:02 +0000 (09:30 -0800)
committerSalvatore Orlando <salv.orlando@gmail.com>
Mon, 2 Feb 2015 17:49:08 +0000 (09:49 -0800)
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
neutron/plugins/vmware/dbexts/maclearning.py
neutron/plugins/vmware/dbexts/nsx_models.py

index a4865bdfc19dd5c1d95b7a2f95fb58cb0b39a1de..fd6019713419f5762ba91a49573780e53e385181 100644 (file)
 #
 
 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())
index 6a5f73acdf0b58b49ddebbfcd6177bc3e02cfba6..6a85162e1d321c6777bbb30a672ea383006d9460 100644 (file)
 #    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)
index c2e3bc687297df6e9374fa1e9582088cee118f19..6400b175d89e43359b73c694b11c1534aef26cf2 100644 (file)
@@ -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