/
db.py
424 lines (342 loc) · 14.5 KB
/
db.py
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
# Copyright (c) 2013 OpenStack Foundation
#
# 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.
import sqlalchemy as sa
from neutron import context as nctx
import neutron.db.api as db
from neutron.db import db_base_plugin_v2
from neutron.db import model_base
from neutron.db import models_v2
VLAN_SEGMENTATION = 'vlan'
UUID_LEN = 36
STR_LEN = 255
class AristaProvisionedNets(model_base.BASEV2, models_v2.HasId,
models_v2.HasTenant):
"""Stores networks provisioned on Arista EOS.
Saves the segmentation ID for each network that is provisioned
on EOS. This information is used during synchronization between
Neutron and EOS.
"""
__tablename__ = 'arista_provisioned_nets'
network_id = sa.Column(sa.String(UUID_LEN))
segmentation_id = sa.Column(sa.Integer)
def eos_network_representation(self, segmentation_type):
return {u'networkId': self.network_id,
u'segmentationTypeId': self.segmentation_id,
u'segmentationType': segmentation_type}
class AristaProvisionedVms(model_base.BASEV2, models_v2.HasId,
models_v2.HasTenant):
"""Stores VMs provisioned on Arista EOS.
All VMs launched on physical hosts connected to Arista
Switches are remembered
"""
__tablename__ = 'arista_provisioned_vms'
vm_id = sa.Column(sa.String(STR_LEN))
host_id = sa.Column(sa.String(STR_LEN))
port_id = sa.Column(sa.String(UUID_LEN))
network_id = sa.Column(sa.String(UUID_LEN))
def eos_vm_representation(self):
return {u'vmId': self.vm_id,
u'host': self.host_id,
u'ports': {self.port_id: [{u'portId': self.port_id,
u'networkId': self.network_id}]}}
def eos_port_representation(self):
return {u'vmId': self.vm_id,
u'host': self.host_id,
u'portId': self.port_id,
u'networkId': self.network_id}
class AristaProvisionedTenants(model_base.BASEV2, models_v2.HasId,
models_v2.HasTenant):
"""Stores Tenants provisioned on Arista EOS.
Tenants list is maintained for sync between Neutron and EOS.
"""
__tablename__ = 'arista_provisioned_tenants'
def eos_tenant_representation(self):
return {u'tenantId': self.tenant_id}
def remember_tenant(tenant_id):
"""Stores a tenant information in repository.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
tenant = (session.query(AristaProvisionedTenants).
filter_by(tenant_id=tenant_id).first())
if not tenant:
tenant = AristaProvisionedTenants(
tenant_id=tenant_id)
session.add(tenant)
def forget_tenant(tenant_id):
"""Removes a tenant information from repository.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
(session.query(AristaProvisionedTenants).
filter_by(tenant_id=tenant_id).
delete())
def get_all_tenants():
"""Returns a list of all tenants stored in repository."""
session = db.get_session()
with session.begin():
return session.query(AristaProvisionedTenants).all()
def num_provisioned_tenants():
"""Returns number of tenants stored in repository."""
session = db.get_session()
with session.begin():
return session.query(AristaProvisionedTenants).count()
def remember_vm(vm_id, host_id, port_id, network_id, tenant_id):
"""Stores all relevent information about a VM in repository.
:param vm_id: globally unique identifier for VM instance
:param host_id: ID of the host where the VM is placed
:param port_id: globally unique port ID that connects VM to network
:param network_id: globally unique neutron network identifier
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
vm = (session.query(AristaProvisionedVms).
filter_by(vm_id=vm_id, host_id=host_id,
port_id=port_id, tenant_id=tenant_id,
network_id=network_id).first())
if not vm:
vm = AristaProvisionedVms(
vm_id=vm_id,
host_id=host_id,
port_id=port_id,
network_id=network_id,
tenant_id=tenant_id)
session.add(vm)
def forget_vm(vm_id, host_id, port_id, network_id, tenant_id):
"""Removes all relevent information about a VM from repository.
:param vm_id: globally unique identifier for VM instance
:param host_id: ID of the host where the VM is placed
:param port_id: globally unique port ID that connects VM to network
:param network_id: globally unique neutron network identifier
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
(session.query(AristaProvisionedVms).
filter_by(vm_id=vm_id, host_id=host_id,
port_id=port_id, tenant_id=tenant_id,
network_id=network_id).delete())
def remember_network(tenant_id, network_id, segmentation_id):
"""Stores all relevent information about a Network in repository.
:param tenant_id: globally unique neutron tenant identifier
:param network_id: globally unique neutron network identifier
:param segmentation_id: VLAN ID that is assigned to the network
"""
session = db.get_session()
with session.begin():
net = (session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id,
network_id=network_id).first())
if not net:
net = AristaProvisionedNets(
tenant_id=tenant_id,
network_id=network_id,
segmentation_id=segmentation_id)
session.add(net)
def forget_network(tenant_id, network_id):
"""Deletes all relevent information about a Network from repository.
:param tenant_id: globally unique neutron tenant identifier
:param network_id: globally unique neutron network identifier
"""
session = db.get_session()
with session.begin():
(session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id, network_id=network_id).
delete())
def get_segmentation_id(tenant_id, network_id):
"""Returns Segmentation ID (VLAN) associated with a network.
:param tenant_id: globally unique neutron tenant identifier
:param network_id: globally unique neutron network identifier
"""
session = db.get_session()
with session.begin():
net = (session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id,
network_id=network_id).first())
return net and net.segmentation_id or None
def is_vm_provisioned(vm_id, host_id, port_id,
network_id, tenant_id):
"""Checks if a VM is already known to EOS
:returns: True, if yes; False otherwise.
:param vm_id: globally unique identifier for VM instance
:param host_id: ID of the host where the VM is placed
:param port_id: globally unique port ID that connects VM to network
:param network_id: globally unique neutron network identifier
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
num_vm = (session.query(AristaProvisionedVms).
filter_by(tenant_id=tenant_id,
vm_id=vm_id,
port_id=port_id,
network_id=network_id,
host_id=host_id).count())
return num_vm > 0
def is_network_provisioned(tenant_id, network_id, seg_id=None):
"""Checks if a networks is already known to EOS
:returns: True, if yes; False otherwise.
:param tenant_id: globally unique neutron tenant identifier
:param network_id: globally unique neutron network identifier
:param seg_id: Optionally matches the segmentation ID (VLAN)
"""
session = db.get_session()
with session.begin():
if not seg_id:
num_nets = (session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id,
network_id=network_id).count())
else:
num_nets = (session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id,
network_id=network_id,
segmentation_id=seg_id).count())
return num_nets > 0
def is_tenant_provisioned(tenant_id):
"""Checks if a tenant is already known to EOS
:returns: True, if yes; False otherwise.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
num_tenants = (session.query(AristaProvisionedTenants).
filter_by(tenant_id=tenant_id).count())
return num_tenants > 0
def num_nets_provisioned(tenant_id):
"""Returns number of networks for a given tennat.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
return (session.query(AristaProvisionedNets).
filter_by(tenant_id=tenant_id).count())
def num_vms_provisioned(tenant_id):
"""Returns number of VMs for a given tennat.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
return (session.query(AristaProvisionedVms).
filter_by(tenant_id=tenant_id).count())
def get_networks(tenant_id):
"""Returns all networks for a given tenant in EOS-compatible format.
See AristaRPCWrapper.get_network_list() for return value format.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
model = AristaProvisionedNets
# hack for pep8 E711: comparison to None should be
# 'if cond is not None'
none = None
all_nets = (session.query(model).
filter(model.tenant_id == tenant_id,
model.segmentation_id != none))
res = dict(
(net.network_id, net.eos_network_representation(
VLAN_SEGMENTATION))
for net in all_nets
)
return res
def get_vms(tenant_id):
"""Returns all VMs for a given tenant in EOS-compatible format.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
model = AristaProvisionedVms
# hack for pep8 E711: comparison to None should be
# 'if cond is not None'
none = None
all_vms = (session.query(model).
filter(model.tenant_id == tenant_id,
model.host_id != none,
model.vm_id != none,
model.network_id != none,
model.port_id != none))
res = dict(
(vm.vm_id, vm.eos_vm_representation())
for vm in all_vms
)
return res
def get_ports(tenant_id):
"""Returns all ports of VMs in EOS-compatible format.
:param tenant_id: globally unique neutron tenant identifier
"""
session = db.get_session()
with session.begin():
model = AristaProvisionedVms
# hack for pep8 E711: comparison to None should be
# 'if cond is not None'
none = None
all_ports = (session.query(model).
filter(model.tenant_id == tenant_id,
model.host_id != none,
model.vm_id != none,
model.network_id != none,
model.port_id != none))
res = dict(
(port.port_id, port.eos_port_representation())
for port in all_ports
)
return res
def get_tenants():
"""Returns list of all tenants in EOS-compatible format."""
session = db.get_session()
with session.begin():
model = AristaProvisionedTenants
all_tenants = session.query(model)
res = dict(
(tenant.tenant_id, tenant.eos_tenant_representation())
for tenant in all_tenants
)
return res
class NeutronNets(db_base_plugin_v2.NeutronDbPluginV2):
"""Access to Neutron DB.
Provides access to the Neutron Data bases for all provisioned
networks as well ports. This data is used during the synchronization
of DB between ML2 Mechanism Driver and Arista EOS
Names of the networks and ports are not stroed in Arista repository
They are pulled from Neutron DB.
"""
def __init__(self):
self.admin_ctx = nctx.get_admin_context()
def get_network_name(self, tenant_id, network_id):
network = self._get_network(tenant_id, network_id)
network_name = None
if network:
network_name = network[0]['name']
return network_name
def get_all_networks_for_tenant(self, tenant_id):
filters = {'tenant_id': [tenant_id]}
return super(NeutronNets,
self).get_networks(self.admin_ctx, filters=filters) or []
def get_all_ports_for_tenant(self, tenant_id):
filters = {'tenant_id': [tenant_id]}
return super(NeutronNets,
self).get_ports(self.admin_ctx, filters=filters) or []
def get_all_ports_for_vm(self, tenant_id, vm_id):
filters = {'tenant_id': [tenant_id],
'device_id': [vm_id]}
return super(NeutronNets,
self).get_ports(self.admin_ctx, filters=filters) or []
def _get_network(self, tenant_id, network_id):
filters = {'tenant_id': [tenant_id],
'id': [network_id]}
return super(NeutronNets,
self).get_networks(self.admin_ctx, filters=filters) or []