API changes to support router association to BGP speaker

Signed-off-by: Manu B <manubk2020@gmail.com>
Change-Id: I0116fb8e7977c9595cdde0ba6944bc5c1f25c466
This commit is contained in:
Manu B 2021-05-19 11:55:03 +00:00
parent 7245c228c2
commit bc67d11ce6
6 changed files with 294 additions and 1 deletions

View File

@ -69,6 +69,55 @@ class BgpSpeakerPeerBinding(model_base.BASEV2):
primary_key=True)
class BgpSpeakerAddressScopeBinding(model_base.BASEV2):
"""Represents a mapping between a address scope and router attached BGP
speaker"""
__tablename__ = 'bgp_speaker_address_scope_bindings'
bgp_speaker_id = sa.Column(sa.String(length=36),
sa.ForeignKey('bgp_speakers.id',
ondelete='CASCADE'),
nullable=False,
primary_key=True)
bgp_speaker_router_binding_id = sa.Column(
sa.String(length=36),
sa.ForeignKey('bgp_speaker_router_bindings.id',
ondelete='CASCADE'),
nullable=False,
primary_key=True)
address_scope_id = sa.Column(sa.String(length=36),
sa.ForeignKey('address_scopes.id',
ondelete='CASCADE'),
nullable=False)
class BgpSpeakerRouterBinding(model_base.BASEV2,
model_base.HasId):
"""Represents a mapping between a router and BGP speaker"""
__tablename__ = 'bgp_speaker_router_bindings'
bgp_speaker_id = sa.Column(sa.String(length=36),
sa.ForeignKey('bgp_speakers.id',
ondelete='CASCADE'),
nullable=False,
primary_key=True)
router_id = sa.Column(sa.String(length=36),
sa.ForeignKey('routers.id',
ondelete='CASCADE'),
nullable=False,
primary_key=True)
redistribute_static = sa.Column(sa.Boolean, nullable=False)
address_scopes = orm.relationship(
BgpSpeakerAddressScopeBinding,
backref='bgp_speaker_address_scope_bindings',
cascade='all, delete, delete-orphan',
lazy='joined')
class BgpSpeakerNetworkBinding(model_base.BASEV2):
"""Represents a mapping between a network and BGP speaker"""
@ -109,6 +158,10 @@ class BgpSpeaker(model_base.BASEV2,
backref='bgp_speaker_network_bindings',
cascade='all, delete, delete-orphan',
lazy='joined')
routers = orm.relationship(BgpSpeakerRouterBinding,
backref='bgp_speaker_router_bindings',
cascade='all, delete, delete-orphan',
lazy='joined')
ip_version = sa.Column(sa.Integer, nullable=False, autoincrement=False)
@ -221,6 +274,27 @@ class BgpDbMixin(object):
network_id)
return {'network_id': network_id}
def add_gateway_router(self, context, bgp_speaker_id, router_assoc_info):
router_id = self._get_id_for(router_assoc_info, 'router_id')
uuid = uuidutils.generate_uuid()
with db_api.CONTEXT_WRITER.using(context):
try:
self._save_bgp_speaker_router_binding(context, uuid,
bgp_speaker_id,
router_assoc_info)
except oslo_db_exc.DBDuplicateEntry:
raise bgp_ext.BgpSpeakerRouterBindingError(
router_id=router_id,
bgp_speaker_id=bgp_speaker_id)
return {'router_id': router_id}
def remove_gateway_router(self, context, bgp_speaker_id, router_info):
router_id = self._get_id_for(router_info, 'router_id')
with db_api.CONTEXT_WRITER.using(context):
self._remove_bgp_speaker_router_binding(context, bgp_speaker_id,
router_id)
return {'router_id': router_id}
def delete_bgp_speaker(self, context, bgp_speaker_id):
with db_api.CONTEXT_WRITER.using(context):
bgp_speaker_db = self._get_bgp_speaker(context, bgp_speaker_id)
@ -314,6 +388,10 @@ class BgpDbMixin(object):
routes = self.get_routes_by_bgp_speaker_id(context, bgp_speaker_id)
return self._make_advertised_routes_dict(routes)
def get_routes(self, context, bgp_speaker_id):
"""TODO: Add implementation to list advertised and learnt routes"""
pass
def _get_id_for(self, resource, id_name):
try:
uuid = resource[id_name]
@ -415,6 +493,76 @@ class BgpDbMixin(object):
bgp_speaker_id=bgp_speaker_id)
context.session.delete(binding)
def _save_bgp_speaker_router_binding(self, context, uuid,
bgp_speaker_id, router_assoc_info):
router_id = self._get_id_for(router_assoc_info, 'router_id')
red_static = router_assoc_info.get('redistribute_static', False)
with db_api.CONTEXT_WRITER.using(context):
try:
model_query.get_by_id(context, BgpSpeaker, bgp_speaker_id)
except sa_exc.NoResultFound:
raise bgp_ext.BgpSpeakerNotFound(id=bgp_speaker_id)
try:
model_query.get_by_id(context, l3_db.Router, router_id)
except sa_exc.NoResultFound:
raise l3_exc.RouterNotFound(router_id=router_id)
binding = BgpSpeakerRouterBinding(id=uuid,
bgp_speaker_id=bgp_speaker_id,
router_id=router_id,
redistribute_static=red_static)
context.session.add(binding)
self._save_address_scope_router_binding(context, uuid,
bgp_speaker_id,
router_assoc_info)
def _remove_bgp_speaker_router_binding(self, context,
bgp_speaker_id, router_id):
with db_api.CONTEXT_WRITER.using(context):
try:
binding = self._get_bgp_speaker_router_binding(context,
bgp_speaker_id,
router_id)
except sa_exc.NoResultFound:
raise bgp_ext.BgpSpeakerRouterNotAssociated(
router_id=router_id,
bgp_speaker_id=bgp_speaker_id)
context.session.delete(binding)
self._remove_address_scope_router_binding(context, bgp_speaker_id,
router_id, binding)
def _save_address_scope_router_binding(self, context, uuid,
bgp_speaker_id, router_assoc_info):
addr_scopes = router_assoc_info.get('address_scope_id', {})
if not addr_scopes:
addr_scopes = self._get_address_scopes_for_router_bound_speaker(
context,
bgp_speaker_id)
for address_scope in addr_scopes:
binding = BgpSpeakerAddressScopeBinding(
bgp_speaker_id=bgp_speaker_id,
bgp_speaker_router_binding_id=uuid,
address_scope_id=address_scope)
context.session.add(binding)
def _remove_address_scope_router_binding(self, context,
bgp_speaker_id, router_id,
router_assoc_info):
with db_api.CONTEXT_WRITER.using(context):
try:
address_bindings = self._get_bgp_speaker_address_scope_binding(
context,
bgp_speaker_id,
router_assoc_info.id)
except sa_exc.NoResultFound:
raise bgp_ext.BgpSpeakerRouterNotAssociated(
router_id=router_id,
bgp_speaker_id=bgp_speaker_id)
for address_binding in address_bindings:
context.session.delete(address_binding)
def _make_bgp_speaker_dict(self, bgp_speaker, fields=None):
attrs = {'id', 'local_as', 'tenant_id', 'name', 'ip_version',
'advertise_floating_ip_host_routes',
@ -449,6 +597,22 @@ class BgpDbMixin(object):
BgpSpeakerNetworkBinding.bgp_speaker_id == bgp_speaker_id,
BgpSpeakerNetworkBinding.network_id == network_id).one()
def _get_bgp_speaker_router_binding(self, context,
bgp_speaker_id, router_id):
query = model_query.query_with_hooks(context, BgpSpeakerRouterBinding)
return query.filter(
BgpSpeakerRouterBinding.bgp_speaker_id == bgp_speaker_id,
BgpSpeakerRouterBinding.router_id == router_id).one()
def _get_bgp_speaker_address_scope_binding(self, context,
bgp_speaker_id, id):
query = model_query.query_with_hooks(context,
BgpSpeakerAddressScopeBinding)
query = query.filter(
BgpSpeakerAddressScopeBinding.bgp_speaker_id == bgp_speaker_id,
BgpSpeakerAddressScopeBinding.bgp_speaker_router_binding_id == id)
return query.all()
def _make_bgp_peer_dict(self, bgp_peer, fields=None):
attrs = ['tenant_id', 'id', 'name', 'peer_ip', 'remote_as',
'auth_type', 'password']
@ -468,6 +632,21 @@ class BgpDbMixin(object):
models_v2.SubnetPool.address_scope_id == address_scope.id)
return [scope.id for scope in query.all()]
def _get_address_scopes_for_router_bound_speaker(self, context,
bgp_speaker_id):
with db_api.CONTEXT_READER.using(context):
binding = aliased(BgpSpeakerRouterBinding)
address_scope = aliased(address_scope_db.AddressScope)
query = context.session.query(address_scope)
query = query.filter(
binding.bgp_speaker_id == bgp_speaker_id,
l3_db.RouterPort.router_id == binding.router_id,
l3_db.RouterPort.port_id == models_v2.Port.id,
models_v2.Subnet.network_id == models_v2.Port.network_id,
models_v2.Subnet.subnetpool_id == models_v2.SubnetPool.id,
models_v2.SubnetPool.address_scope_id == address_scope.id)
return [scope.id for scope in query.all()]
def get_routes_by_bgp_speaker_id(self, context, bgp_speaker_id):
"""Get all routes that should be advertised by a BgpSpeaker."""
with db_api.CONTEXT_READER.using(context):

View File

@ -1 +1 @@
f399fa0f5f25
738d0ae1984e

View File

@ -0,0 +1,57 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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.
#
"""bgpaas enh
Revision ID: 738d0ae1984e
Revises: f399fa0f5f25
Create Date: 2021-05-21 10:25:57.492514
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '738d0ae1984e'
down_revision = 'f399fa0f5f25'
def upgrade():
op.create_table(
'bgp_speaker_router_bindings',
sa.Column('id', sa.String(length=36),
nullable=False, primary_key=True),
sa.Column('bgp_speaker_id', sa.String(length=36),
sa.ForeignKey('bgp_speakers.id', ondelete='CASCADE'),
nullable=False, primary_key=True),
sa.Column('router_id', sa.String(length=36),
sa.ForeignKey('routers.id', ondelete='CASCADE'),
nullable=False, primary_key=True),
sa.Column('redistribute_static', sa.Boolean, nullable=False),
)
op.create_table(
'bgp_speaker_address_scope_bindings',
sa.Column('bgp_speaker_id', sa.String(length=36),
sa.ForeignKey('bgp_speakers.id', ondelete='CASCADE'),
nullable=False, primary_key=True),
sa.Column('bgp_speaker_router_binding_id', sa.String(length=36),
sa.ForeignKey('bgp_speaker_router_bindings.id',
ondelete='CASCADE'),
nullable=False, primary_key=True),
sa.Column('address_scope_id', sa.String(length=36),
sa.ForeignKey('address_scopes.id', ondelete='CASCADE'),
nullable=False),
)

View File

@ -145,6 +145,16 @@ class BgpSpeakerNetworkBindingError(n_exc.Conflict):
"%(bgp_speaker_id)s.")
class BgpSpeakerRouterNotAssociated(n_exc.NotFound):
message = _("Router %(router_id)s is not associated with "
"BGP speaker %(bgp_speaker_id)s.")
class BgpSpeakerRouterBindingError(n_exc.Conflict):
message = _("Router %(router_id)s is already bound to BgpSpeaker "
"%(bgp_speaker_id)s.")
class NetworkNotBound(n_exc.NotFound):
message = _("Network %(network_id)s is not bound to a BgpSpeaker.")

View File

@ -109,6 +109,39 @@ rules = [
},
]
),
policy.DocumentedRuleDefault(
'add_gateway_router',
base.RULE_ADMIN_ONLY,
'Add a gateway router to a BGP speaker',
[
{
'method': 'PUT',
'path': '/bgp-speakers/{id}/add_gateway_router',
},
]
),
policy.DocumentedRuleDefault(
'remove_gateway_router',
base.RULE_ADMIN_ONLY,
'Remove a gateway router from a BGP speaker',
[
{
'method': 'PUT',
'path': '/bgp-speakers/{id}/remove_gateway_router',
},
]
),
policy.DocumentedRuleDefault(
'get_routes',
base.RULE_ADMIN_ONLY,
'Get advertised and learned routes of a BGP speaker',
[
{
'method': 'GET',
'path': '/bgp-speakers/{id}/get_routes',
},
]
),
policy.DocumentedRuleDefault(
'get_advertised_routes',
base.RULE_ADMIN_ONLY,

View File

@ -220,6 +220,20 @@ class BgpPlugin(service_base.ServicePluginBase,
bgp_speaker_id,
network_info)
def add_gateway_router(self, context, bgp_speaker_id, router_info):
return super(BgpPlugin, self).add_gateway_router(context,
bgp_speaker_id,
router_info)
def remove_gateway_router(self, context, bgp_speaker_id, router_info):
return super(BgpPlugin, self).remove_gateway_router(context,
bgp_speaker_id,
router_info)
def get_routes(self, context, bgp_speaker_id):
return super(BgpPlugin, self).get_routes(context,
bgp_speaker_id)
def get_advertised_routes(self, context, bgp_speaker_id):
return super(BgpPlugin, self).get_advertised_routes(context,
bgp_speaker_id)