~ntt-pf-lab/nova/network-service

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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 NTT.
# 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.
"""
SQLAlchemy models for flat vlan network service data.
"""
from sqlalchemy import or_
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import joinedload, joinedload_all
from sqlalchemy import Column, Boolean, Integer, String
from sqlalchemy import ForeignKey
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base

from nova import exception
from nova.db import api as nova_db
from nova.db.sqlalchemy.models import NovaBase

BASE = declarative_base()

# ----------------------
# Data Transfer Objects.
# ----------------------

class EthernetCard(BASE, NovaBase):
    """Represents an ethernet card."""
    __tablename__ = 'ethernet_cards'
    id = Column(Integer, primary_key=True)
    mac_address = Column(String(255), nullable=False, unique=True)


class Network(BASE, NovaBase):
    """Represents a simple IP network."""
    __tablename__ = 'networks'
    id = Column(Integer, primary_key=True)
    cidr = Column(String(255))
    netmask = Column(String(255))
    bridge = Column(String(255))
    gateway = Column(String(255))
    broadcast = Column(String(255))
    dns = Column(String(255))
    vlan = Column(Integer)
    vpn_public_address = Column(String(255))
    vpn_public_port = Column(Integer)
    vpn_private_address = Column(String(255))
    dhcp_start = Column(String(255))
    cidr_v6 = Column(String(255), unique=True)
    gateway_v6 = Column(String(255))
    netmask_v6 = Column(String(255))
    label = Column(String(255))
    project_id = Column(String(255))
    host = Column(String(255))  # , ForeignKey('hosts.id'))


class FixedIp(BASE, NovaBase):
    """Represents a fixed ip for an instance."""
    __tablename__ = 'fixed_ips'
    id = Column(Integer, primary_key=True)
    address = Column(String(255))
    network_id = Column(Integer, ForeignKey('networks.id'), nullable=True)
    network = relationship(Network, backref=backref('fixed_ips'))
    ethernet_card_id = Column(Integer, ForeignKey('ethernet_cards.id'))
    ethernet_card = relationship(EthernetCard, backref=backref('fixed_ips'))
    allocated = Column(Boolean, default=False)
    leased = Column(Boolean, default=False)
    reserved = Column(Boolean, default=False)


class FloatingIp(BASE, NovaBase):
    """Represents a floating ip that dynamically forwards to a fixed ip."""
    __tablename__ = 'floating_ips'
    id = Column(Integer, primary_key=True)
    address = Column(String(255))
    fixed_ip_id = Column(Integer, ForeignKey('fixed_ips.id'), nullable=True)
    fixed_ip = relationship(FixedIp,
                            backref=backref('floating_ips'),
                            foreign_keys=fixed_ip_id,
                            primaryjoin='and_('
                                'FloatingIp.fixed_ip_id == FixedIp.id,'
                                'FloatingIp.deleted == False)')
    project_id = Column(String(255))
    host = Column(String(255))  # , ForeignKey('hosts.id'))


class DataAccess(object):
    """The base class to implement Data Access Objects.
    """

    def __init__(self, session):
        """Initialize this Data Access Object.

        :param session: The SQLAlchemy session to use as this Data
            Access Object's data source.
        """
        self._session = session


class EthernetCardDataAccess(DataAccess):
    """A Data Access Object to access ethernet cards.
    """

    ethernet_card_dto_class = EthernetCard

    def ethernet_card_get(self, id):
        result = self._session.query(self.ethernet_card_dto_class).\
            filter_by(deleted=False).\
            filter_by(id=id).\
            first()

        if not result:
            raise exception.NotFound(_("No ethernet_card with id %s") % id)

        return result

    def ethernet_card_get_all(self, read_deleted=False):
        return self._session.query(self.ethernet_card_dto_class).\
            filter_by(deleted=read_deleted).\
            all()

    def ethernet_card_create(self, values):
        ethernet_card_ref = self.ethernet_card_dto_class()
        ethernet_card_ref.update(values)
        ethernet_card_ref.save(session=self._session)
        return ethernet_card_ref

    def ethernet_card_update(self, ethernet_card_id, values):
        with self._session.begin():
            ethernet_card_ref = self.ethernet_card_get(ethernet_card_id)
            ethernet_card_ref.update(values)
            ethernet_card_ref.save(session=self._session)

    def ethernet_card_delete(self, id):
        with self._session.begin():
            ethernet_card_ref = self.ethernet_card_get(id)
            ethernet_card_ref.delete(session=self._session)


class NetworkDataAccess(DataAccess):
    """A Data Access Object to access networks.
    """

    network_dto_class = Network

    def network_get(self, id):
        result = self._session.query(self.network_dto_class).\
            filter_by(deleted=False).\
            filter_by(id=id).\
            first()

        if not result:
            raise exception.NotFound(_("No network with id %s") % id)

        return result

    def network_get_by_bridge(self, bridge):
        result = self._session.query(self.network_dto_class).\
            filter_by(bridge=bridge).\
            filter_by(deleted=False).\
            first()

        if not result:
            raise exception.NotFound(_('No network for bridge %s') % bridge)

        return result

    def network_get_by_ethernet_card(self, ethernet_card_id):
        rv = self._session.query(self.network_dto_class).\
            filter_by(deleted=False).\
            join(self.network_dto_class.fixed_ips).\
            filter_by(ethernet_card_id=ethernet_card_id).\
            filter_by(deleted=False).\
            first()
        if not rv:
            raise exception.NotFound(_('No network for ethernet card %s') %
                                     ethernet_card_id)
        return rv

    def host_get_networks(self, host):
        with self._session.begin():
            return self._session.query(self.network_dto_class).\
                filter_by(deleted=False).\
                filter_by(host=host).\
                all()

    def network_get_all(self, read_deleted=False):
        return self._session.query(self.network_dto_class).\
            filter_by(deleted=read_deleted).\
            all()

    def network_create(self, values):
        network_ref = self.network_dto_class()
        network_ref.update(values)
        network_ref.save(session=self._session)
        return network_ref

    def network_create_safe(self, values):
        try:
            return self.network_create(values)
        except IntegrityError:
            return None

    def network_update(self, network_id, values):
        with self._session.begin():
            network_ref = self.network_get(network_id)
            network_ref.update(values)
            network_ref.save(session=self._session)

    def network_delete(self, id):
        with self._session.begin():
            network_ref = self.network_get(id)
            network_ref.delete(session=self._session)

    def network_delete_safe(self, id):
        try:
            return self.network_delete(id)
        except IntegrityError:
            return None

class FixedIpDataAccess(DataAccess):
    """A Data Access Object to access fixed IPs.
    """

    fixed_ip_dto_class = FixedIp

    def fixed_ip_create(self, values):
        fixed_ip_ref = self.fixed_ip_dto_class()
        fixed_ip_ref.update(values)
        fixed_ip_ref.save(session=self._session)
        return fixed_ip_ref['address']

    def fixed_ip_update(self, fixed_ip_id, values):
        with self._session.begin():
            fixed_ip_ref = self.fixed_ip_get(fixed_ip_id)
            fixed_ip_ref.update(values)
            fixed_ip_ref.save(session=self._session)

    def fixed_ip_get(self, fixed_ip_id):
        result = self._session.query(self.fixed_ip_dto_class).\
            filter_by(deleted=False).\
            filter_by(id=fixed_ip_id).\
            first()

        if not result:
            raise exception.NotFound(_("No Fixed IP with id %s") % id)

        return result

    def fixed_ip_get_all(self):
        result = self._session.query(self.fixed_ip_dto_class).all()
        if not result:
            raise exception.NotFound(_('No fixed ips defined'))

        return result

    def network_get_associated_fixed_ips(self, network_id):
        return self._session.query(self.fixed_ip_dto_class).\
            options(joinedload_all('ethernet_card')).\
            filter_by(network_id=network_id).\
            filter(self.fixed_ip_dto_class.ethernet_card_id != None).\
            filter_by(deleted=False).\
            all()

    network_get_associated_ips = network_get_associated_fixed_ips

    def fixed_ip_get_by_address(self, address, read_deleted=False):
        result = self._session.query(self.fixed_ip_dto_class).\
            filter_by(address=address).\
            filter_by(deleted=read_deleted).\
            options(joinedload('network')).\
            options(joinedload('ethernet_card')).\
            first()
        if not result:
            raise exception.NotFound(_('No fixed ip for address %s') % address)

        return result

    def fixed_ip_disassociate(self, fixed_ip_id):
        with self._session.begin():
            fixed_ip_ref = self.fixed_ip_get(fixed_ip_id)
            fixed_ip_ref.ethernet_card = None
            fixed_ip_ref.save(session=self._session)

    def fixed_ip_get_by_ethernet_card(self, ethernet_card_id,
                                      read_deleted=False):
        result = self._session.query(self.fixed_ip_dto_class).\
            filter_by(ethernet_card_id=ethernet_card_id).\
            filter_by(deleted=read_deleted).\
            first()
        return result

    def fixed_ip_disassociate_all_by_timeout(self, host, time):
        inner_q = self._session.query(Network.id).\
                            filter_by(host=host).\
                            subquery()
        result = self._session.query(self.fixed_ip_dto_class).\
                    filter(self.fixed_ip_dto_class.network_id.in_(inner_q)).\
                    filter(self.fixed_ip_dto_class.updated_at < time).\
                    filter(self.fixed_ip_dto_class.ethernet_card_id != None).\
                    filter_by(allocated=0).\
                    update({'ethernet_card_id': None,
                           'leased': 0}, synchronize_session='fetch')
        return result


class FloatingIpDataAccess(DataAccess):
    """A Data Access Object to access floating IPs.
    """

    floating_ip_dto_class = FloatingIp

    def floating_ip_count_by_project(self, project_id):
        return self._session.query(self.floating_ip_dto_class).\
            filter_by(project_id=project_id).\
            filter_by(deleted=False).\
            count()

    def floating_ip_allocate_address(self, host, project_id):
        with self._session.begin():
            floating_ip_ref = self._session.query(self.floating_ip_dto_class).\
                              filter_by(host=host).\
                              filter_by(fixed_ip_id=None).\
                              filter_by(project_id=None).\
                              filter_by(deleted=False).\
                              with_lockmode('update').\
                              first()
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
            #             then this has concurrency issues
            if not floating_ip_ref:
                raise nova_db.NoMoreAddresses()
            floating_ip_ref['project_id'] = project_id
            self._session.add(floating_ip_ref)
        return floating_ip_ref

    def floating_ip_deallocate(self, address):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                address, read_deleted=False)
            floating_ip_ref['project_id'] = None
            floating_ip_ref.save(session=self._session)

    def floating_ip_destroy(self, address):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                address, read_deleted=False)
            floating_ip_ref.delete(session=self._session)

    def floating_ip_disassociate_by_address(self, address):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                address, read_deleted=False)
            fixed_ip_ref = floating_ip_ref.fixed_ip
            if fixed_ip_ref:
                fixed_ip_address = fixed_ip_ref['address']
            else:
                fixed_ip_address = None
            floating_ip_ref.fixed_ip = None
            floating_ip_ref.save(session=self._session)
        return fixed_ip_address

    def floating_ip_get_all(self):
        return self._session.query(self.floating_ip_dto_class).\
            options(joinedload_all('fixed_ip.ethernet_card')).\
            filter_by(deleted=False).\
            all()

    def floating_ip_get_all_by_project(self, project_id):
        return self._session.query(self.floating_ip_dto_class).\
            options(joinedload_all('fixed_ip.ethernet_card')).\
            filter_by(project_id=project_id).\
            filter_by(deleted=False).\
            all()

    def floating_ip_get_by_address(self, address, read_deleted=False):
        result = self._session.query(self.floating_ip_dto_class).\
            options(joinedload_all('fixed_ip.network')).\
            filter_by(address=address).\
            filter_by(deleted=read_deleted).\
            first()
        if not result:
            raise exception.NotFound('No floating ip for address %s' % address)
        return result

    def floating_ip_update(self, address, values):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                address, read_deleted=False)
            for (key, value) in values.iteritems():
                floating_ip_ref[key] = value
            floating_ip_ref.save(session=self._session)

    def floating_ip_create(self, values):
        floating_ip_ref = self.floating_ip_dto_class()
        floating_ip_ref.update(values)
        floating_ip_ref.save(session=self._session)
        return floating_ip_ref

    def floating_ip_disassociate(self, address):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                address, read_deleted=False)
            fixed_ip_ref = floating_ip_ref.fixed_ip
            if fixed_ip_ref:
                fixed_ip_address = fixed_ip_ref['address']
            else:
                fixed_ip_address = None
            floating_ip_ref.fixed_ip = None
            floating_ip_ref.save(session=self._session)
        return fixed_ip_ref

    def floating_ip_get_all_by_host(self, host):
        return self._session.query(self.floating_ip_dto_class).\
                   options(joinedload_all('fixed_ip.ethernet_card')).\
                   filter_by(host=host).\
                   filter_by(deleted=False).\
                   all()


# ----------------------
# Data Access Objects.
# ----------------------

class FlatVlanNetworkDataAccess(
    EthernetCardDataAccess, NetworkDataAccess, FixedIpDataAccess,
    FloatingIpDataAccess):
    """A Data Access Object to access all data for the Flat VLAN network
    service.

    Also access fixed IP address associations.
    """

    ethernet_card_dto_class = EthernetCard

    network_dto_class = Network

    fixed_ip_dto_class = FixedIp

    floating_ip_dto_class = FloatingIp

    def network_get_by_project(self, project_id, associate=True):
        result = self._session.query(self.network_dto_class).\
            filter_by(project_id=project_id).\
            filter_by(deleted=False).\
            first()
        if not result:
            if not associate:
                return None
            try:
                return self.network_associate(project_id)
            except IntegrityError:
                # NOTE(vish): We hit this if there is a race and two
                #             processes are attempting to allocate the
                #             network at the same time
                result = self._session.query(self.network_dto_class).\
                    filter_by(project_id=project_id).\
                    filter_by(deleted=False).\
                    first()
        return result

    def network_get_by_cidr(self, cidr):
        result = self._session.query(self.network_dto_class).\
            filter_by(cidr=cidr).first()

        if not result:
            raise exception.NotFound(_('Network with cidr %s does not exist') %
                                     cidr)
        return result

    def network_associate(self, project_id):
        with self._session.begin():
            network_ref = self._session.query(self.network_dto_class).\
                filter_by(deleted=False).\
                filter_by(project_id=None).\
                with_lockmode('update').\
                first()
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
            #             then this has concurrency issues
            if not network_ref:
                raise nova_api.NoMoreNetworks()
            network_ref['project_id'] = project_id
            self._session.add(network_ref)
        return network_ref

    def fixed_ip_get_network(self, address, read_deleted=False):
        fixed_ip_ref = self.fixed_ip_get_by_address(
            address, read_deleted=read_deleted)
        return fixed_ip_ref.network

    def floating_ip_fixed_ip_associate(self, floating_address, fixed_address):
        with self._session.begin():
            floating_ip_ref = self.floating_ip_get_by_address(
                floating_address, read_deleted=False)
            fixed_ip_ref = self.fixed_ip_get_by_address(
                fixed_address, read_deleted=False)
            floating_ip_ref.fixed_ip = fixed_ip_ref
            floating_ip_ref.save(session=self._session)

    def fixed_ip_associate(self, fixed_ip_id, ethernet_card_id):
        with self._session.begin():
            ethernet_card = self.ethernet_card_get(ethernet_card_id)
            fixed_ip_ref = self._session.query(self.fixed_ip_dto_class).\
                                   filter_by(id=fixed_ip_id).\
                                   filter_by(deleted=False).\
                                   filter_by(ethernet_card=None).\
                                   with_lockmode('update').\
                                   first()
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
            #             then this has concurrency issues
            if not fixed_ip_ref:
                raise nova_db.NoMoreAddresses()
            fixed_ip_ref.ethernet_card = ethernet_card
            self._session.add(fixed_ip_ref)

    def fixed_ip_associate_pool(self, network_id, ethernet_card_id):
        with self._session.begin():
            network_or_none = or_(
                self.fixed_ip_dto_class.network_id == network_id,
                self.fixed_ip_dto_class.network_id == None)
            ip_ref = self._session.query(self.fixed_ip_dto_class).\
                filter(network_or_none).\
                filter_by(reserved=False).\
                filter_by(deleted=False).\
                filter_by(ethernet_card=None).\
                with_lockmode('update').\
                first()
            if not ip_ref:
                raise nova_db.NoMoreAddresses()
            if not ip_ref.network or not ip_ref.ethernet_card:
                ip_ref.network = self.network_get(network_id)
                ip_ref.ethernet_card = self.ethernet_card_get(ethernet_card_id)
                self._session.add(ip_ref)
            return ip_ref