File: bgpvpn.py

package info (click to toggle)
networking-bagpipe 23.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,872 kB
  • sloc: python: 15,677; sh: 927; makefile: 62
file content (420 lines) | stat: -rw-r--r-- 15,016 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#   Copyright (c) 2017 Orange.  # All Rights Reserved.
#
#    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_log import log as logging
from oslo_versionedobjects import fields as obj_fields

from networking_bgpvpn.neutron.db import bgpvpn_db

from neutron.api.rpc.callbacks import resources
from neutron.objects import base
from neutron.objects.ports import IPAllocation
from neutron.objects.ports import Port
from neutron.objects.router import RouterPort
from neutron.objects.subnet import Subnet

from neutron_lib.api.definitions import bgpvpn as bgpvpn_api
from neutron_lib.api.definitions import bgpvpn_routes_control as bgpvpn_rc_api
from neutron_lib import constants
from neutron_lib.objects import common_types
from neutron_lib.utils import net as net_utils


LOG = logging.getLogger(__name__)


def _get_gateway_mac_by_subnet(obj_context, subnet):
    if not subnet.gateway_ip:
        LOG.error("no gateway IP defined for subnet %s", subnet)
        return None

    ip_allocation = IPAllocation.get_object(obj_context,
                                            network_id=subnet.network_id,
                                            subnet_id=subnet.id,
                                            ip_address=subnet.gateway_ip)
    # pylint: disable=no-member
    if ip_allocation:
        port = Port.get_object(obj_context, id=ip_allocation.port_id)
        return str(port.mac_address)
    else:
        LOG.debug("no port allocated to gateway IP for subnet %s", subnet.id)
        return None


def _get_subnets_info(obj_context, net_id):
    subnets = Subnet.get_objects(obj_context, network_id=net_id)
    return [
        {'ip_version': subnet.ip_version,
         'id': subnet.id,
         'cidr': subnet.cidr,
         'gateway_ip': subnet.gateway_ip,
         'gateway_mac': _get_gateway_mac_by_subnet(obj_context, subnet)
         }
        for subnet in subnets
    ]


class BGPVPNTypeField(obj_fields.AutoTypedField):
    AUTO_TYPE = obj_fields.Enum(valid_values=bgpvpn_api.BGPVPN_TYPES)


@base.NeutronObjectRegistry.register
class BGPVPN(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPN
    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'type': BGPVPNTypeField(),
        'name': obj_fields.StringField(nullable=True,
                                       default=None),
        'route_targets': obj_fields.ListOfStringsField(nullable=True,
                                                       default=[]),
        'import_targets': obj_fields.ListOfStringsField(nullable=True,
                                                        default=[]),
        'export_targets': obj_fields.ListOfStringsField(nullable=True,
                                                        default=[]),
        'route_distinguishers': obj_fields.ListOfStringsField(nullable=True,
                                                              default=[]),
        'local_pref': obj_fields.IntegerField(nullable=True),
        'vni': obj_fields.IntegerField(nullable=True),
    }

    fields_no_update = ['id',
                        'project_id',
                        'type',
                        'port_id']

    foreign_keys = {'BGPVPNNetAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNRouterAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNPortAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNPortAssociationRoute': {'id': 'bgpvpn_id'},
                    }

    @classmethod
    def modify_fields_from_db(cls, db_obj):
        result = super().modify_fields_from_db(db_obj)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = (result[field].split(',')
                                 if result[field] else [])
        return result

    @classmethod
    def modify_fields_to_db(cls, fields):
        result = super().modify_fields_to_db(fields)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = ','.join(result.get(field, []))
        return result


@base.NeutronObjectRegistry.register
class BGPVPNNetAssociation(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPNNetAssociation

    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'bgpvpn_id': obj_fields.StringField(),
        'bgpvpn': obj_fields.ObjectField('BGPVPN'),
        'network_id': obj_fields.StringField(),
        'subnets': common_types.ListOfDictOfMiscValuesField(nullable=True)
    }

    fields_no_update = ['id',
                        'project_id',
                        'bgpvpn_id',
                        'network_id']

    synthetic_fields = ['bgpvpn',
                        'subnets']

    def __init__(self, context=None, **kwargs):
        super().__init__(context, **kwargs)

    def create(self):
        with self.db_context_writer(self.obj_context):
            super().create()
            self.obj_load_attr('subnets')

    def obj_load_attr(self, attrname):
        if attrname == 'subnets':
            self._load_subnets()
        else:
            super().obj_load_attr(attrname)

    def _load_subnets(self, db_obj=None):
        # pylint: disable=no-member
        subnets_info = _get_subnets_info(self.obj_context, self.network_id)
        setattr(self, 'subnets', subnets_info)
        self.obj_reset_changes(['subnets'])

    def from_db_object(self, obj):
        super().from_db_object(obj)
        self._load_subnets(obj)

    def all_subnets(self, network_id):
        # pylint: disable=no-member
        return self.subnets


@base.NeutronObjectRegistry.register
class BGPVPNRouterAssociation(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPNRouterAssociation

    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'bgpvpn_id': obj_fields.StringField(),
        'bgpvpn': obj_fields.ObjectField('BGPVPN'),
        'router_id': obj_fields.StringField(),
        'connected_networks':
            common_types.ListOfDictOfMiscValuesField(nullable=True)
    }

    fields_no_update = ['id',
                        'project_id',
                        'bgpvpn_id',
                        'router_id']

    synthetic_fields = ['bgpvpn',
                        'connected_networks']

    def __init__(self, context=None, **kwargs):
        super().__init__(context, **kwargs)

    def create(self):
        with self.db_context_writer(self.obj_context):
            super().create()
            self.obj_load_attr('connected_networks')

    def update(self):
        with self.db_context_writer(self.obj_context):
            if 'connected_networks' in self.obj_what_changed():
                self.obj_load_attr('connected_networks')
            super().update()

    def obj_load_attr(self, attrname):
        if attrname == 'connected_networks':
            return self._load_connected_networks()
        super().obj_load_attr(attrname)

    @classmethod
    def get_objects(cls, context, _pager=None, validate_filters=True,
                    **kwargs):
        if 'network_id' in kwargs and 'router_id' not in kwargs:
            ports = Port.get_objects(
                context,
                network_id=kwargs.pop('network_id'),
                device_owner=constants.DEVICE_OWNER_ROUTER_INTF)

            router_assocs = []
            for port in ports:
                # pylint: disable=no-member
                router_assocs.extend(
                    super().get_objects(
                        context, _pager=_pager,
                        validate_filters=validate_filters,
                        router_id=RouterPort.get_object(
                            context, port_id=port.id).router_id,
                        **kwargs)
                )
            return router_assocs

        return super().get_objects(
            context, _pager=_pager, validate_filters=validate_filters,
            **kwargs)

    # pylint: disable=no-member
    def _load_connected_networks(self, db_obj=None):
        # NOTE(tmorin): can be improved by directly looking up
        # Ports with device_id=self.router_id
        router_ports = RouterPort.get_objects(
            self.obj_context,
            router_id=self.router_id)
        connected_networks = []
        for router_port in router_ports:
            port = Port.get_object(self.obj_context,
                                   id=router_port.port_id)
            if port:
                # router gateway networks are not considered as requiring
                # to be bound to BGPVPNs
                if port.device_owner == constants.DEVICE_OWNER_ROUTER_GW:
                    LOG.debug("skipping port %s, because router gateway",
                              port.id)
                    continue
                connected_networks.append({
                    'network_id': port.network_id,
                    'subnets': _get_subnets_info(self.obj_context,
                                                 port.network_id)
                })
            else:
                LOG.warning("Couldn't find Port for RouterPort (router:%s,"
                            "port:%s)", router_port.router_id,
                            router_port.port_id)
        setattr(self, 'connected_networks', connected_networks)
        self.obj_reset_changes(['connected_networks'])

    def from_db_object(self, obj):
        super().from_db_object(obj)
        self._load_connected_networks(obj)

    def all_subnets(self, network_id):
        # pylint: disable=no-member
        for connected_net in self.connected_networks:
            if connected_net['network_id'] == network_id:
                return connected_net['subnets']
        return []


@base.NeutronObjectRegistry.register
class BGPVPNPortAssociation(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPNPortAssociation

    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'bgpvpn_id': obj_fields.StringField(),
        'bgpvpn': obj_fields.ObjectField('BGPVPN'),
        'port_id': obj_fields.StringField(),
        'subnets': common_types.ListOfDictOfMiscValuesField(nullable=True),
        'routes': obj_fields.ListOfObjectsField('BGPVPNPortAssociationRoute'),
        'advertise_fixed_ips': obj_fields.BooleanField(default=True)
    }

    fields_no_update = ['id',
                        'project_id',
                        'bgpvpn_id',
                        'port_id']

    synthetic_fields = ['bgpvpn',
                        'subnets',
                        'routes']

    def __init__(self, context=None, **kwargs):
        super().__init__(context, **kwargs)

    def create(self):
        with self.db_context_writer(self.obj_context):
            super().create()
            self.obj_load_attr('subnets')

    def obj_load_attr(self, attrname):
        if attrname == 'subnets':
            self._load_subnets()
        else:
            super().obj_load_attr(attrname)

    def _load_subnets(self, db_obj=None):
        # pylint: disable=no-member
        port = Port.get_object(self.obj_context, id=self.port_id)
        subnets_info = _get_subnets_info(self.obj_context, port.network_id)
        setattr(self, 'subnets', subnets_info)
        self.obj_reset_changes(['subnets'])

    def from_db_object(self, obj):
        super().from_db_object(obj)
        self._load_subnets(obj)

    def all_subnets(self, network_id):
        # pylint: disable=no-member
        return self.subnets


class BGPVPNPortAssociationRouteTypeField(obj_fields.AutoTypedField):
    AUTO_TYPE = obj_fields.Enum(valid_values=bgpvpn_rc_api.ROUTE_TYPES)


@base.NeutronObjectRegistry.register
class BGPVPNPortAssociationRoute(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPNPortAssociationRoute

    fields = {
        'id': common_types.UUIDField(),
        'port_association_id': common_types.UUIDField(),
        'type': BGPVPNPortAssociationRouteTypeField(),
        'prefix': common_types.IPNetworkField(nullable=True,
                                              default=None),
        'local_pref': obj_fields.IntegerField(nullable=True),
        'bgpvpn_id': obj_fields.StringField(nullable=True,
                                            default=None),
        'bgpvpn': obj_fields.ObjectField('BGPVPN',
                                         nullable=True,
                                         default=None),
    }

    fields_no_update = fields.keys()

    foreign_keys = {'BGPVPNPortAssociation': {'port_association_id': 'id'},
                    'BGPVPN': {'bgpvpn_id': 'id'},
                    }

    synthetic_fields = ['bgpvpn']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @classmethod
    def modify_fields_from_db(cls, db_obj):
        fields = super().modify_fields_from_db(db_obj)
        if 'prefix' in fields and fields['prefix'] is not None:
            fields['prefix'] = net_utils.AuthenticIPNetwork(fields['prefix'])

        return fields

    @classmethod
    def modify_fields_to_db(cls, fields):
        result = super().modify_fields_to_db(fields)
        if 'prefix' in result and result['prefix'] is not None:
            result['prefix'] = cls.filter_to_str(result['prefix'])

        return result

    # we use these objects in set() in bgpvpn agent extension

    def __eq__(self, other):
        # pylint: disable=no-member
        return ((self.type, self.prefix, self.bgpvpn_id) ==
                (other.type, other.prefix, other.bgpvpn_id))

    def __hash__(self):
        # pylint: disable=no-member
        return hash((self.type, self.prefix, self.bgpvpn_id))


resources.register_resource_class(BGPVPN)
resources.register_resource_class(BGPVPNNetAssociation)
resources.register_resource_class(BGPVPNRouterAssociation)
resources.register_resource_class(BGPVPNPortAssociation)