-
Notifications
You must be signed in to change notification settings - Fork 3
/
vtn.py
512 lines (393 loc) · 20.6 KB
/
vtn.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
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
'''
Copyright 2014-2016 PTIN
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 json
import logging
from requests import post, get
logging.getLogger("urllib3").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
class VtnException(Exception):
pass
class TenantNotCreated(Exception):
pass
class TenantNotFound(Exception):
pass
class BridgeAlreadyExists(VtnException):
pass
class BridgeNotCreated(VtnException):
pass
class BridgeNotFound(VtnException):
pass
class TerminalNotCreated(VtnException):
pass
class TerminalNotFound(VtnException):
pass
class InterfaceNotCreated(VtnException):
pass
class InterfaceNotFound(VtnException):
pass
class InterfaceAlreadyExists(VtnException):
pass
class MappingConflict(VtnException):
pass
class MappingFailed(VtnException):
pass
class ConditionNotCreated(VtnException):
pass
class RedirectionNotCreated(VtnException):
pass
class RedirectionNotDeleted(VtnException):
pass
def _headers():
return {'content-type': 'application/json'}
class VtnWrapper:
'''Wrappes the OpenDaylight Virtual Tenant plugin to handle
the WICMs needs'''
def __init__(self, host, port, username='admin', password='admin'):
self.host = host
self.port = port
self.auth = (username, password)
logger.debug(('OpenDaylight client created!'
'Adress "{}:{}".'
'Auth "{}:{}"').format(self.host, self.port, self.auth[0],
self.auth[1]))
self.condition_create()
def _vtenant_exists(self, tenant):
url_check = '{}:{}/restconf/operational/vtn:vtns/vtn/'
logger.debug(('Checking if tenant "{}" exists. url: "{}"').format(
tenant, url_check))
r = get(url_check, auth=self.auth)
if r.ok:
logger.debug('Found tenant "{}": {}'.format(tenant, r.json()))
else:
logger.debug('Not found tenant "{}": {}'.format(tenant, r.text))
return r.ok
def _vbrige_exists(self, tenant, vbridge):
if not self._tenant_exists(tenant):
logger.error(('Failed to check for vbridge "{}" in tenant "{}" '
'because the tenant does not exist').format(vbridge,
tenant))
raise TenantNotFound(tenant)
url_check = ('{}:{}/restconf/operational/vtn:vtns/vtn/{}/vbridge/{}'.
format(self.host, self.port, tenant, vbridge))
logger.debug(('Checking if a vbridge "{}" already exists for tenant "{}'
'". url: "{}"').format(vbridge, tenant, url_check))
r = get(url_check, auth=self.auth)
if r.ok:
logger.debug('Found vbridge "{}" for tenant "{}": {}'.
format(vbridge, tenant, r.json()))
else:
logger.debug('Not found vbridge "{}" for tenant "{}": {}'.
format(vbridge, tenant, r.text))
return r.ok
def _vinterface_exists(self, tenant, vbridge, vinterface):
if not self._vbrige_exists(tenant, vbridge):
logger.error(('Failed to check for vinterface "{}" in vbridge "{}" '
'for tenant "{}" because the bridge doesnot exist').
format(vinterface, vbridge, tenant))
raise BridgeNotFound(vbridge)
url_check = ('{}:{}/restconf/operational/vtn:vtns/vtn/{}/vbridge/{}/'
'vinterface/{}').format(self.host, self.port, tenant,
vbridge, vinterface)
logger.debug(('Checking if vinterface {} in vbridge "{}" exists for '
'tenant "{}". url: "{}"').format(vbridge, tenant,
url_check))
r = get(url_check, auth=self.auth)
if r.ok:
logger.debug(('Found vinterface "{}" in vbridge "{}" for tenant "{}'
'": {}').format(vinterface, vbridge, tenant,
r.json()))
else:
logger.debug(('Not found vinterface "{}" in vbridge "{}" for tenant'
' "{}": {}').format(vinterface, vbridge, tenant,
r.json()))
return r.ok
def _vtenant_create(self, tenant):
url = 'http://{}:{}/restconf/operations/vtn:update-vtn'.format(
self.host, self.port)
data = json.dumps({'input': {'tenant-name': tenant}})
logger.info('Creating tenant "{}"'.format(tenant))
logger.debug('Creating new tenant url:"{}" data:"{}"'.format(url, data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug('Success creating tenant "{}": {}'.format(tenant,
r.json()))
else:
logger.error('Failed to create tenant "{}": {}'.format(tenant,
r.text))
raise TenantNotCreated(tenant)
def _vbrige_delete(self, tenant, vbridge):
url = ('http://{}:{}/restconf/operations/vtn-vbridge:'
'remove-vbridge').format(self.host, self.port)
data = json.dumps({'input':
{'tenant-name': tenant,
'bridge-name': vbridge}})
logger.info('Deleting vbridge "{}" for tenant "{}"'.format(vbridge,
tenant))
logger.debug('Deleting vbridge url:"{}" data:"{}"'.format(url, data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug('Success deleting vbridge "{}" for "{}"'.format(
vbridge, tenant))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed to delete vbridge "{}" for tenant "{}":'
' tenant does not exist.').format(vbridge, tenant))
raise TenantNotFound(tenant)
else:
logger.error('Failed to delete vbridge "{}" for tenant "{}": {}'.
format(vbridge, tenant, r.text))
raise BridgeNotCreated(vbridge)
def _vbridge_create(self, tenant, vbridge, description=''):
url = ('http://{}:{}/restconf/operations/vtn-vbridge:'
'update-vbridge').format(self.host, self.port)
data = json.dumps({'input':
{'tenant-name': tenant,
'bridge-name': vbridge,
'description': description}})
logger.info('Creating vbridge "{}" for tenant "{}"'.format(vbridge,
tenant))
logger.debug('Creating new vbridge url:"{}" data:"{}"'.format(url,
data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug('Success creating vbridge "{}" for "{}": {}'.format(
vbridge, tenant, r.json()))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed to create vbridge "{}" for tenant "{}":'
' tenant does not exist.').format(vbridge, tenant))
raise TenantNotFound(tenant)
else:
logger.error('Failed to create vbridge "{}" for tenant "{}": {}'.
format(vbridge, tenant, r.text))
raise BridgeNotCreated(vbridge)
def _vinterface_create(self, tenant, vbridge, vinterface, vterminal=False):
url = ('http://{}:{}/restconf/operations/vtn-vinterface:'
'update-vinterface').format(self.host, self.port)
data = {'input': {'tenant-name': tenant,
'interface-name': vinterface}}
if vterminal:
data['input']['terminal-name'] = vbridge
else:
data['input']['bridge-name'] = vbridge
data = json.dumps(data)
logger.info('Creating vinterface "{}" in "{}" for tenant "{}"'.
format(vinterface, vbridge, tenant))
logger.debug('Creating new vinterface url:"{}" data:"{}"'.format(url,
data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug(('Success creating vinterface "{}" in "{}"'
' for tenant "{}": {}').format(vinterface, vbridge,
tenant, r.json()))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed creating vinterface "{}" in "{}"'
' for tenant "{}": vbridge not found').format(
vinterface, vbridge, tenant))
if vterminal:
raise TerminalNotFound(vbridge)
else:
raise BridgeNotFound(vbridge)
else:
logger.error(('Failed creating vinterface "{}" in vbridge "{}"'
' for tenant "{}": {}').format(vinterface, vbridge,
tenant, r.text))
raise InterfaceNotCreated(vinterface)
def _vinterface_map(self, tenant, vbridge, vinterface, interface,
vterminal=False):
url = ('http://{}:{}/restconf/operations/vtn-port-map:set-port-map'
.format(self.host, self.port))
data = {'input': {'tenant-name': tenant,
'interface-name': vinterface,
'node': interface[0],
'port-id': interface[1]}}
if vterminal:
data['input']['terminal-name'] = vbridge
else:
data['input']['bridge-name'] = vbridge
if len(interface) >= 3:
data['input']['vlan-id'] = interface[2]
data = json.dumps(data)
logger.info('Mapping vinterface "{}" in "{}" for tenant "{}"'.
format(vinterface, vbridge, tenant))
logger.debug('Mapping vinterface url:"{}" data:"{}"'.format(url, data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug(('Success mapping vinterface "{}" in "{}"'
' for tenant "{}": {}').format(vinterface, vbridge,
tenant, r.json()))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed mapping vinterface "{}" in "{}"'
' for tenant "{}": vinterface not found').format(
vinterface, vbridge, tenant))
raise InterfaceNotFound(vbridge)
elif r.status_code == 409: # CONFLICT
logger.error(('Failed mapping vinterface "{}" in "{}"'
' for tenant "{}" Conflict: {}').format(
vinterface, vbridge, tenant, r.text))
raise MappingConflict(vinterface)
else:
logger.error(('Failed creating vinterface "{}" in "{}"'
' for tenant "{}": {}').format(vinterface, vbridge,
tenant, r.text))
raise MappingFailed(vinterface)
def _vterminal_create(self, tenant, vterminal):
url = ('http://{}:{}/restconf/operations/vtn-vterminal:'
'update-vterminal').format(self.host, self.port)
data = json.dumps({'input':
{'tenant-name': tenant, 'terminal-name': vterminal}})
logger.info('Creating vterminal "{}" for tenant "{}"'.format(vterminal,
tenant))
logger.debug('Creating new vterminal url:"{}" data:"{}"'.format(url,
data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug('Success creating vterminal "{}" for "{}": {}'.format(
vterminal, tenant, r.json()))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed to create vterminal {} for tenant "{}":'
' tenant does not exist.').format(vterminal, tenant))
raise TenantNotFound(tenant)
else:
logger.error('Failed to create vterminal {} for tenant "{}": {}'.
format(vterminal, tenant, r.text))
raise TerminalNotCreated(vterminal)
def _redirect_create(self, tenant, from_vbridge, from_vinterface,
to_vbridge, to_vinterface):
url = ('http://{}:{}/restconf/operations/vtn-flow-filter:'
'set-flow-filter').format(self.host, self.port)
data = json.dumps({'input':
{'output': 'false',
'tenant-name': tenant,
'bridge-name': from_vbridge,
'interface-name': from_vinterface,
'vtn-flow-filter': [
{'condition': 'cond_1',
'index': 10,
'vtn-redirect-filter':
{'redirect-destination':
{'bridge-name': to_vbridge,
'interface-name': to_vinterface},
'output': 'true'}}]}})
logger.info('Creating redirection for tenant "{}" from {}@{} to {}@{}'
.format(tenant, from_vbridge, from_vinterface, to_vbridge,
to_vinterface))
logger.debug('Creating new redirection url:"{}" data:"{}"'.format(url,
data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug(('Success creating redirection for tenant "{}"'
'from {}@{} to {}@{}')
.format(tenant, from_vbridge, from_vinterface,
to_vbridge, to_vinterface))
else:
logger.error(('Failed creating redirection for tenant "{}"'
'from {}@{} to {}: {}')
.format(tenant, from_vbridge, from_vinterface,
to_vbridge, to_vinterface, r.text))
raise RedirectionNotCreated()
def nap_create(self, client_id, mkt_id, ce_interface, pe_interface):
self._vtenant_create(client_id)
self._vbridge_create(client_id, mkt_id + '_NAP')
self._vinterface_create(client_id, mkt_id + '_NAP', 'ce')
self._vinterface_map(client_id, mkt_id + '_NAP', 'ce', ce_interface)
self._vinterface_create(client_id, mkt_id + '_NAP', 'pe')
self._vinterface_map(client_id, mkt_id + '_NAP', 'pe', pe_interface)
def nap_delete(self, client_id, mkt_id):
self._vbrige_delete(client_id, mkt_id + '_NAP')
def chain_create(self, client_id, ns_instance_id, nap_mkt_id, ce_pe, pe_ce):
self._vbridge_create(client_id, ns_instance_id + '_NS')
ce_pe_path = [(nap_mkt_id + '_NAP', 'ce')]
pe_ce_path = [(nap_mkt_id + '_NAP', 'pe')]
try:
for i in range(0, len(ce_pe)):
self._vinterface_create(client_id, ns_instance_id + '_NS',
'ce_pe' + str(i))
self._vinterface_map(client_id, ns_instance_id + '_NS',
'ce_pe' + str(i), ce_pe[i])
ce_pe_path.append((ns_instance_id + '_NS', 'ce_pe' + str(i)))
for i in range(0, len(pe_ce)):
self._vinterface_create(client_id, ns_instance_id + '_NS',
'pe_ce' + str(i))
self._vinterface_map(client_id, ns_instance_id + '_NS',
'pe_ce' + str(i), pe_ce[i])
pe_ce_path.append((ns_instance_id + '_NS', 'pe_ce' + str(i)))
ce_pe_path.append((nap_mkt_id + '_NAP', 'pe'))
pe_ce_path.append((nap_mkt_id + '_NAP', 'ce'))
redirections = zip(ce_pe_path, ce_pe_path[1:])
for if0, if1 in redirections:
self._redirect_create(client_id, if0[0], if0[1], if1[0], if1[1])
redirections = zip(pe_ce_path, pe_ce_path[1:])
for if0, if1 in redirections:
self._redirect_create(client_id, if0[0], if0[1], if1[0], if1[1])
except Exception as ex:
self._vbrige_delete(client_id, ns_instance_id + '_NS')
self._redirect_delete(client_id, nap_mkt_id + '_NAP', 'ce')
self._redirect_delete(client_id, nap_mkt_id + '_NAP', 'pe')
raise ex
def _redirect_delete(self, tenant, vbridge, vinterface):
url = ('http://{}:{}/restconf/operations/vtn-flow-filter:'
'remove-flow-filter').format(self.host, self.port)
data = json.dumps({'input':
{'tenant-name': tenant,
'bridge-name': vbridge,
'interface-name': vinterface}})
logger.info('Deleting redirection "{}" at vbridge "{}" for tenant "{}"'
.format(vinterface, vbridge, tenant))
logger.debug('Deleting redirection at vbridge url:"{}" data:"{}"'
.format(url, data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug(('Success deleting redirection "{}" at vbridge "{}" '
'for "{}"').format(vinterface, vbridge, tenant))
elif r.status_code == 404: # NOT FOUND
logger.error(('Failed to delete redirection "{}" at vbridge "{}" '
'for tenant "{}": tenant does not exist.')
.format(vinterface, vbridge, tenant))
raise TenantNotFound(tenant)
else:
logger.error(('Failed to delete redirection "{}" at vbridge "{}" '
'for tenant "{}": {}')
.format(vinterface, vbridge, tenant, r.text))
raise RedirectionNotDeleted(vbridge)
def chain_delete(self, client_id, ns_instance_id, nap_mkt_id):
self._vbrige_delete(client_id, ns_instance_id + '_NS')
self._redirect_delete(client_id, nap_mkt_id + '_NAP', 'ce')
self._redirect_delete(client_id, nap_mkt_id + '_NAP', 'pe')
def nfvi_create(self, mkt_id, ce_atachment, pe_atachment):
self._vtenant_create('TeNOR')
description = json.dumps({'ce_atachment': ce_atachment,
'pe_atachment': pe_atachment,
'range_min': 100,
'range_max': 400})
self._vbridge_create('TeNOR', mkt_id, description=description)
def condition_create(self):
url = ('http://{}:{}/restconf/operations/vtn-flow-condition:'
'set-flow-condition').format(self.host, self.port)
data = json.dumps({'input': {'operation': 'SET',
'present': 'false',
'name': 'cond_1',
'vtn-flow-match': [{
'index': 1,
'vtn-ether-match': {
'ether-type': '0x0800'},
'vtn-inet-match': {}}]}})
logger.info('Creating flow condition for IP filtering')
logger.debug('Creating new condition url:"{}" data:"{}"'.format(url,
data))
r = post(url, data=data, auth=self.auth, headers=_headers())
if r.ok:
logger.debug('Success creating IP filter condition: {}'.format(
r.json()))
else:
logger.error('Failed to create IP filter condition: {}'.
format(r.text))
raise ConditionNotCreated('IP Filter')