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

760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
# Copyright (c) 2011 NTT.
4
# All Rights Reserved.
5
#
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
#    not use this file except in compliance with the License. You may obtain
8
#    a copy of the License at
9
#
10
#         http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#    Unless required by applicable law or agreed to in writing, software
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
#    License for the specific language governing permissions and limitations
16
#    under the License.
17
"""
18
SQLAlchemy models for flat vlan network service data.
19
"""
20
from sqlalchemy import or_
21
from sqlalchemy.orm import relationship, backref
767.1.5 by Ryu Ishimoto
Added missing import joinedload_all
22
from sqlalchemy.orm import joinedload, joinedload_all
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
23
from sqlalchemy import Column, Boolean, Integer, String
24
from sqlalchemy import ForeignKey
767.1.20 by Ryu Ishimoto
Added a missing IntegrityError import for DB API.
25
from sqlalchemy.exc import IntegrityError
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
26
from sqlalchemy.ext.declarative import declarative_base
27
28
from nova import exception
767.1.17 by Ryu Ishimoto
Changed the firewall driver in libvirt_conn to flat_vlan driver. Changed the DB to include the ipv6 changes. Added ipv6_enabled flag in the network_info dictionary returend from net agent
29
from nova.db import api as nova_db
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
30
from nova.db.sqlalchemy.models import NovaBase
31
32
BASE = declarative_base()
33
34
# ----------------------
35
# Data Transfer Objects.
36
# ----------------------
37
38
class EthernetCard(BASE, NovaBase):
39
    """Represents an ethernet card."""
40
    __tablename__ = 'ethernet_cards'
41
    id = Column(Integer, primary_key=True)
42
    mac_address = Column(String(255), nullable=False, unique=True)
43
44
45
class Network(BASE, NovaBase):
46
    """Represents a simple IP network."""
47
    __tablename__ = 'networks'
48
    id = Column(Integer, primary_key=True)
49
    cidr = Column(String(255))
50
    netmask = Column(String(255))
51
    bridge = Column(String(255))
52
    gateway = Column(String(255))
53
    broadcast = Column(String(255))
54
    dns = Column(String(255))
55
    vlan = Column(Integer)
56
    vpn_public_address = Column(String(255))
57
    vpn_public_port = Column(Integer)
58
    vpn_private_address = Column(String(255))
59
    dhcp_start = Column(String(255))
60
    cidr_v6 = Column(String(255), unique=True)
767.1.17 by Ryu Ishimoto
Changed the firewall driver in libvirt_conn to flat_vlan driver. Changed the DB to include the ipv6 changes. Added ipv6_enabled flag in the network_info dictionary returend from net agent
61
    gateway_v6 = Column(String(255))
62
    netmask_v6 = Column(String(255))
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
63
    label = Column(String(255))
64
    project_id = Column(String(255))
65
    host = Column(String(255))  # , ForeignKey('hosts.id'))
66
67
68
class FixedIp(BASE, NovaBase):
69
    """Represents a fixed ip for an instance."""
70
    __tablename__ = 'fixed_ips'
71
    id = Column(Integer, primary_key=True)
72
    address = Column(String(255))
73
    network_id = Column(Integer, ForeignKey('networks.id'), nullable=True)
74
    network = relationship(Network, backref=backref('fixed_ips'))
75
    ethernet_card_id = Column(Integer, ForeignKey('ethernet_cards.id'))
76
    ethernet_card = relationship(EthernetCard, backref=backref('fixed_ips'))
77
    allocated = Column(Boolean, default=False)
78
    leased = Column(Boolean, default=False)
79
    reserved = Column(Boolean, default=False)
80
81
82
class FloatingIp(BASE, NovaBase):
83
    """Represents a floating ip that dynamically forwards to a fixed ip."""
84
    __tablename__ = 'floating_ips'
85
    id = Column(Integer, primary_key=True)
86
    address = Column(String(255))
87
    fixed_ip_id = Column(Integer, ForeignKey('fixed_ips.id'), nullable=True)
88
    fixed_ip = relationship(FixedIp,
89
                            backref=backref('floating_ips'),
90
                            foreign_keys=fixed_ip_id,
91
                            primaryjoin='and_('
92
                                'FloatingIp.fixed_ip_id == FixedIp.id,'
93
                                'FloatingIp.deleted == False)')
94
    project_id = Column(String(255))
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
95
    host = Column(String(255))  # , ForeignKey('hosts.id'))
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
96
97
98
class DataAccess(object):
99
    """The base class to implement Data Access Objects.
100
    """
101
767.2.2 by Romain Lenglet
Separate authorization and data access responsibilities in the flat_vlan network service.
102
    def __init__(self, session):
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
103
        """Initialize this Data Access Object.
104
105
        :param session: The SQLAlchemy session to use as this Data
106
            Access Object's data source.
107
        """
108
        self._session = session
109
110
111
class EthernetCardDataAccess(DataAccess):
112
    """A Data Access Object to access ethernet cards.
113
    """
114
115
    ethernet_card_dto_class = EthernetCard
116
117
    def ethernet_card_get(self, id):
118
        result = self._session.query(self.ethernet_card_dto_class).\
119
            filter_by(deleted=False).\
120
            filter_by(id=id).\
121
            first()
122
123
        if not result:
124
            raise exception.NotFound(_("No ethernet_card with id %s") % id)
125
126
        return result
127
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
128
    def ethernet_card_get_all(self, read_deleted=False):
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
129
        return self._session.query(self.ethernet_card_dto_class).\
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
130
            filter_by(deleted=read_deleted).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
131
            all()
132
133
    def ethernet_card_create(self, values):
134
        ethernet_card_ref = self.ethernet_card_dto_class()
135
        ethernet_card_ref.update(values)
136
        ethernet_card_ref.save(session=self._session)
137
        return ethernet_card_ref
138
139
    def ethernet_card_update(self, ethernet_card_id, values):
140
        with self._session.begin():
141
            ethernet_card_ref = self.ethernet_card_get(ethernet_card_id)
142
            ethernet_card_ref.update(values)
143
            ethernet_card_ref.save(session=self._session)
144
145
    def ethernet_card_delete(self, id):
146
        with self._session.begin():
147
            ethernet_card_ref = self.ethernet_card_get(id)
148
            ethernet_card_ref.delete(session=self._session)
149
150
151
class NetworkDataAccess(DataAccess):
152
    """A Data Access Object to access networks.
153
    """
154
155
    network_dto_class = Network
156
157
    def network_get(self, id):
158
        result = self._session.query(self.network_dto_class).\
159
            filter_by(deleted=False).\
160
            filter_by(id=id).\
161
            first()
162
163
        if not result:
164
            raise exception.NotFound(_("No network with id %s") % id)
165
166
        return result
167
168
    def network_get_by_bridge(self, bridge):
169
        result = self._session.query(self.network_dto_class).\
170
            filter_by(bridge=bridge).\
171
            filter_by(deleted=False).\
172
            first()
173
174
        if not result:
175
            raise exception.NotFound(_('No network for bridge %s') % bridge)
176
177
        return result
178
179
    def network_get_by_ethernet_card(self, ethernet_card_id):
180
        rv = self._session.query(self.network_dto_class).\
181
            filter_by(deleted=False).\
182
            join(self.network_dto_class.fixed_ips).\
183
            filter_by(ethernet_card_id=ethernet_card_id).\
184
            filter_by(deleted=False).\
185
            first()
186
        if not rv:
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
187
            raise exception.NotFound(_('No network for ethernet card %s') %
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
188
                                     ethernet_card_id)
189
        return rv
190
191
    def host_get_networks(self, host):
192
        with self._session.begin():
193
            return self._session.query(self.network_dto_class).\
194
                filter_by(deleted=False).\
195
                filter_by(host=host).\
196
                all()
197
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
198
    def network_get_all(self, read_deleted=False):
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
199
        return self._session.query(self.network_dto_class).\
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
200
            filter_by(deleted=read_deleted).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
201
            all()
202
203
    def network_create(self, values):
204
        network_ref = self.network_dto_class()
205
        network_ref.update(values)
206
        network_ref.save(session=self._session)
207
        return network_ref
208
209
    def network_create_safe(self, values):
210
        try:
211
            return self.network_create(values)
212
        except IntegrityError:
213
            return None
214
215
    def network_update(self, network_id, values):
216
        with self._session.begin():
217
            network_ref = self.network_get(network_id)
218
            network_ref.update(values)
219
            network_ref.save(session=self._session)
220
221
    def network_delete(self, id):
222
        with self._session.begin():
223
            network_ref = self.network_get(id)
224
            network_ref.delete(session=self._session)
225
767.1.33 by Ryu Ishimoto
Added network management client tool. Changed the networks REST API to be compatible with the current implementation
226
    def network_delete_safe(self, id):
227
        try:
228
            return self.network_delete(id)
229
        except IntegrityError:
230
            return None
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
231
232
class FixedIpDataAccess(DataAccess):
233
    """A Data Access Object to access fixed IPs.
234
    """
235
236
    fixed_ip_dto_class = FixedIp
237
238
    def fixed_ip_create(self, values):
239
        fixed_ip_ref = self.fixed_ip_dto_class()
240
        fixed_ip_ref.update(values)
241
        fixed_ip_ref.save(session=self._session)
242
        return fixed_ip_ref['address']
243
244
    def fixed_ip_update(self, fixed_ip_id, values):
245
        with self._session.begin():
246
            fixed_ip_ref = self.fixed_ip_get(fixed_ip_id)
247
            fixed_ip_ref.update(values)
248
            fixed_ip_ref.save(session=self._session)
249
250
    def fixed_ip_get(self, fixed_ip_id):
251
        result = self._session.query(self.fixed_ip_dto_class).\
252
            filter_by(deleted=False).\
253
            filter_by(id=fixed_ip_id).\
254
            first()
255
256
        if not result:
257
            raise exception.NotFound(_("No Fixed IP with id %s") % id)
258
259
        return result
260
261
    def fixed_ip_get_all(self):
262
        result = self._session.query(self.fixed_ip_dto_class).all()
263
        if not result:
264
            raise exception.NotFound(_('No fixed ips defined'))
265
266
        return result
267
268
    def network_get_associated_fixed_ips(self, network_id):
269
        return self._session.query(self.fixed_ip_dto_class).\
270
            options(joinedload_all('ethernet_card')).\
271
            filter_by(network_id=network_id).\
272
            filter(self.fixed_ip_dto_class.ethernet_card_id != None).\
273
            filter_by(deleted=False).\
274
            all()
275
276
    network_get_associated_ips = network_get_associated_fixed_ips
277
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
278
    def fixed_ip_get_by_address(self, address, read_deleted=False):
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
279
        result = self._session.query(self.fixed_ip_dto_class).\
280
            filter_by(address=address).\
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
281
            filter_by(deleted=read_deleted).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
282
            options(joinedload('network')).\
283
            options(joinedload('ethernet_card')).\
284
            first()
285
        if not result:
286
            raise exception.NotFound(_('No fixed ip for address %s') % address)
287
288
        return result
289
290
    def fixed_ip_disassociate(self, fixed_ip_id):
291
        with self._session.begin():
292
            fixed_ip_ref = self.fixed_ip_get(fixed_ip_id)
293
            fixed_ip_ref.ethernet_card = None
294
            fixed_ip_ref.save(session=self._session)
295
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
296
    def fixed_ip_get_by_ethernet_card(self, ethernet_card_id,
297
                                      read_deleted=False):
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
298
        result = self._session.query(self.fixed_ip_dto_class).\
299
            filter_by(ethernet_card_id=ethernet_card_id).\
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
300
            filter_by(deleted=read_deleted).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
301
            first()
302
        return result
303
767.1.6 by Ryu Ishimoto
Added fixed IP disassociation due to timeout logic
304
    def fixed_ip_disassociate_all_by_timeout(self, host, time):
305
        inner_q = self._session.query(Network.id).\
306
                            filter_by(host=host).\
307
                            subquery()
308
        result = self._session.query(self.fixed_ip_dto_class).\
309
                    filter(self.fixed_ip_dto_class.network_id.in_(inner_q)).\
310
                    filter(self.fixed_ip_dto_class.updated_at < time).\
311
                    filter(self.fixed_ip_dto_class.ethernet_card_id != None).\
312
                    filter_by(allocated=0).\
313
                    update({'ethernet_card_id': None,
314
                           'leased': 0}, synchronize_session='fetch')
315
        return result
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
316
767.1.17 by Ryu Ishimoto
Changed the firewall driver in libvirt_conn to flat_vlan driver. Changed the DB to include the ipv6 changes. Added ipv6_enabled flag in the network_info dictionary returend from net agent
317
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
318
class FloatingIpDataAccess(DataAccess):
319
    """A Data Access Object to access floating IPs.
320
    """
321
322
    floating_ip_dto_class = FloatingIp
323
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
324
    def floating_ip_count_by_project(self, project_id):
325
        return self._session.query(self.floating_ip_dto_class).\
326
            filter_by(project_id=project_id).\
327
            filter_by(deleted=False).\
328
            count()
329
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
330
    def floating_ip_allocate_address(self, host, project_id):
331
        with self._session.begin():
332
            floating_ip_ref = self._session.query(self.floating_ip_dto_class).\
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
333
                              filter_by(host=host).\
334
                              filter_by(fixed_ip_id=None).\
335
                              filter_by(project_id=None).\
336
                              filter_by(deleted=False).\
337
                              with_lockmode('update').\
338
                              first()
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
339
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
340
            #             then this has concurrency issues
341
            if not floating_ip_ref:
342
                raise nova_db.NoMoreAddresses()
343
            floating_ip_ref['project_id'] = project_id
344
            self._session.add(floating_ip_ref)
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
345
        return floating_ip_ref
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
346
347
    def floating_ip_deallocate(self, address):
348
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
349
            floating_ip_ref = self.floating_ip_get_by_address(
350
                address, read_deleted=False)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
351
            floating_ip_ref['project_id'] = None
352
            floating_ip_ref.save(session=self._session)
353
354
    def floating_ip_destroy(self, address):
355
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
356
            floating_ip_ref = self.floating_ip_get_by_address(
357
                address, read_deleted=False)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
358
            floating_ip_ref.delete(session=self._session)
359
360
    def floating_ip_disassociate_by_address(self, address):
361
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
362
            floating_ip_ref = self.floating_ip_get_by_address(
363
                address, read_deleted=False)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
364
            fixed_ip_ref = floating_ip_ref.fixed_ip
365
            if fixed_ip_ref:
366
                fixed_ip_address = fixed_ip_ref['address']
367
            else:
368
                fixed_ip_address = None
369
            floating_ip_ref.fixed_ip = None
370
            floating_ip_ref.save(session=self._session)
371
        return fixed_ip_address
372
373
    def floating_ip_get_all(self):
374
        return self._session.query(self.floating_ip_dto_class).\
375
            options(joinedload_all('fixed_ip.ethernet_card')).\
376
            filter_by(deleted=False).\
377
            all()
378
379
    def floating_ip_get_all_by_project(self, project_id):
380
        return self._session.query(self.floating_ip_dto_class).\
381
            options(joinedload_all('fixed_ip.ethernet_card')).\
382
            filter_by(project_id=project_id).\
383
            filter_by(deleted=False).\
384
            all()
385
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
386
    def floating_ip_get_by_address(self, address, read_deleted=False):
767.1.21 by Ryu Ishimoto
Added missing DB APIs, and added nova-net-flat-vlan-manage script for floating IP management
387
        result = self._session.query(self.floating_ip_dto_class).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
388
            options(joinedload_all('fixed_ip.network')).\
389
            filter_by(address=address).\
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
390
            filter_by(deleted=read_deleted).\
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
391
            first()
392
        if not result:
393
            raise exception.NotFound('No floating ip for address %s' % address)
394
        return result
395
396
    def floating_ip_update(self, address, values):
397
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
398
            floating_ip_ref = self.floating_ip_get_by_address(
399
                address, read_deleted=False)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
400
            for (key, value) in values.iteritems():
401
                floating_ip_ref[key] = value
402
            floating_ip_ref.save(session=self._session)
403
404
    def floating_ip_create(self, values):
405
        floating_ip_ref = self.floating_ip_dto_class()
406
        floating_ip_ref.update(values)
407
        floating_ip_ref.save(session=self._session)
408
        return floating_ip_ref
409
410
    def floating_ip_disassociate(self, address):
411
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
412
            floating_ip_ref = self.floating_ip_get_by_address(
413
                address, read_deleted=False)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
414
            fixed_ip_ref = floating_ip_ref.fixed_ip
415
            if fixed_ip_ref:
416
                fixed_ip_address = fixed_ip_ref['address']
417
            else:
418
                fixed_ip_address = None
419
            floating_ip_ref.fixed_ip = None
420
            floating_ip_ref.save(session=self._session)
767.1.24 by Ryu Ishimoto
Added ec2 associate/disassociate floating support
421
        return fixed_ip_ref
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
422
423
    def floating_ip_get_all_by_host(self, host):
424
        return self._session.query(self.floating_ip_dto_class).\
425
                   options(joinedload_all('fixed_ip.ethernet_card')).\
426
                   filter_by(host=host).\
427
                   filter_by(deleted=False).\
428
                   all()
429
430
431
# ----------------------
432
# Data Access Objects.
433
# ----------------------
434
435
class FlatVlanNetworkDataAccess(
436
    EthernetCardDataAccess, NetworkDataAccess, FixedIpDataAccess,
437
    FloatingIpDataAccess):
438
    """A Data Access Object to access all data for the Flat VLAN network
439
    service.
440
441
    Also access fixed IP address associations.
442
    """
443
444
    ethernet_card_dto_class = EthernetCard
445
446
    network_dto_class = Network
447
448
    fixed_ip_dto_class = FixedIp
449
450
    floating_ip_dto_class = FloatingIp
451
452
    def network_get_by_project(self, project_id, associate=True):
453
        result = self._session.query(self.network_dto_class).\
454
            filter_by(project_id=project_id).\
455
            filter_by(deleted=False).\
456
            first()
457
        if not result:
458
            if not associate:
459
                return None
460
            try:
461
                return self.network_associate(project_id)
462
            except IntegrityError:
463
                # NOTE(vish): We hit this if there is a race and two
464
                #             processes are attempting to allocate the
465
                #             network at the same time
466
                result = self._session.query(self.network_dto_class).\
467
                    filter_by(project_id=project_id).\
468
                    filter_by(deleted=False).\
469
                    first()
470
        return result
471
472
    def network_get_by_cidr(self, cidr):
473
        result = self._session.query(self.network_dto_class).\
474
            filter_by(cidr=cidr).first()
475
476
        if not result:
477
            raise exception.NotFound(_('Network with cidr %s does not exist') %
478
                                     cidr)
479
        return result
480
481
    def network_associate(self, project_id):
482
        with self._session.begin():
483
            network_ref = self._session.query(self.network_dto_class).\
484
                filter_by(deleted=False).\
485
                filter_by(project_id=None).\
486
                with_lockmode('update').\
487
                first()
488
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
489
            #             then this has concurrency issues
490
            if not network_ref:
491
                raise nova_api.NoMoreNetworks()
492
            network_ref['project_id'] = project_id
493
            self._session.add(network_ref)
494
        return network_ref
495
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
496
    def fixed_ip_get_network(self, address, read_deleted=False):
497
        fixed_ip_ref = self.fixed_ip_get_by_address(
498
            address, read_deleted=read_deleted)
760 by Hisaharu Ishii
Added DB migration/models/apis for flat_vlan, as well as FLAGS accessor to start merging the plugins into one
499
        return fixed_ip_ref.network
500
767.1.17 by Ryu Ishimoto
Changed the firewall driver in libvirt_conn to flat_vlan driver. Changed the DB to include the ipv6 changes. Added ipv6_enabled flag in the network_info dictionary returend from net agent
501
    def floating_ip_fixed_ip_associate(self, floating_address, fixed_address):
502
        with self._session.begin():
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
503
            floating_ip_ref = self.floating_ip_get_by_address(
504
                floating_address, read_deleted=False)
505
            fixed_ip_ref = self.fixed_ip_get_by_address(
506
                fixed_address, read_deleted=False)
767.1.17 by Ryu Ishimoto
Changed the firewall driver in libvirt_conn to flat_vlan driver. Changed the DB to include the ipv6 changes. Added ipv6_enabled flag in the network_info dictionary returend from net agent
507
            floating_ip_ref.fixed_ip = fixed_ip_ref
508
            floating_ip_ref.save(session=self._session)
509
510
    def fixed_ip_associate(self, fixed_ip_id, ethernet_card_id):
511
        with self._session.begin():
512
            ethernet_card = self.ethernet_card_get(ethernet_card_id)
513
            fixed_ip_ref = self._session.query(self.fixed_ip_dto_class).\
514
                                   filter_by(id=fixed_ip_id).\
515
                                   filter_by(deleted=False).\
516
                                   filter_by(ethernet_card=None).\
517
                                   with_lockmode('update').\
518
                                   first()
519
            # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
520
            #             then this has concurrency issues
521
            if not fixed_ip_ref:
522
                raise nova_db.NoMoreAddresses()
523
            fixed_ip_ref.ethernet_card = ethernet_card
524
            self._session.add(fixed_ip_ref)
525
526
    def fixed_ip_associate_pool(self, network_id, ethernet_card_id):
527
        with self._session.begin():
528
            network_or_none = or_(
529
                self.fixed_ip_dto_class.network_id == network_id,
530
                self.fixed_ip_dto_class.network_id == None)
531
            ip_ref = self._session.query(self.fixed_ip_dto_class).\
532
                filter(network_or_none).\
533
                filter_by(reserved=False).\
534
                filter_by(deleted=False).\
535
                filter_by(ethernet_card=None).\
536
                with_lockmode('update').\
537
                first()
538
            if not ip_ref:
539
                raise nova_db.NoMoreAddresses()
540
            if not ip_ref.network or not ip_ref.ethernet_card:
541
                ip_ref.network = self.network_get(network_id)
542
                ip_ref.ethernet_card = self.ethernet_card_get(ethernet_card_id)
543
                self._session.add(ip_ref)
767.2.1 by Romain Lenglet
Make the can_read_deleted parameter an explicit argument of every DAO method that requires it, in the flat_vlan db API.
544
            return ip_ref