Merge "NEC: Merge NEC plugin models into single module"
This commit is contained in:
commit
e49abc9729
@ -63,8 +63,6 @@ from neutron.plugins.ml2.drivers import type_vlan # noqa
|
||||
from neutron.plugins.ml2.drivers import type_vxlan # noqa
|
||||
from neutron.plugins.ml2 import models # noqa
|
||||
from neutron.plugins.nec.db import models as nec_models # noqa
|
||||
from neutron.plugins.nec.db import packetfilter as nec_packetfilter # noqa
|
||||
from neutron.plugins.nec.db import router # noqa
|
||||
from neutron.plugins.nuage import nuage_models # noqa
|
||||
from neutron.plugins.openvswitch import ovs_models_v2 # noqa
|
||||
from neutron.plugins.vmware.dbexts import nsx_models # noqa
|
||||
|
@ -15,6 +15,7 @@
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
from neutron.db import l3_db
|
||||
from neutron.db import model_base
|
||||
from neutron.db import models_v2
|
||||
|
||||
@ -66,3 +67,51 @@ class PortInfo(model_base.BASEV2):
|
||||
backref=orm.backref("portinfo",
|
||||
lazy='joined', uselist=False,
|
||||
cascade='delete'))
|
||||
|
||||
|
||||
class RouterProvider(models_v2.model_base.BASEV2):
|
||||
"""Represents a binding of router_id to provider."""
|
||||
provider = sa.Column(sa.String(255))
|
||||
router_id = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('routers.id', ondelete="CASCADE"),
|
||||
primary_key=True)
|
||||
|
||||
router = orm.relationship(l3_db.Router, uselist=False,
|
||||
backref=orm.backref('provider', uselist=False,
|
||||
lazy='joined',
|
||||
cascade='delete'))
|
||||
|
||||
|
||||
class PacketFilter(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant):
|
||||
"""Represents a packet filter."""
|
||||
name = sa.Column(sa.String(255))
|
||||
network_id = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('networks.id', ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
priority = sa.Column(sa.Integer, nullable=False)
|
||||
action = sa.Column(sa.String(16), nullable=False)
|
||||
# condition
|
||||
in_port = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('ports.id', ondelete="CASCADE"),
|
||||
nullable=True)
|
||||
src_mac = sa.Column(sa.String(32), nullable=False)
|
||||
dst_mac = sa.Column(sa.String(32), nullable=False)
|
||||
eth_type = sa.Column(sa.Integer, nullable=False)
|
||||
src_cidr = sa.Column(sa.String(64), nullable=False)
|
||||
dst_cidr = sa.Column(sa.String(64), nullable=False)
|
||||
protocol = sa.Column(sa.String(16), nullable=False)
|
||||
src_port = sa.Column(sa.Integer, nullable=False)
|
||||
dst_port = sa.Column(sa.Integer, nullable=False)
|
||||
# status
|
||||
admin_state_up = sa.Column(sa.Boolean(), nullable=False)
|
||||
status = sa.Column(sa.String(16), nullable=False)
|
||||
|
||||
network = orm.relationship(
|
||||
models_v2.Network,
|
||||
backref=orm.backref('packetfilters', lazy='joined', cascade='delete'),
|
||||
uselist=False)
|
||||
in_port_ref = orm.relationship(
|
||||
models_v2.Port,
|
||||
backref=orm.backref('packetfilters', lazy='joined', cascade='delete'),
|
||||
primaryjoin="Port.id==PacketFilter.in_port",
|
||||
uselist=False)
|
||||
|
@ -12,14 +12,10 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
from sqlalchemy.orm import exc as sa_exc
|
||||
from sqlalchemy import sql
|
||||
|
||||
from neutron.api.v2 import attributes
|
||||
from neutron.db import model_base
|
||||
from neutron.db import models_v2
|
||||
from neutron.openstack.common import uuidutils
|
||||
from neutron.plugins.nec.db import models as nmodels
|
||||
from neutron.plugins.nec.extensions import packetfilter as ext_pf
|
||||
@ -32,41 +28,6 @@ PF_STATUS_ERROR = 'ERROR'
|
||||
INT_FIELDS = ('eth_type', 'src_port', 'dst_port')
|
||||
|
||||
|
||||
class PacketFilter(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant):
|
||||
"""Represents a packet filter."""
|
||||
name = sa.Column(sa.String(255))
|
||||
network_id = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('networks.id', ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
priority = sa.Column(sa.Integer, nullable=False)
|
||||
action = sa.Column(sa.String(16), nullable=False)
|
||||
# condition
|
||||
in_port = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('ports.id', ondelete="CASCADE"),
|
||||
nullable=True)
|
||||
src_mac = sa.Column(sa.String(32), nullable=False)
|
||||
dst_mac = sa.Column(sa.String(32), nullable=False)
|
||||
eth_type = sa.Column(sa.Integer, nullable=False)
|
||||
src_cidr = sa.Column(sa.String(64), nullable=False)
|
||||
dst_cidr = sa.Column(sa.String(64), nullable=False)
|
||||
protocol = sa.Column(sa.String(16), nullable=False)
|
||||
src_port = sa.Column(sa.Integer, nullable=False)
|
||||
dst_port = sa.Column(sa.Integer, nullable=False)
|
||||
# status
|
||||
admin_state_up = sa.Column(sa.Boolean(), nullable=False)
|
||||
status = sa.Column(sa.String(16), nullable=False)
|
||||
|
||||
network = orm.relationship(
|
||||
models_v2.Network,
|
||||
backref=orm.backref('packetfilters', lazy='joined', cascade='delete'),
|
||||
uselist=False)
|
||||
in_port_ref = orm.relationship(
|
||||
models_v2.Port,
|
||||
backref=orm.backref('packetfilters', lazy='joined', cascade='delete'),
|
||||
primaryjoin="Port.id==PacketFilter.in_port",
|
||||
uselist=False)
|
||||
|
||||
|
||||
class PacketFilterDbMixin(object):
|
||||
|
||||
def _make_packet_filter_dict(self, pf_entry, fields=None):
|
||||
@ -92,7 +53,7 @@ class PacketFilterDbMixin(object):
|
||||
|
||||
def _get_packet_filter(self, context, id):
|
||||
try:
|
||||
pf_entry = self._get_by_id(context, PacketFilter, id)
|
||||
pf_entry = self._get_by_id(context, nmodels.PacketFilter, id)
|
||||
except sa_exc.NoResultFound:
|
||||
raise ext_pf.PacketFilterNotFound(id=id)
|
||||
return pf_entry
|
||||
@ -103,7 +64,7 @@ class PacketFilterDbMixin(object):
|
||||
|
||||
def get_packet_filters(self, context, filters=None, fields=None):
|
||||
return self._get_collection(context,
|
||||
PacketFilter,
|
||||
nmodels.PacketFilter,
|
||||
self._make_packet_filter_dict,
|
||||
filters=filters,
|
||||
fields=fields)
|
||||
@ -174,7 +135,7 @@ class PacketFilterDbMixin(object):
|
||||
self._set_eth_type_from_protocol(params)
|
||||
|
||||
with context.session.begin(subtransactions=True):
|
||||
pf_entry = PacketFilter(**params)
|
||||
pf_entry = nmodels.PacketFilter(**params)
|
||||
context.session.add(pf_entry)
|
||||
|
||||
return self._make_packet_filter_dict(pf_entry)
|
||||
@ -201,16 +162,19 @@ class PacketFilterDbMixin(object):
|
||||
It returns a list of tuple (neutron filter_id, OFC id).
|
||||
"""
|
||||
query = (context.session.query(nmodels.OFCFilterMapping)
|
||||
.join(PacketFilter,
|
||||
nmodels.OFCFilterMapping.neutron_id == PacketFilter.id)
|
||||
.filter(PacketFilter.admin_state_up == sql.true()))
|
||||
.join(nmodels.PacketFilter,
|
||||
nmodels.OFCFilterMapping.neutron_id
|
||||
== nmodels.PacketFilter.id)
|
||||
.filter(nmodels.PacketFilter.admin_state_up == sql.true()))
|
||||
|
||||
network_id = port['network_id']
|
||||
net_pf_query = (query.filter(PacketFilter.network_id == network_id)
|
||||
.filter(PacketFilter.in_port == sql.null()))
|
||||
net_pf_query = (query.filter(nmodels.PacketFilter.network_id
|
||||
== network_id)
|
||||
.filter(nmodels.PacketFilter.in_port == sql.null()))
|
||||
net_filters = [(pf['neutron_id'], pf['ofc_id']) for pf in net_pf_query]
|
||||
|
||||
port_pf_query = query.filter(PacketFilter.in_port == port['id'])
|
||||
port_pf_query = query.filter(nmodels.PacketFilter.in_port
|
||||
== port['id'])
|
||||
port_filters = [(pf['neutron_id'], pf['ofc_id'])
|
||||
for pf in port_pf_query]
|
||||
|
||||
|
@ -12,42 +12,27 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
from sqlalchemy.orm import exc as sa_exc
|
||||
|
||||
from neutron.db import l3_db
|
||||
from neutron.db import models_v2
|
||||
from neutron.openstack.common import log as logging
|
||||
from neutron.plugins.nec.db import models as nmodels
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RouterProvider(models_v2.model_base.BASEV2):
|
||||
"""Represents a binding of router_id to provider."""
|
||||
provider = sa.Column(sa.String(255))
|
||||
router_id = sa.Column(sa.String(36),
|
||||
sa.ForeignKey('routers.id', ondelete="CASCADE"),
|
||||
primary_key=True)
|
||||
|
||||
router = orm.relationship(l3_db.Router, uselist=False,
|
||||
backref=orm.backref('provider', uselist=False,
|
||||
lazy='joined',
|
||||
cascade='delete'))
|
||||
|
||||
|
||||
def _get_router_providers_query(query, provider=None, router_ids=None):
|
||||
if provider:
|
||||
query = query.filter_by(provider=provider)
|
||||
if router_ids:
|
||||
column = RouterProvider.router_id
|
||||
column = nmodels.RouterProvider.router_id
|
||||
query = query.filter(column.in_(router_ids))
|
||||
return query
|
||||
|
||||
|
||||
def get_router_providers(session, provider=None, router_ids=None):
|
||||
"""Retrieve a list of a pair of router ID and its provider."""
|
||||
query = session.query(RouterProvider)
|
||||
query = session.query(nmodels.RouterProvider)
|
||||
query = _get_router_providers_query(query, provider, router_ids)
|
||||
return [{'provider': router.provider, 'router_id': router.router_id}
|
||||
for router in query]
|
||||
@ -55,14 +40,14 @@ def get_router_providers(session, provider=None, router_ids=None):
|
||||
|
||||
def get_routers_by_provider(session, provider, router_ids=None):
|
||||
"""Retrieve a list of router IDs with the given provider."""
|
||||
query = session.query(RouterProvider.router_id)
|
||||
query = session.query(nmodels.RouterProvider.router_id)
|
||||
query = _get_router_providers_query(query, provider, router_ids)
|
||||
return [router[0] for router in query]
|
||||
|
||||
|
||||
def get_router_count_by_provider(session, provider, tenant_id=None):
|
||||
"""Return the number of routers with the given provider."""
|
||||
query = session.query(RouterProvider).filter_by(provider=provider)
|
||||
query = session.query(nmodels.RouterProvider).filter_by(provider=provider)
|
||||
if tenant_id:
|
||||
query = (query.join('router').
|
||||
filter(l3_db.Router.tenant_id == tenant_id))
|
||||
@ -72,7 +57,7 @@ def get_router_count_by_provider(session, provider, tenant_id=None):
|
||||
def get_provider_by_router(session, router_id):
|
||||
"""Retrieve a provider of the given router."""
|
||||
try:
|
||||
binding = (session.query(RouterProvider).
|
||||
binding = (session.query(nmodels.RouterProvider).
|
||||
filter_by(router_id=router_id).
|
||||
one())
|
||||
except sa_exc.NoResultFound:
|
||||
@ -85,6 +70,6 @@ def add_router_provider_binding(session, provider, router_id):
|
||||
LOG.debug("Add provider binding "
|
||||
"(router=%(router_id)s, provider=%(provider)s)",
|
||||
{'router_id': router_id, 'provider': provider})
|
||||
binding = RouterProvider(provider=provider, router_id=router_id)
|
||||
binding = nmodels.RouterProvider(provider=provider, router_id=router_id)
|
||||
session.add(binding)
|
||||
return binding
|
||||
|
Loading…
Reference in New Issue
Block a user