blob: 11d7ca1c8c69f07859abd54f452638ef77bebd9a [file] [log] [blame]
Ales Komarekad46d2e2017-03-09 17:16:38 +01001#!/usr/bin/python
2# Copyright 2017 Mirantis, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from netaddr import IPNetwork
17
18try:
19 from vnc_api import vnc_api
Petr Jediný5f3efe32017-05-26 17:55:09 +020020 from vnc_api.vnc_api import LinklocalServiceEntryType, \
21 LinklocalServicesTypes, GlobalVrouterConfig
Ales Komarekad46d2e2017-03-09 17:16:38 +010022 from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \
23 ConfigNode, DatabaseNode, BgpRouter
24 from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \
25 BgpSession, BgpPeeringAttributes, BgpRouterParams
26 HAS_CONTRAIL = True
27except ImportError:
28 HAS_CONTRAIL = False
29
30__opts__ = {}
31
32
33def __virtual__():
34 '''
35 Only load this module if vnc_api library is installed.
36 '''
37 if HAS_CONTRAIL:
38 return 'contrail'
39
40 return False
41
42
43def _auth(**kwargs):
44 '''
45 Set up Contrail API credentials.
46 '''
47 user = kwargs.get('user')
48 password = kwargs.get('password')
49 tenant_name = kwargs.get('project')
50 api_host = kwargs.get('api_server_ip')
51 api_port = kwargs.get('api_server_port')
52 api_base_url = kwargs.get('api_base_url')
53 use_ssl = False
54 auth_host = kwargs.get('auth_host_ip')
55 vnc_lib = vnc_api.VncApi(user, password, tenant_name,
56 api_host, api_port, api_base_url, wait_for_connect=True,
57 api_server_use_ssl=use_ssl, auth_host=auth_host)
58
59 return vnc_lib
60
61
62def _get_config(vnc_client, global_system_config = 'default-global-system-config'):
63 try:
64 gsc_obj = vnc_client.global_system_config_read(id=global_system_config)
65 except vnc_api.NoIdError:
66 gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config)
67 except:
68 gsc_obj = None
69
70 return gsc_obj
71
72
73def _get_rt_inst_obj(vnc_client):
74
75 # TODO pick fqname hardcode from common
76 rt_inst_obj = vnc_client.routing_instance_read(
77 fq_name=['default-domain', 'default-project',
78 'ip-fabric', '__default__'])
79
80 return rt_inst_obj
81
82
83def _get_ip(ip_w_pfx):
84 return str(IPNetwork(ip_w_pfx).ip)
85
86
87
88def virtual_router_list(**kwargs):
89 '''
90 Return a list of all Contrail virtual routers
91
92 CLI Example:
93
94 .. code-block:: bash
95
96 salt '*' contrail.virtual_router_list
97 '''
98 ret = {}
99 vnc_client = _auth(**kwargs)
100 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
101 for vrouter_obj in vrouter_objs:
102 ret[vrouter_obj.name] = {
103 'ip_address': vrouter_obj.virtual_router_ip_address,
104 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled
105 }
106 return ret
107
108
109def virtual_router_get(name, **kwargs):
110 '''
111 Return a specific Contrail virtual router
112
113 CLI Example:
114
115 .. code-block:: bash
116
117 salt '*' contrail.virtual_router_get cmp01
118 '''
119 ret = {}
120 vrouter_objs = virtual_router_list(**kwargs)
121 if name in vrouter_objs:
122 ret[name] = vrouter_objs.get(name)
123 if len(ret) == 0:
124 return {'Error': 'Error in retrieving virtual router.'}
125 return ret
126
127
128def virtual_router_create(name, ip_address, dpdk_enabled=False, **kwargs):
129 '''
130 Create specific Contrail virtual router
131
132 CLI Example:
133
134 .. code-block:: bash
135
136 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
137 '''
138 ret = {}
139 vnc_client = _auth(**kwargs)
140 gsc_obj = _get_config(vnc_client)
141 vrouter_objs = virtual_router_list(**kwargs)
142 if name in vrouter_objs:
143 return {'Error': 'Virtual router %s already exists' % name}
144 else:
145 vrouter_obj = VirtualRouter(
146 name, gsc_obj,
147 virtual_router_ip_address=ip_address)
148 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
149 vnc_client.virtual_router_create(vrouter_obj)
150 ret = virtual_router_list(**kwargs)
151 return ret[name]
152
153
154def virtual_router_delete(name, **kwargs):
155 '''
156 Delete specific Contrail virtual router
157
158 CLI Example:
159
160 .. code-block:: bash
161
162 salt '*' contrail.virtual_router_delete cmp01
163 '''
164 vnc_client = _auth(**kwargs)
165 gsc_obj = _get_config(vnc_client)
166 vrouter_obj = VirtualRouter(name, gsc_obj)
167 vnc_client.virtual_router_delete(
168 fq_name=vrouter_obj.get_fq_name())
169
170
171def analytics_node_list(**kwargs):
172 '''
173 Return a list of all Contrail analytics nodes
174
175 CLI Example:
176
177 .. code-block:: bash
178
179 salt '*' contrail.analytics_node_list
180 '''
181 ret = {}
182 vnc_client = _auth(**kwargs)
183 node_objs = vnc_client._objects_list('analytics-node', detail=True)
184 for node_obj in node_objs:
185 ret[node_obj.name] = node_obj.__dict__
186 return ret
187
188
189def analytics_node_get(name, **kwargs):
190 '''
191 Return a specific Contrail analytics node
192
193 CLI Example:
194
195 .. code-block:: bash
196
197 salt '*' contrail.analytics_node_get nal01
198 '''
199 ret = {}
200 vrouter_objs = analytics_node_list(**kwargs)
201 if name in vrouter_objs:
202 ret[name] = vrouter_objs.get(name)
203 if len(ret) == 0:
204 return {'Error': 'Error in retrieving analytics node.'}
205 return ret
206
207
208def analytics_node_create(name, ip_address, **kwargs):
209 '''
210 Create specific Contrail analytics node
211
212 CLI Example:
213
214 .. code-block:: bash
215
216 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
217 '''
218 ret = {}
219 vnc_client = _auth(**kwargs)
220 gsc_obj = _get_config(vnc_client)
221 analytics_node_objs = analytics_node_list(**kwargs)
222 if name in analytics_node_objs:
223 return {'Error': 'Analytics node %s already exists' % name}
224 else:
225 analytics_node_obj = AnalyticsNode(
226 name, gsc_obj,
227 analytics_node_ip_address=ip_address)
228 vnc_client.analytics_node_create(analytics_node_obj)
229 ret = analytics_node_list(**kwargs)
230 return ret[name]
231
232
233def analytics_node_delete(name, **kwargs):
234 '''
235 Delete specific Contrail analytics node
236
237 CLI Example:
238
239 .. code-block:: bash
240
241 salt '*' contrail.analytics_node_delete cmp01
242 '''
243 vnc_client = _auth(**kwargs)
244 gsc_obj = _get_config(vnc_client)
245 analytics_node_obj = AnalyticsNode(name, gsc_obj)
246 vnc_client.analytics_node_delete(
247 fq_name=analytics_node_obj.get_fq_name())
248
249
250def config_node_list(**kwargs):
251 '''
252 Return a list of all Contrail config nodes
253
254 CLI Example:
255
256 .. code-block:: bash
257
258 salt '*' contrail.config_node_list
259 '''
260 ret = {}
261 vnc_client = _auth(**kwargs)
262 node_objs = vnc_client._objects_list('config-node', detail=True)
263 for node_obj in node_objs:
264 ret[node_obj.name] = node_obj.__dict__
265 return ret
266
267
268def config_node_get(name, **kwargs):
269 '''
270 Return a specific Contrail config node
271
272 CLI Example:
273
274 .. code-block:: bash
275
276 salt '*' contrail.config_node_get nal01
277 '''
278 ret = {}
279 vrouter_objs = config_node_list(**kwargs)
280 if name in vrouter_objs:
281 ret[name] = vrouter_objs.get(name)
282 if len(ret) == 0:
283 return {'Error': 'Error in retrieving config node.'}
284 return ret
285
286
287def config_node_create(name, ip_address, **kwargs):
288 '''
289 Create specific Contrail config node
290
291 CLI Example:
292
293 .. code-block:: bash
294
295 salt '*' contrail.config_node_create ntw03 10.10.10.103
296 '''
297 ret = {}
298 vnc_client = _auth(**kwargs)
299 gsc_obj = _get_config(vnc_client)
300 config_node_objs = config_node_list(**kwargs)
301 if name in config_node_objs:
302 return {'Error': 'Config node %s already exists' % name}
303 else:
304 config_node_obj = ConfigNode(
305 name, gsc_obj,
306 config_node_ip_address=ip_address)
307 vnc_client.config_node_create(config_node_obj)
308 ret = config_node_list(**kwargs)
309 return ret[name]
310
311
312def config_node_delete(name, **kwargs):
313 '''
314 Delete specific Contrail config node
315
316 CLI Example:
317
318 .. code-block:: bash
319
320 salt '*' contrail.config_node_delete cmp01
321 '''
322 vnc_client = _auth(**kwargs)
323 gsc_obj = _get_config(vnc_client)
324 config_node_obj = ConfigNode(name, gsc_obj)
325 vnc_client.config_node_delete(
326 fq_name=config_node_obj.get_fq_name())
327
328
329def bgp_router_list(**kwargs):
330 '''
331 Return a list of all Contrail BGP routers
332
333 CLI Example:
334
335 .. code-block:: bash
336
337 salt '*' contrail.bgp_router_list
338 '''
339 ret = {}
340 vnc_client = _auth(**kwargs)
341 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
342 for bgp_router_obj in bgp_router_objs:
343 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
344 return ret
345
346
347def bgp_router_get(name, **kwargs):
348 '''
349 Return a specific Contrail BGP router
350
351 CLI Example:
352
353 .. code-block:: bash
354
355 salt '*' contrail.bgp_router_get nal01
356 '''
357 ret = {}
358 bgp_router_objs = bgp_router_list(**kwargs)
359 if name in bgp_router_objs:
360 ret[name] = bgp_router_objs.get(name)
361 if len(ret) == 0:
362 return {'Error': 'Error in retrieving BGP router.'}
363 return ret
364
365
366def bgp_router_create(name, type, ip_address, asn=64512, **kwargs):
367 '''
368 Create specific Contrail control node
369
370 CLI Example:
371
372 .. code-block:: bash
373
374 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
375 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
376 '''
377 ret = {}
378 vnc_client = _auth(**kwargs)
379
380 bgp_router_objs = bgp_router_list(**kwargs)
381 if name in bgp_router_objs:
382 return {'Error': 'control node %s already exists' % name}
383 else:
384 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
385 'inet6-vpn']
386 if type != 'control-node':
387 address_families.remove('erm-vpn')
388
389 bgp_addr_fams = AddressFamilies(address_families)
390 bgp_sess_attrs = [
391 BgpSessionAttributes(address_families=bgp_addr_fams)]
392 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
393 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
394 rt_inst_obj = _get_rt_inst_obj(vnc_client)
395
396 if type == 'control-node':
397 vendor = 'contrail'
398 elif type == 'router':
399 vendor = 'mx'
400 else:
401 vendor = 'unknown'
402
403 router_params = BgpRouterParams(router_type=type,
404 vendor=vendor, autonomous_system=int(asn),
405 identifier=_get_ip(ip_address),
406 address=_get_ip(ip_address),
407 port=179, address_families=bgp_addr_fams)
408 bgp_router_obj = BgpRouter(name, rt_inst_obj,
409 bgp_router_parameters=router_params)
410 vnc_client.bgp_router_create(bgp_router_obj)
411 ret = bgp_router_list(**kwargs)
412 return ret[name]
413
414
415def bgp_router_delete(name, **kwargs):
416 '''
417 Delete specific Contrail control node
418
419 CLI Example:
420
421 .. code-block:: bash
422
423 salt '*' contrail.bgp_router_delete mx01
424 '''
425 vnc_client = _auth(**kwargs)
426 gsc_obj = _get_control(vnc_client)
427 bgp_router_obj = BgpRouter(name, gsc_obj)
428 vnc_client.bgp_router_delete(
429 fq_name=bgp_router_obj.get_fq_name())
430
431
432def database_node_list(**kwargs):
433 '''
434 Return a list of all Contrail database nodes
435
436 CLI Example:
437
438 .. code-block:: bash
439
440 salt '*' contrail.database_node_list
441 '''
442 ret = {}
443 vnc_client = _auth(**kwargs)
444 node_objs = vnc_client._objects_list('database-node', detail=True)
445 for node_obj in node_objs:
446 ret[node_obj.name] = node_obj.__dict__
447 return ret
448
449
450def database_node_get(name, **kwargs):
451 '''
452 Return a specific Contrail database node
453
454 CLI Example:
455
456 .. code-block:: bash
457
458 salt '*' contrail.database_node_get nal01
459 '''
460 ret = {}
461 vrouter_objs = database_node_list(**kwargs)
462 if name in vrouter_objs:
463 ret[name] = vrouter_objs.get(name)
464 if len(ret) == 0:
465 return {'Error': 'Error in retrieving database node.'}
466 return ret
467
468
469def database_node_create(name, ip_address, **kwargs):
470 '''
471 Create specific Contrail database node
472
473 CLI Example:
474
475 .. code-block:: bash
476
477 salt '*' contrail.database_node_create ntw03 10.10.10.103
478 '''
479 ret = {}
480 vnc_client = _auth(**kwargs)
481 gsc_obj = _get_config(vnc_client)
482 database_node_objs = database_node_list(**kwargs)
483 if name in database_node_objs:
484 return {'Error': 'Database node %s already exists' % name}
485 else:
486 database_node_obj = DatabaseNode(
487 name, gsc_obj,
488 database_node_ip_address=ip_address)
489 vnc_client.database_node_create(database_node_obj)
490 ret = database_node_list(**kwargs)
491 return ret[name]
492
493
494def database_node_delete(name, **kwargs):
495 '''
496 Delete specific Contrail database node
497
498 CLI Example:
499
500 .. code-block:: bash
501
502 salt '*' contrail.database_node_delete cmp01
503 '''
504 vnc_client = _auth(**kwargs)
505 gsc_obj = _get_database(vnc_client)
506 database_node_obj = databaseNode(name, gsc_obj)
507 vnc_client.database_node_delete(
508 fq_name=database_node_obj.get_fq_name())
Petr Jediný5f3efe32017-05-26 17:55:09 +0200509
510
511
512
513def _get_vrouter_config(vnc_client):
514 try:
515 config = vnc_client.global_vrouter_config_read(
516 fq_name=['default-global-system-config', 'default-global-vrouter-config'])
517 except Exception:
518 config = None
519
520 return config
521
522
523
524def linklocal_service_list(**kwargs):
525 '''
526 Return a list of all Contrail link local services
527
528 CLI Example:
529
530 .. code-block:: bash
531
532 salt '*' contrail.linklocal_service_list
533 '''
534 ret = {}
535 vnc_client = _auth(**kwargs)
536
537 current_config = _get_vrouter_config(vnc_client)
538 if current_config is None:
539 return ret
540
541 service_list_res = current_config.get_linklocal_services()
542 if service_list_res is None:
543 service_list_obj = {'linklocal_service_entry': []}
544 else:
545 service_list_obj = service_list_res.__dict__
546 for _, value in service_list_obj.iteritems():
547 for entry in value:
548 service = entry.__dict__
549 if 'linklocal_service_name' in service:
550 ret[service['linklocal_service_name']] = service
551 return ret
552
553
554def linklocal_service_get(name, **kwargs):
555 '''
556 Return a specific Contrail link local service
557
558 CLI Example:
559
560 .. code-block:: bash
561
562 salt '*' contrail.linklocal_service_get llservice
563 '''
564 ret = {}
565 services = linklocal_service_list(**kwargs)
566 if name in services:
567 ret[name] = services.get(name)
568 if len(ret) == 0:
569 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
570 return ret
571
572
573def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, **kwargs):
574 '''
575 Create specific Contrail link local service
576
577 CLI Example:
578
579 .. code-block:: bash
580
581 salt '*' contrail.linklocal_service_create \
582 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
583 salt '*' contrail.linklocal_service_create \
584 llservice 10.10.10.103 22 link-local.service.dns-name 22
585 '''
586 ret = {}
587 vnc_client = _auth(**kwargs)
588
589 current_config = _get_vrouter_config(vnc_client)
590
591 service_entry = LinklocalServiceEntryType(
592 linklocal_service_name=name,
593 linklocal_service_ip=lls_ip,
594 linklocal_service_port=lls_port,
595 ip_fabric_service_port=ipf_port)
596 if isinstance(ipf_dns_or_ip, basestring):
597 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
598 elif isinstance(ipf_dns_or_ip, list):
599 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
600 service_entry.ip_fabric_DNS_service_name = ''
601
602 if current_config is None:
603 new_services = LinklocalServicesTypes([service_entry])
604 new_config = GlobalVrouterConfig(linklocal_services=new_services)
605 vnc_client.global_vrouter_config_create(new_config)
606 else:
607 _current_service_list = current_config.get_linklocal_services()
608 if _current_service_list is None:
609 service_list = {'linklocal_service_entry': []}
610 else:
611 service_list = _current_service_list.__dict__
612 new_services = [service_entry]
613 for key, value in service_list.iteritems():
614 if key != 'linklocal_service_entry':
615 continue
616 for _entry in value:
617 entry = _entry.__dict__
618 if 'linklocal_service_name' in entry:
619 if entry['linklocal_service_name'] == name:
620 return {'Error': 'Link local service "{0}" already exists'.format(name)}
621 new_services.append(_entry)
622 service_list[key] = new_services
623 new_config = GlobalVrouterConfig(linklocal_services=service_list)
624 vnc_client.global_vrouter_config_update(new_config)
625 ret = linklocal_service_list(**kwargs)
626 return ret[name]
627
628
629def linklocal_service_delete(name, **kwargs):
630 '''
631 Delete specific link local service entry
632
633 CLI Example:
634
635 .. code-block:: bash
636
637 salt '*' contrail.linklocal_service_delete llservice
638 '''
639 vnc_client = _auth(**kwargs)
640
641 current_config = _get_vrouter_config(vnc_client)
642
643 found = False
644 if current_config is not None:
645 _current_service_list = current_config.get_linklocal_services()
646 if _current_service_list is None:
647 service_list = {'linklocal_service_entry': []}
648 else:
649 service_list = _current_service_list.__dict__
650 new_services = []
651 for key, value in service_list.iteritems():
652 if key != 'linklocal_service_entry':
653 continue
654 for _entry in value:
655 entry = _entry.__dict__
656 if 'linklocal_service_name' in entry:
657 if entry['linklocal_service_name'] == name:
658 found = True
659 else:
660 new_services.append(_entry)
661 service_list[key] = new_services
662 new_config = GlobalVrouterConfig(linklocal_services=service_list)
663 vnc_client.global_vrouter_config_update(new_config)
664 if not found:
665 return {'Error': 'Link local service "{0}" not found'.format(name)}