From ad4489feab472e8b8ac191a2b896df7415e82150 Mon Sep 17 00:00:00 2001 From: Manu B Date: Thu, 15 Jul 2021 05:18:15 +0000 Subject: [PATCH] Support peer association to BGP speaker * This change covers the support for create, get and delete operations for peer_associations to bgp speaker. * It also covers the population of peer association DB. Signed-off-by: Manu B Change-Id: I3369eb6c7823c38c38ec63d08fe8e25cc415acbc --- neutron_dynamic_routing/db/bgp_db.py | 84 +++++++++++++++++++ .../xena/expand/738d0ae1984e_bgpaas_enh.py | 13 +++ .../extensions/bgp_associations.py | 4 + neutron_dynamic_routing/policies/__init__.py | 2 + .../policies/bgp_peer_association.py | 74 ++++++++++++++++ .../services/bgp/bgp_plugin.py | 29 +++++++ .../tests/unit/db/test_bgp_db.py | 66 ++++++++++++++- 7 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 neutron_dynamic_routing/policies/bgp_peer_association.py diff --git a/neutron_dynamic_routing/db/bgp_db.py b/neutron_dynamic_routing/db/bgp_db.py index 0a22e407..b501e266 100644 --- a/neutron_dynamic_routing/db/bgp_db.py +++ b/neutron_dynamic_routing/db/bgp_db.py @@ -90,6 +90,22 @@ class BgpSpeakerRouterAssociation(model_base.BASEV2, status = sa.Column(sa.String(16)) +class BgpSpeakerPeerAssociation(model_base.BASEV2, + model_base.HasId, + model_base.HasProject): + + """Represents an association between BGP speaker and BGP peer""" + + __tablename__ = 'bgp_speaker_peer_associations' + + bgp_speaker_id = sa.Column(sa.String(length=36), + sa.ForeignKey('bgp_speakers.id'), + nullable=False) + peer_id = sa.Column(sa.String(length=36), + sa.ForeignKey('bgp_peers.id'), nullable=False) + status = sa.Column(sa.String(16)) + + class BgpSpeakerNetworkBinding(model_base.BASEV2): """Represents a mapping between a network and BGP speaker""" @@ -134,6 +150,10 @@ class BgpSpeaker(model_base.BASEV2, BgpSpeakerRouterAssociation, backref='bgp_speaker_router_associations', lazy='joined') + peer_associations = orm.relationship( + BgpSpeakerPeerAssociation, + backref='bgp_speaker_peer_associations', + lazy='joined') ip_version = sa.Column(sa.Integer, nullable=False, autoincrement=False) @@ -296,6 +316,55 @@ class BgpDbMixin(object): return self._make_bgp_speaker_router_association_dict(router_assoc, fields=fields) + def create_bgp_speaker_peer_association(self, context, bgp_speaker_id, + peer_association): + assoc_info = peer_association[bgp_associations.PEER_ASSOCIATION] + peer_id = self._get_id_for(assoc_info, 'peer_id') + assoc_info['bgp_speaker_id'] = bgp_speaker_id + assoc_info['status'] = bgp_consts.STATUS_DOWN + assoc_info['tenant_id'] = context.tenant_id + 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, BgpPeer, peer_id) + except sa_exc.NoResultFound: + raise bgp_ext.BgpPeerNotFound(id=peer_id) + + res_keys = ['bgp_speaker_id', 'tenant_id', 'peer_id', 'status'] + res = dict((k, assoc_info[k]) for k in res_keys) + res['id'] = uuidutils.generate_uuid() + speaker_peer_assoc_db = BgpSpeakerPeerAssociation(**res) + context.session.add(speaker_peer_assoc_db) + return self._make_bgp_speaker_peer_association_dict( + speaker_peer_assoc_db) + + def delete_bgp_speaker_peer_association(self, context, id): + with db_api.CONTEXT_WRITER.using(context): + binding = self._get_bgp_speaker_peer_association(context, id) + context.session.delete(binding) + + def get_bgp_speaker_peer_associations(self, context, bgp_speaker_id, + fields=None, filters=None, + sorts=None, limit=None, + marker=None, page_reverse=False): + if not filters: + filters = {} + filters['bgp_speaker_id'] = [bgp_speaker_id] + return model_query.get_collection( + context, BgpSpeakerPeerAssociation, + self._make_bgp_speaker_peer_association_dict, + filters=filters, fields=fields, sorts=sorts, + limit=limit, page_reverse=page_reverse) + + def get_bgp_speaker_peer_association(self, context, id, fields=None): + peer_assoc = self._get_bgp_speaker_peer_association(context, id) + return self._make_bgp_speaker_peer_association_dict(peer_assoc, + fields=fields) + 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) @@ -501,10 +570,12 @@ class BgpDbMixin(object): peer_bindings = bgp_speaker['peers'] network_bindings = bgp_speaker['networks'] router_associations = bgp_speaker['router_associations'] + peer_associations = bgp_speaker['peer_associations'] res = dict((k, bgp_speaker[k]) for k in attrs) res['peers'] = [x.bgp_peer_id for x in peer_bindings] res['networks'] = [x.network_id for x in network_bindings] res['router_associations'] = [x.id for x in router_associations] + res['peer_associations'] = [x.id for x in peer_associations] return db_utils.resource_fields(res, fields) def _make_advertised_routes_dict(self, routes): @@ -537,6 +608,13 @@ class BgpDbMixin(object): except sa_exc.NoResultFound: raise bgp_asso_ext.BgpSpeakerRouterAssociationNotFound(id=id) + def _get_bgp_speaker_peer_association(self, context, id): + try: + return model_query.get_by_id(context, BgpSpeakerPeerAssociation, + id) + except sa_exc.NoResultFound: + raise bgp_asso_ext.BgpSpeakerPeerAssociationNotFound(id=id) + def _make_bgp_speaker_router_association_dict(self, router_association, fields=None): attrs = ['id', 'tenant_id', 'router_id', 'bgp_speaker_id', @@ -544,6 +622,12 @@ class BgpDbMixin(object): res = dict((k, router_association[k]) for k in attrs) return db_utils.resource_fields(res, fields) + def _make_bgp_speaker_peer_association_dict(self, peer_association, + fields=None): + attrs = ['id', 'tenant_id', 'peer_id', 'bgp_speaker_id', 'status'] + res = dict((k, peer_association[k]) for k in attrs) + return db_utils.resource_fields(res, fields) + def _make_bgp_peer_dict(self, bgp_peer, fields=None): attrs = ['tenant_id', 'id', 'name', 'peer_ip', 'remote_as', 'auth_type', 'password'] diff --git a/neutron_dynamic_routing/db/migration/alembic_migrations/versions/xena/expand/738d0ae1984e_bgpaas_enh.py b/neutron_dynamic_routing/db/migration/alembic_migrations/versions/xena/expand/738d0ae1984e_bgpaas_enh.py index b842ed79..7219cb4f 100644 --- a/neutron_dynamic_routing/db/migration/alembic_migrations/versions/xena/expand/738d0ae1984e_bgpaas_enh.py +++ b/neutron_dynamic_routing/db/migration/alembic_migrations/versions/xena/expand/738d0ae1984e_bgpaas_enh.py @@ -40,3 +40,16 @@ def upgrade(): sa.Column('advertise_extra_routes', sa.Boolean, nullable=False), sa.Column('status', sa.String(16)), ) + op.create_table( + 'bgp_speaker_peer_associations', + sa.Column('id', sa.String(length=constants.UUID_FIELD_SIZE), + nullable=False), + sa.Column('project_id', sa.String(length=255)), + sa.Column('bgp_speaker_id', sa.String(length=36), + sa.ForeignKey('bgp_speakers.id'), + nullable=False), + sa.Column('peer_id', sa.String(length=36), + sa.ForeignKey('bgp_peers.id'), + nullable=False), + sa.Column('status', sa.String(16)), + ) diff --git a/neutron_dynamic_routing/extensions/bgp_associations.py b/neutron_dynamic_routing/extensions/bgp_associations.py index d266a008..e8f5105e 100644 --- a/neutron_dynamic_routing/extensions/bgp_associations.py +++ b/neutron_dynamic_routing/extensions/bgp_associations.py @@ -68,3 +68,7 @@ class Bgp_associations(api_extensions.APIExtensionDescriptor): #Bgp Association Exceptions class BgpSpeakerRouterAssociationNotFound(n_exc.NotFound): message = _("BGP speaker Router Association %(id)s could not be found.") + + +class BgpSpeakerPeerAssociationNotFound(n_exc.NotFound): + message = _("BGP speaker Peer Association %(id)s could not be found.") diff --git a/neutron_dynamic_routing/policies/__init__.py b/neutron_dynamic_routing/policies/__init__.py index 4a92b75e..62ee9376 100644 --- a/neutron_dynamic_routing/policies/__init__.py +++ b/neutron_dynamic_routing/policies/__init__.py @@ -15,11 +15,13 @@ import itertools from neutron_dynamic_routing.policies import bgp_dragent from neutron_dynamic_routing.policies import bgp_peer from neutron_dynamic_routing.policies import bgp_speaker +from neutron_dynamic_routing.policies import bgp_peer_association def list_rules(): return itertools.chain( bgp_speaker.list_rules(), bgp_peer.list_rules(), + bgp_peer_association.list_rules(), bgp_dragent.list_rules(), ) diff --git a/neutron_dynamic_routing/policies/bgp_peer_association.py b/neutron_dynamic_routing/policies/bgp_peer_association.py new file mode 100644 index 00000000..6a07ed21 --- /dev/null +++ b/neutron_dynamic_routing/policies/bgp_peer_association.py @@ -0,0 +1,74 @@ +# 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. + +from oslo_policy import policy + +from neutron_dynamic_routing.policies import base + + +rules = [ + policy.DocumentedRuleDefault( + 'create_bgp_speaker_peer_association', + base.RULE_ADMIN_ONLY, + 'Create a BGP speaker Peer association', + [ + { + 'method': 'POST', + 'path': '/bgp-speakers/{bgp_speaker_id}/peer_associations', + }, + ] + ), + policy.DocumentedRuleDefault( + 'update_bgp_speaker_peer_association', + base.RULE_ADMIN_ONLY, + 'Update a BGP speaker Peer association', + [ + { + 'method': 'PUT', + 'path': '/bgp-speakers/{bgp_speaker_id}/peer_associations/' + '{peer_association_id}', + }, + ] + ), + policy.DocumentedRuleDefault( + 'delete_bgp_speaker_peer_association', + base.RULE_ADMIN_ONLY, + 'Delete a BGP speaker Peer association', + [ + { + 'method': 'DELETE', + 'path': '/bgp-speakers/{bgp_speaker_id}/peer_associations/' + '{peer_association_id}', + }, + ] + ), + policy.DocumentedRuleDefault( + 'get_bgp_speaker_peer_association', + base.RULE_ADMIN_ONLY, + 'Get BGP speaker Peer Association', + [ + { + 'method': 'GET', + 'path': '/bgp-speakers/{bgp_speaker_id}/peer_associations', + }, + { + 'method': 'GET', + 'path': '/bgp-speakers/{bgp_speaker_id}/peer_associations/' + '{peer_association_id}', + }, + ] + ), +] + + +def list_rules(): + return rules diff --git a/neutron_dynamic_routing/services/bgp/bgp_plugin.py b/neutron_dynamic_routing/services/bgp/bgp_plugin.py index 2037f9cc..84b5dd30 100644 --- a/neutron_dynamic_routing/services/bgp/bgp_plugin.py +++ b/neutron_dynamic_routing/services/bgp/bgp_plugin.py @@ -250,6 +250,35 @@ class BgpPlugin(service_base.ServicePluginBase, limit=limit, marker=marker, page_reverse=page_reverse) + def create_bgp_speaker_peer_association(self, context, bgp_speaker_id, + peer_association): + return super(BgpPlugin, self).create_bgp_speaker_peer_association( + context, bgp_speaker_id, + peer_association) + + def delete_bgp_speaker_peer_association(self, context, id, + bgp_speaker_id): + super(BgpPlugin, self).delete_bgp_speaker_peer_association( + context, id) + + def get_bgp_speaker_peer_associations( + self, context, bgp_speaker_id, + filters=None, fields=None, sorts=None, + limit=None, marker=None, + page_reverse=False): + return super(BgpPlugin, self).get_bgp_speaker_peer_associations( + context, bgp_speaker_id, + fields=fields, filters=filters, + sorts=sorts, limit=limit, + marker=marker, + page_reverse=page_reverse) + + def get_bgp_speaker_peer_association(self, context, id, bgp_speaker_id, + fields=None): + return super(BgpPlugin, self).get_bgp_speaker_peer_association( + context, id, + fields=fields) + def get_routes(self, context, bgp_speaker_id): return super(BgpPlugin, self).get_routes(context, bgp_speaker_id) diff --git a/neutron_dynamic_routing/tests/unit/db/test_bgp_db.py b/neutron_dynamic_routing/tests/unit/db/test_bgp_db.py index f8586c4f..346e8702 100644 --- a/neutron_dynamic_routing/tests/unit/db/test_bgp_db.py +++ b/neutron_dynamic_routing/tests/unit/db/test_bgp_db.py @@ -50,7 +50,9 @@ class BgpEntityCreationMixin(object): def bgp_speaker(self, ip_version, local_as, name='my-speaker', advertise_fip_host_routes=True, advertise_tenant_networks=True, - networks=None, peers=None, router_id=None): + networks=None, peers=None, router_id=None, + peer_assocs=None + ): data = {'ip_version': ip_version, ADVERTISE_FIPS_KEY: advertise_fip_host_routes, 'advertise_tenant_networks': advertise_tenant_networks, @@ -75,6 +77,12 @@ class BgpEntityCreationMixin(object): {'router_association': {'router_id': router_id, 'advertise_extra_routes': True}}) + if peer_assocs: + for peer_id in peer_assocs: + self.bgp_plugin.create_bgp_speaker_peer_association( + self.context, bgp_speaker_id, + {'peer_association': + {'peer_id': peer_id}}) yield self.bgp_plugin.get_bgp_speaker(self.context, bgp_speaker_id) @@ -553,6 +561,62 @@ class BgpTests(BgpEntityCreationMixin): self.context, speaker['router_associations'][0], speaker['id']) + def test_create_bgp_speaker_peer_association(self): + tenant_id = _uuid() + scope_data = {'tenant_id': tenant_id, 'ip_version': 4, + 'shared': True, 'name': 'bgp-scope'} + self.plugin.create_address_scope(self.context, + {'address_scope': scope_data}) + with self.bgp_speaker(scope_data['ip_version'], 1234) as speaker: + peer_data = {'peer_ip': '10.10.10.10', 'tenant_id': tenant_id, + 'remote_as': '1111', 'auth_type': 'md5', + 'password': 'my-secret', 'name': 'peer'} + peer = self.bgp_plugin.create_bgp_peer(self.context, + {'bgp_peer': peer_data}) + peer_assoc = self.bgp_plugin\ + .create_bgp_speaker_peer_association( + self.context, speaker['id'], + {bgp_assocs.PEER_ASSOCIATION: + {'peer_id': peer['id']}}) + new_speaker = self.bgp_plugin.get_bgp_speaker(self.context, + speaker['id']) + peer_binding = self.bgp_plugin\ + .get_bgp_speaker_peer_association( + self.context, peer_assoc['id'], + speaker['id']) + self.assertEqual(1, len(new_speaker['peer_associations'])) + self.assertIsNotNone(peer_binding['id']) + self.assertTrue( + peer_binding['id'] in new_speaker['peer_associations']) + self.assertEqual(peer_binding['bgp_speaker_id'], speaker['id']) + + def test_delete_bgp_speaker_peer_association(self): + tenant_id = _uuid() + scope_data = {'tenant_id': tenant_id, 'ip_version': 4, + 'shared': True, 'name': 'bgp-scope'} + self.plugin.create_address_scope(self.context, + {'address_scope': scope_data}) + peer_data = {'peer_ip': '10.10.10.10', 'tenant_id': tenant_id, + 'remote_as': '1111', 'auth_type': 'md5', + 'password': 'my-secret', 'name': 'peer'} + peer = self.bgp_plugin.create_bgp_peer(self.context, + {'bgp_peer': peer_data}) + + with self.bgp_speaker(scope_data['ip_version'], 1234, + peer_assocs=[peer['id']]) as speaker: + self.bgp_plugin.delete_bgp_speaker_peer_association( + self.context, + speaker['peer_associations'][0], + speaker['id']) + new_speaker = self.bgp_plugin.get_bgp_speaker(self.context, + speaker['id']) + self.assertEqual(0, len(new_speaker['peer_associations'])) + self.assertRaises( + bgp_asso_ext.BgpSpeakerPeerAssociationNotFound, + self.bgp_plugin.get_bgp_speaker_peer_association, + self.context, speaker['peer_associations'][0], + speaker['id']) + def test_create_bgp_peer_md5_auth_no_password(self): bgp_peer = {'bgp_peer': {'auth_type': 'md5', 'password': None}} self.assertRaises(bgp.InvalidBgpPeerMd5Authentication,