blob: f03ee33577da20d7860a4ac2f5278e78316ebcac [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
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020017from vnc_api.vnc_api import PhysicalRouter, PhysicalInterface, LogicalInterface
18from vnc_api.vnc_api import EncapsulationPrioritiesType
Ales Komarekad46d2e2017-03-09 17:16:38 +010019
20try:
21 from vnc_api import vnc_api
Petr Jediný5f3efe32017-05-26 17:55:09 +020022 from vnc_api.vnc_api import LinklocalServiceEntryType, \
23 LinklocalServicesTypes, GlobalVrouterConfig
Ales Komarekad46d2e2017-03-09 17:16:38 +010024 from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \
25 ConfigNode, DatabaseNode, BgpRouter
26 from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \
27 BgpSession, BgpPeeringAttributes, BgpRouterParams
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020028
Ales Komarekad46d2e2017-03-09 17:16:38 +010029 HAS_CONTRAIL = True
30except ImportError:
31 HAS_CONTRAIL = False
32
33__opts__ = {}
34
35
36def __virtual__():
37 '''
38 Only load this module if vnc_api library is installed.
39 '''
40 if HAS_CONTRAIL:
41 return 'contrail'
42
43 return False
44
45
46def _auth(**kwargs):
47 '''
48 Set up Contrail API credentials.
49 '''
50 user = kwargs.get('user')
51 password = kwargs.get('password')
52 tenant_name = kwargs.get('project')
53 api_host = kwargs.get('api_server_ip')
54 api_port = kwargs.get('api_server_port')
55 api_base_url = kwargs.get('api_base_url')
56 use_ssl = False
57 auth_host = kwargs.get('auth_host_ip')
58 vnc_lib = vnc_api.VncApi(user, password, tenant_name,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020059 api_host, api_port, api_base_url, wait_for_connect=True,
60 api_server_use_ssl=use_ssl, auth_host=auth_host)
Ales Komarekad46d2e2017-03-09 17:16:38 +010061
62 return vnc_lib
63
64
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020065def _get_config(vnc_client, global_system_config='default-global-system-config'):
Ales Komarekad46d2e2017-03-09 17:16:38 +010066 try:
67 gsc_obj = vnc_client.global_system_config_read(id=global_system_config)
68 except vnc_api.NoIdError:
69 gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config)
70 except:
71 gsc_obj = None
72
73 return gsc_obj
74
75
76def _get_rt_inst_obj(vnc_client):
Ales Komarekad46d2e2017-03-09 17:16:38 +010077 # TODO pick fqname hardcode from common
78 rt_inst_obj = vnc_client.routing_instance_read(
79 fq_name=['default-domain', 'default-project',
80 'ip-fabric', '__default__'])
81
82 return rt_inst_obj
83
84
85def _get_ip(ip_w_pfx):
86 return str(IPNetwork(ip_w_pfx).ip)
87
88
Ales Komarekad46d2e2017-03-09 17:16:38 +010089def virtual_router_list(**kwargs):
90 '''
91 Return a list of all Contrail virtual routers
92
93 CLI Example:
94
95 .. code-block:: bash
96
97 salt '*' contrail.virtual_router_list
98 '''
99 ret = {}
100 vnc_client = _auth(**kwargs)
101 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
102 for vrouter_obj in vrouter_objs:
103 ret[vrouter_obj.name] = {
104 'ip_address': vrouter_obj.virtual_router_ip_address,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200105 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled,
106 'uuid': vrouter_obj.uuid
107
Ales Komarekad46d2e2017-03-09 17:16:38 +0100108 }
109 return ret
110
111
112def virtual_router_get(name, **kwargs):
113 '''
114 Return a specific Contrail virtual router
115
116 CLI Example:
117
118 .. code-block:: bash
119
120 salt '*' contrail.virtual_router_get cmp01
121 '''
122 ret = {}
123 vrouter_objs = virtual_router_list(**kwargs)
124 if name in vrouter_objs:
125 ret[name] = vrouter_objs.get(name)
126 if len(ret) == 0:
127 return {'Error': 'Error in retrieving virtual router.'}
128 return ret
129
130
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200131def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +0100132 '''
133 Create specific Contrail virtual router
134
135 CLI Example:
136
137 .. code-block:: bash
138
139 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200140 router_types:
141 - tor-agent
142 - tor-service-node
143 - embedded
Ales Komarekad46d2e2017-03-09 17:16:38 +0100144 '''
145 ret = {}
146 vnc_client = _auth(**kwargs)
147 gsc_obj = _get_config(vnc_client)
148 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200149 router_types = ['tor-agent', 'tor-service-node', 'embedded']
150 if router_type not in router_types:
151 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100152 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200153 vrouter = virtual_router_get(name)
154 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
155 changed = False
156 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
157 ret['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
158 vrouter_obj.set_virtual_router_ip_address(ip_address)
159 changed = True
160 if vrouter_obj.get_virtual_router_type() != router_type:
161 ret['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
162 vrouter_obj.set_virtual_router_type(router_type)
163 changed = True
164 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
165 ret['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
166 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
167 changed = True
168 if changed:
169 if __opts__['test']:
170 return "Virtual router " + name + " will be updated"
171 vnc_client.virtual_router_update(vrouter_obj)
172 return ret
173 return {'OK': 'Virtual router %s already exists and is updated' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100174 else:
175 vrouter_obj = VirtualRouter(
176 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200177 virtual_router_ip_address=ip_address,
178 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100179 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200180 if __opts__['test']:
181 return "Virtual router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100182 vnc_client.virtual_router_create(vrouter_obj)
183 ret = virtual_router_list(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200184 return "Create"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100185
186
187def virtual_router_delete(name, **kwargs):
188 '''
189 Delete specific Contrail virtual router
190
191 CLI Example:
192
193 .. code-block:: bash
194
195 salt '*' contrail.virtual_router_delete cmp01
196 '''
197 vnc_client = _auth(**kwargs)
198 gsc_obj = _get_config(vnc_client)
199 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200200 if __opts__['test']:
201 return "Virtual router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100202 vnc_client.virtual_router_delete(
203 fq_name=vrouter_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200204 return "Deleted"
205
206
207def physical_router_list(**kwargs):
208 '''
209 Return a list of all Contrail physical routers
210
211 CLI Example:
212
213 .. code-block:: bash
214
215 salt '*' contrail.physical_router_list
216 '''
217 ret = {}
218 vnc_client = _auth(**kwargs)
219 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
220 for prouter_obj in prouter_objs:
221 ret[prouter_obj.name] = {
222 'uuid': prouter_obj._uuid,
223 'management_ip': prouter_obj._physical_router_management_ip,
224 'product_name': prouter_obj._physical_router_product_name,
225 }
226
227 return ret
228
229
230def physical_router_get(name, **kwargs):
231 '''
232 Return a specific Contrail physical router
233
234 CLI Example:
235
236 .. code-block:: bash
237
238 salt '*' contrail.physical_router_get router_name
239 '''
240 ret = {}
241 vnc_client = _auth(**kwargs)
242 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
243 for prouter_obj in prouter_objs:
244 if name == prouter_obj.name:
245 ret[name] = prouter_obj.__dict__
246 if len(ret) == 0:
247 return {'Error': 'Error in retrieving physical router.'}
248 return ret
249
250
251def physical_router_create(name, parent_type=None,
252 management_ip=None,
253 dataplane_ip=None, # VTEP address in web GUI
254 vendor_name=None,
255 product_name=None,
256 vnc_managed=None,
257 junos_service_ports=None,
258 agents=None, **kwargs):
259 '''
260 Create specific Contrail physical router
261
262 CLI Example:
263
264 .. code-block:: bash
265
266 salt '*' contrail.physical_router_create OVSDB_router management_ip=10.167.4.202 dataplane_ip=172.16.20.15 vendor_name=MyVendor product_name=MyProduct agents="['tor01','tns01']"
267 '''
268 ret = {}
269 vnc_client = _auth(**kwargs)
270 gsc_obj = _get_config(vnc_client)
271 prouter_objs = physical_router_list(**kwargs)
272 if name in prouter_objs:
273 prouter = physical_router_get(name)
274 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
275 if prouter_obj.physical_router_management_ip != management_ip:
276 ret['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
277 prouter_obj.set_physical_router_management_ip(management_ip)
278 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
279 ret['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
280 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
281 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
282 ret['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
283 prouter_obj.set_physical_router_vendor_name(vendor_name)
284 if prouter_obj.get_physical_router_product_name() != product_name:
285 ret['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
286 prouter_obj.set_physical_router_product_name(product_name)
287 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
288 ret['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
289 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
290 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
291 ret['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
292 "to": junos_service_ports}
293 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
294
295 if __opts__['test']:
296 if len(ret) != 0:
297 return "Physical router " + name + " will be updated"
298 return {"OK": "Physical router exists and is updated"}
299
300 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
301 c_agents = [] # referenced vrouters
302 for c_agent in prouter_obj.get_virtual_router_refs():
303 c_agents.append(c_agent['uuid'])
304 agent_objs = [] # required state of references
305 for vrouter_obj in vrouter_objs:
306 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
307 prouter_obj.add_virtual_router(vrouter_obj)
308 ret['vrouter ' + vrouter_obj._display_name] = "Reference added"
309 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
310 prouter_obj.del_virtual_router(vrouter_obj)
311 ret['vrouter ' + vrouter_obj._display_name] = "Reference removed"
312 vnc_client.physical_router_update(prouter_obj)
313
314 if len(ret) == 0:
315 return {"OK": "Physical router exists and is updated"}
316 return ret
317 else:
318 if __opts__['test']:
319 return "Physical router " + name + " will be created"
320 prouter_obj = PhysicalRouter(
321 name=name,
322 parent_obj=None,
323 physical_router_management_ip=management_ip,
324 physical_router_dataplane_ip=dataplane_ip,
325 physical_router_vendor_name=vendor_name,
326 physical_router_product_name=product_name,
327 physical_router_vnc_managed=vnc_managed,
328 physical_router_junos_service_ports=junos_service_ports,
329 )
330 for agent in agents:
331 vrouter = virtual_router_get(agent)
332 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
333 prouter_obj.add_virtual_router(vrouter_obj)
334 vnc_client.physical_router_create(prouter_obj)
335 return "Created"
336
337
338def physical_router_delete(name, **kwargs):
339 '''
340 Delete specific Contrail physical router
341
342 CLI Example:
343
344 .. code-block:: bash
345
346 salt '*' contrail.physical_router_delete router_name
347 '''
348 vnc_client = _auth(**kwargs)
349 gsc_obj = _get_config(vnc_client)
350 prouter_obj = PhysicalRouter(name, gsc_obj)
351 if __opts__['test']:
352 return "Physical router " + name + " will be deleted"
353 vnc_client.physical_router_delete(
354 fq_name=prouter_obj.get_fq_name())
355 return "Deleted"
356
357
358def physical_interface_list(**kwargs):
359 '''
360 Return a list of all Contrail physical interface
361
362 CLI Example:
363
364 .. code-block:: bash
365
366 salt '*' contrail.physical_interface_list
367 '''
368 ret = {}
369 vnc_client = _auth(**kwargs)
370 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
371 for pinterface_obj in pinterface_objs:
372 ret[pinterface_obj.name] = {
373 'uuid': pinterface_obj._uuid,
374 'fq_name': pinterface_obj.fq_name,
375 'parent_type': pinterface_obj.parent_type,
376 }
377
378 return ret
379
380
381def physical_interface_get(name, physical_router, **kwargs):
382 '''
383 Return a specific Contrail physical interface
384
385 CLI Example:
386
387 .. code-block:: bash
388
389 salt '*' contrail.physical_interface_get interface_name physical_router_name
390 '''
391 ret = {}
392 vnc_client = _auth(**kwargs)
393 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
394 for pinterf_obj in pinterf_objs:
395 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
396 ret[name] = pinterf_obj.__dict__
397 if len(ret) == 0:
398 return {'Error': 'Error in retrieving physical interface.'}
399 return ret
400
401
402def physical_interface_create(name, physical_router, **kwargs):
403 '''
404 Create specific Contrail physical interface
405
406 CLI Example:
407
408 .. code-block:: bash
409
410 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
411 '''
412 ret = {}
413 vnc_client = _auth(**kwargs)
414 gsc_obj = _get_config(vnc_client)
415 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
416 if 'Error' not in pinterf_obj:
417 return {'OK': 'Physical interface ' + name + ' on ' + physical_router + ' already exists'}
418 else:
419 if __opts__['test']:
420 return "Physical interface " + name + " will be created"
421 prouter = physical_router_get(physical_router)
422 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
423 pinterf_obj = PhysicalInterface(name, prouter_obj)
424 vnc_client.physical_interface_create(pinterf_obj)
425 return "Created"
426
427
428def physical_interface_delete(name, physical_router, **kwargs):
429 '''
430 Delete specific Contrail physical interface
431
432 CLI Example:
433 .. code-block:: bash
434
435 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
436 '''
437 vnc_client = _auth(**kwargs)
438 gsc_obj = _get_config(vnc_client)
439 piface = physical_interface_get(name, physical_router)
440 if __opts__['test']:
441 return "Physical interface " + name + " will be deleted"
442 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
443 return "Deleted"
444
445
446def logical_interface_list(**kwargs):
447 '''
448 Return a list of all Contrail logical interfaces
449
450 CLI Example:
451
452 .. code-block:: bash
453
454 salt '*' contrail.logical_interface_list
455 '''
456 ret = []
457 vnc_client = _auth(**kwargs)
458 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
459 for liface_obj in liface_objs:
460 ret.append({
461 'name': liface_obj.name,
462 'uuid': liface_obj._uuid,
463 'fq_name': liface_obj.fq_name,
464 'parent_type': liface_obj.parent_type,
465 })
466 return ret
467
468
469def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
470 '''
471 Return a specific Contrail logical interface
472
473 CLI Example:
474
475 .. code-block:: bash
476
477 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
478 or
479 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
480 or
481 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
482 '''
483 ret = {}
484 vnc_client = _auth(**kwargs)
485 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
486 count = 0
487 for liface_obj in liface_objs:
488 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
489 if parent_type and parent_type == liface_obj.parent_type:
490 count += 1
491 ret[liface_obj.name] = liface_obj.__dict__
492 if not parent_type:
493 count += 1
494 ret[liface_obj.name] = liface_obj.__dict__
495 if len(ret) == 0:
496 return {'Error': 'Error in retrieving logical interface.'}
497 if count > 1:
498 return {
499 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
500 return ret
501
502
503def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
504 **kwargs):
505 '''
506 Create specific Contrail logical interface
507
508 CLI Example:
509
510 .. code-block:: bash
511
512 salt '*' contrail.logical_interface_create ge-0/0/10.11 parent_names="['ge-0/0/0','phr1']" parent_type=physical-interface vlan_tag=1025 interface_type=L2
513 '''
514 ret = {}
515 vnc_client = _auth(**kwargs)
516 gsc_obj = _get_config(vnc_client)
517 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
518 if 'Error' not in liface_obj:
519 return {'OK': 'Logical interface ' + name + ' already exists'}
520 else:
521 if __opts__['test']:
522 return "Logical interface " + name + " will be created"
523 parent_obj = None
524 for router in parent_names:
525 parent_router = physical_router_get(router)
526 if 'Error' not in parent_router:
527 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
528 break
529 if not parent_obj:
530 return {'Error': 'Physical router have to be defined'}
531 if parent_type == 'physical-interface':
532 for interface in parent_names:
533 parent_interface = physical_interface_get(interface, parent_obj.name)
534 if 'Error' not in parent_interface:
535 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
536 break
537 if interface_type.lower() == "l3":
538 return {'Error': "Virtual Network have to be defined for L3 interface type"}
539
540 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
541 vnc_client.logical_interface_create(liface_obj)
542 return "Created"
543
544
545def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
546 '''
547 Delete specific Contrail logical interface
548
549 CLI Example:
550
551 .. code-block:: bash
552
553 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
554 or
555 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
556
557 '''
558 vnc_client = _auth(**kwargs)
559 gsc_obj = _get_config(vnc_client)
560 liface = logical_interface_get(name, parent_names, parent_type)
561 if 'Error' not in liface:
562 if __opts__['test']:
563 return "Logical interface " + name + " will be deleted"
564 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
565 return "Deleted"
566 else:
567 return liface
568
569
570def global_vrouter_config_list(**kwargs):
571 '''
572 Return a list of all Contrail global vrouter configs
573
574 CLI Example:
575
576 .. code-block:: bash"
577
578 salt '*' global_vrouter_config_list
579 '''
580 ret = {}
581 vnc_client = _auth(**kwargs)
582 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
583 for vrouter_conf_obj in vrouter_conf_objs:
584 ret[vrouter_conf_obj._display_name] = vrouter_conf_obj.__dict__
585 return ret
586
587
588def global_vrouter_config_get(name, **kwargs):
589 '''
590 Return a specific Contrail global vrouter config
591
592 CLI Example:
593
594 .. code-block:: bash
595
596 salt '*' contrail.global_vrouter_get global-vrouter-config
597 '''
598 ret = {}
599 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
600 if name in vrouter_conf_objs:
601 ret[name] = vrouter_conf_objs.get(name)
602 if len(ret) == 0:
603 return {'Error': 'Error in retrieving global vrouter config.'}
604 return ret
605
606
607def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, *fq_names, **kwargs):
608 '''
609 Create specific Contrail global vrouter config
610
611 CLI Example:
612
613 .. code-block:: bash
614
615 salt '*' contrail.global_vrouter_config_create name=global-vrouter-config parent_type=global-system-config encap_priority="MPLSoUDP,MPLSoGRE" vxlan_vn_id_mode="automatic" fq_names="['default-global-system-config', 'default-global-vrouter-config']"
616 '''
617 ret = {}
618 vnc_client = _auth(**kwargs)
619 gsc_obj = _get_config(vnc_client)
620 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
621 if name in vrouter_conf_objs:
622 return {'OK': 'Global vrouter config %s already exists' % name}
623 else:
624 vrouter_conf_obj = GlobalVrouterConfig(
625 name=name,
626 parent_obj=None,
627 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
628 fq_name=fq_names,
629 vxlan_network_identifier_mode=vxlan_vn_id_mode,
630 parent_type=parent_type,
631 )
632 if __opts__['test']:
633 return "Global vRouter config " + name + " will be created"
634 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
635 return "Created"
636
637
638def global_vrouter_config_delete(name, **kwargs):
639 '''
640 Delete specific Contrail global vrouter config
641
642 CLI Example:
643
644 .. code-block:: bash
645
646 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
647 '''
648 vnc_client = _auth(**kwargs)
649 gsc_obj = _get_config(vnc_client)
650 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
651 if __opts__['test']:
652 return "Global vRouter config " + name + " will be deleted"
653 vnc_client.global_vrouter_config_delete(
654 fq_name=vrouter_conf_obj.get_fq_name())
655 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100656
657
658def analytics_node_list(**kwargs):
659 '''
660 Return a list of all Contrail analytics nodes
661
662 CLI Example:
663
664 .. code-block:: bash
665
666 salt '*' contrail.analytics_node_list
667 '''
668 ret = {}
669 vnc_client = _auth(**kwargs)
670 node_objs = vnc_client._objects_list('analytics-node', detail=True)
671 for node_obj in node_objs:
672 ret[node_obj.name] = node_obj.__dict__
673 return ret
674
675
676def analytics_node_get(name, **kwargs):
677 '''
678 Return a specific Contrail analytics node
679
680 CLI Example:
681
682 .. code-block:: bash
683
684 salt '*' contrail.analytics_node_get nal01
685 '''
686 ret = {}
687 vrouter_objs = analytics_node_list(**kwargs)
688 if name in vrouter_objs:
689 ret[name] = vrouter_objs.get(name)
690 if len(ret) == 0:
691 return {'Error': 'Error in retrieving analytics node.'}
692 return ret
693
694
695def analytics_node_create(name, ip_address, **kwargs):
696 '''
697 Create specific Contrail analytics node
698
699 CLI Example:
700
701 .. code-block:: bash
702
703 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
704 '''
705 ret = {}
706 vnc_client = _auth(**kwargs)
707 gsc_obj = _get_config(vnc_client)
708 analytics_node_objs = analytics_node_list(**kwargs)
709 if name in analytics_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200710 return {'OK': 'Analytics node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100711 else:
712 analytics_node_obj = AnalyticsNode(
713 name, gsc_obj,
714 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200715 if __opts__['test']:
716 return "AnalyticsNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100717 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200718 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100719
720
721def analytics_node_delete(name, **kwargs):
722 '''
723 Delete specific Contrail analytics node
724
725 CLI Example:
726
727 .. code-block:: bash
728
729 salt '*' contrail.analytics_node_delete cmp01
730 '''
731 vnc_client = _auth(**kwargs)
732 gsc_obj = _get_config(vnc_client)
733 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200734 if __opts__['test']:
735 return "AnalyticsNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100736 vnc_client.analytics_node_delete(
737 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200738 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100739
740
741def config_node_list(**kwargs):
742 '''
743 Return a list of all Contrail config nodes
744
745 CLI Example:
746
747 .. code-block:: bash
748
749 salt '*' contrail.config_node_list
750 '''
751 ret = {}
752 vnc_client = _auth(**kwargs)
753 node_objs = vnc_client._objects_list('config-node', detail=True)
754 for node_obj in node_objs:
755 ret[node_obj.name] = node_obj.__dict__
756 return ret
757
758
759def config_node_get(name, **kwargs):
760 '''
761 Return a specific Contrail config node
762
763 CLI Example:
764
765 .. code-block:: bash
766
767 salt '*' contrail.config_node_get nal01
768 '''
769 ret = {}
770 vrouter_objs = config_node_list(**kwargs)
771 if name in vrouter_objs:
772 ret[name] = vrouter_objs.get(name)
773 if len(ret) == 0:
774 return {'Error': 'Error in retrieving config node.'}
775 return ret
776
777
778def config_node_create(name, ip_address, **kwargs):
779 '''
780 Create specific Contrail config node
781
782 CLI Example:
783
784 .. code-block:: bash
785
786 salt '*' contrail.config_node_create ntw03 10.10.10.103
787 '''
788 ret = {}
789 vnc_client = _auth(**kwargs)
790 gsc_obj = _get_config(vnc_client)
791 config_node_objs = config_node_list(**kwargs)
792 if name in config_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200793 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100794 else:
795 config_node_obj = ConfigNode(
796 name, gsc_obj,
797 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200798 if __opts__['test']:
799 return "ConfigNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100800 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200801 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100802
803
804def config_node_delete(name, **kwargs):
805 '''
806 Delete specific Contrail config node
807
808 CLI Example:
809
810 .. code-block:: bash
811
812 salt '*' contrail.config_node_delete cmp01
813 '''
814 vnc_client = _auth(**kwargs)
815 gsc_obj = _get_config(vnc_client)
816 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200817 if __opts__['test']:
818 return "ConfigNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100819 vnc_client.config_node_delete(
820 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200821 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100822
823
824def bgp_router_list(**kwargs):
825 '''
826 Return a list of all Contrail BGP routers
827
828 CLI Example:
829
830 .. code-block:: bash
831
832 salt '*' contrail.bgp_router_list
833 '''
834 ret = {}
835 vnc_client = _auth(**kwargs)
836 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
837 for bgp_router_obj in bgp_router_objs:
838 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
839 return ret
840
841
842def bgp_router_get(name, **kwargs):
843 '''
844 Return a specific Contrail BGP router
845
846 CLI Example:
847
848 .. code-block:: bash
849
850 salt '*' contrail.bgp_router_get nal01
851 '''
852 ret = {}
853 bgp_router_objs = bgp_router_list(**kwargs)
854 if name in bgp_router_objs:
855 ret[name] = bgp_router_objs.get(name)
856 if len(ret) == 0:
857 return {'Error': 'Error in retrieving BGP router.'}
858 return ret
859
860
861def bgp_router_create(name, type, ip_address, asn=64512, **kwargs):
862 '''
863 Create specific Contrail control node
864
865 CLI Example:
866
867 .. code-block:: bash
868
869 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
870 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
871 '''
872 ret = {}
873 vnc_client = _auth(**kwargs)
874
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200875 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
876 'inet6-vpn']
877 if type != 'control-node':
878 address_families.remove('erm-vpn')
879
880 bgp_addr_fams = AddressFamilies(address_families)
881 bgp_sess_attrs = [
882 BgpSessionAttributes(address_families=bgp_addr_fams)]
883 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
884 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
885 rt_inst_obj = _get_rt_inst_obj(vnc_client)
886
887 if type == 'control-node':
888 vendor = 'contrail'
889 elif type == 'router':
890 vendor = 'mx'
891 else:
892 vendor = 'unknown'
893
894 router_params = BgpRouterParams(router_type=type,
895 vendor=vendor, autonomous_system=int(asn),
896 identifier=_get_ip(ip_address),
897 address=_get_ip(ip_address),
898 port=179, address_families=bgp_addr_fams)
899
Ales Komarekad46d2e2017-03-09 17:16:38 +0100900 bgp_router_objs = bgp_router_list(**kwargs)
901 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200902 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
903 bgp_router_obj.set_bgp_router_parameters(router_params)
904 if __opts__['test']:
905 return "BGP router " + name + " will be updated"
906 vnc_client.bgp_router_update(bgp_router_obj)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100907 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200908 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
909 if __opts__['test']:
910 return "BGP router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100911 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200912 return "Created"
913 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100914
915
916def bgp_router_delete(name, **kwargs):
917 '''
918 Delete specific Contrail control node
919
920 CLI Example:
921
922 .. code-block:: bash
923
924 salt '*' contrail.bgp_router_delete mx01
925 '''
926 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200927 gsc_obj = _get_config(vnc_client)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100928 bgp_router_obj = BgpRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200929
930 if __opts__['test']:
931 return "BGP router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100932 vnc_client.bgp_router_delete(
933 fq_name=bgp_router_obj.get_fq_name())
934
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200935 return "Deleted"
936
Ales Komarekad46d2e2017-03-09 17:16:38 +0100937
938def database_node_list(**kwargs):
939 '''
940 Return a list of all Contrail database nodes
941
942 CLI Example:
943
944 .. code-block:: bash
945
946 salt '*' contrail.database_node_list
947 '''
948 ret = {}
949 vnc_client = _auth(**kwargs)
950 node_objs = vnc_client._objects_list('database-node', detail=True)
951 for node_obj in node_objs:
952 ret[node_obj.name] = node_obj.__dict__
953 return ret
954
955
956def database_node_get(name, **kwargs):
957 '''
958 Return a specific Contrail database node
959
960 CLI Example:
961
962 .. code-block:: bash
963
964 salt '*' contrail.database_node_get nal01
965 '''
966 ret = {}
967 vrouter_objs = database_node_list(**kwargs)
968 if name in vrouter_objs:
969 ret[name] = vrouter_objs.get(name)
970 if len(ret) == 0:
971 return {'Error': 'Error in retrieving database node.'}
972 return ret
973
974
975def database_node_create(name, ip_address, **kwargs):
976 '''
977 Create specific Contrail database node
978
979 CLI Example:
980
981 .. code-block:: bash
982
983 salt '*' contrail.database_node_create ntw03 10.10.10.103
984 '''
985 ret = {}
986 vnc_client = _auth(**kwargs)
987 gsc_obj = _get_config(vnc_client)
988 database_node_objs = database_node_list(**kwargs)
989 if name in database_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200990 return {'OK': 'Database node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100991 else:
992 database_node_obj = DatabaseNode(
993 name, gsc_obj,
994 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200995 if __opts__['test']:
996 return "DatabaseNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100997 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200998 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100999
1000
1001def database_node_delete(name, **kwargs):
1002 '''
1003 Delete specific Contrail database node
1004
1005 CLI Example:
1006
1007 .. code-block:: bash
1008
1009 salt '*' contrail.database_node_delete cmp01
1010 '''
1011 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001012 gsc_obj = _get_config(vnc_client)
1013 database_node_obj = DatabaseNode(name, gsc_obj)
1014 if __opts__['test']:
1015 return "DatabaseNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +01001016 vnc_client.database_node_delete(
1017 fq_name=database_node_obj.get_fq_name())
Petr Jediný5f3efe32017-05-26 17:55:09 +02001018
1019
Petr Jediný5f3efe32017-05-26 17:55:09 +02001020def _get_vrouter_config(vnc_client):
1021 try:
1022 config = vnc_client.global_vrouter_config_read(
1023 fq_name=['default-global-system-config', 'default-global-vrouter-config'])
1024 except Exception:
1025 config = None
1026
1027 return config
1028
1029
Petr Jediný5f3efe32017-05-26 17:55:09 +02001030def linklocal_service_list(**kwargs):
1031 '''
1032 Return a list of all Contrail link local services
1033
1034 CLI Example:
1035
1036 .. code-block:: bash
1037
1038 salt '*' contrail.linklocal_service_list
1039 '''
1040 ret = {}
1041 vnc_client = _auth(**kwargs)
1042
1043 current_config = _get_vrouter_config(vnc_client)
1044 if current_config is None:
1045 return ret
1046
1047 service_list_res = current_config.get_linklocal_services()
1048 if service_list_res is None:
1049 service_list_obj = {'linklocal_service_entry': []}
1050 else:
1051 service_list_obj = service_list_res.__dict__
1052 for _, value in service_list_obj.iteritems():
1053 for entry in value:
1054 service = entry.__dict__
1055 if 'linklocal_service_name' in service:
1056 ret[service['linklocal_service_name']] = service
1057 return ret
1058
1059
1060def linklocal_service_get(name, **kwargs):
1061 '''
1062 Return a specific Contrail link local service
1063
1064 CLI Example:
1065
1066 .. code-block:: bash
1067
1068 salt '*' contrail.linklocal_service_get llservice
1069 '''
1070 ret = {}
1071 services = linklocal_service_list(**kwargs)
1072 if name in services:
1073 ret[name] = services.get(name)
1074 if len(ret) == 0:
1075 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1076 return ret
1077
1078
1079def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, **kwargs):
1080 '''
1081 Create specific Contrail link local service
1082
1083 CLI Example:
1084
1085 .. code-block:: bash
1086
1087 salt '*' contrail.linklocal_service_create \
1088 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1089 salt '*' contrail.linklocal_service_create \
1090 llservice 10.10.10.103 22 link-local.service.dns-name 22
1091 '''
1092 ret = {}
1093 vnc_client = _auth(**kwargs)
1094
1095 current_config = _get_vrouter_config(vnc_client)
1096
1097 service_entry = LinklocalServiceEntryType(
1098 linklocal_service_name=name,
1099 linklocal_service_ip=lls_ip,
1100 linklocal_service_port=lls_port,
1101 ip_fabric_service_port=ipf_port)
1102 if isinstance(ipf_dns_or_ip, basestring):
1103 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1104 elif isinstance(ipf_dns_or_ip, list):
1105 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1106 service_entry.ip_fabric_DNS_service_name = ''
1107
1108 if current_config is None:
1109 new_services = LinklocalServicesTypes([service_entry])
1110 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001111 if __opts__['test']:
1112 ret['GlobalVrouterConfig'] = "Global vRouter Config will be created"
1113 else:
1114 ret = "Created"
1115 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001116 else:
1117 _current_service_list = current_config.get_linklocal_services()
1118 if _current_service_list is None:
1119 service_list = {'linklocal_service_entry': []}
1120 else:
1121 service_list = _current_service_list.__dict__
1122 new_services = [service_entry]
1123 for key, value in service_list.iteritems():
1124 if key != 'linklocal_service_entry':
1125 continue
1126 for _entry in value:
1127 entry = _entry.__dict__
1128 if 'linklocal_service_name' in entry:
1129 if entry['linklocal_service_name'] == name:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001130 return {'OK': 'Link local service "{0}" already exists'.format(name)}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001131 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001132 if __opts__['test']:
1133 ret['Test'] = "LinkLocalSevices will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001134 service_list[key] = new_services
1135 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001136 if __opts__['test']:
1137 ret['GlobalVrouterConfig'] = "Global vRouter Config will be updated"
1138 else:
1139 vnc_client.global_vrouter_config_update(new_config)
1140 ret = "Created"
1141 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001142
1143
1144def linklocal_service_delete(name, **kwargs):
1145 '''
1146 Delete specific link local service entry
1147
1148 CLI Example:
1149
1150 .. code-block:: bash
1151
1152 salt '*' contrail.linklocal_service_delete llservice
1153 '''
1154 vnc_client = _auth(**kwargs)
1155
1156 current_config = _get_vrouter_config(vnc_client)
1157
1158 found = False
1159 if current_config is not None:
1160 _current_service_list = current_config.get_linklocal_services()
1161 if _current_service_list is None:
1162 service_list = {'linklocal_service_entry': []}
1163 else:
1164 service_list = _current_service_list.__dict__
1165 new_services = []
1166 for key, value in service_list.iteritems():
1167 if key != 'linklocal_service_entry':
1168 continue
1169 for _entry in value:
1170 entry = _entry.__dict__
1171 if 'linklocal_service_name' in entry:
1172 if entry['linklocal_service_name'] == name:
1173 found = True
1174 else:
1175 new_services.append(_entry)
1176 service_list[key] = new_services
1177 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001178 if __opts__['test']:
1179 return "Link local service " + name + " will be deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001180 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001181 return "Deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001182 if not found:
1183 return {'Error': 'Link local service "{0}" not found'.format(name)}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001184
1185
1186def virtual_machine_interface_list(**kwargs):
1187 '''
1188 Return a list of all Contrail virtual machine interfaces
1189
1190 CLI Example:
1191
1192 .. code-block:: bash
1193
1194 salt '*' contrail.virtual_machine_interfaces
1195 '''
1196 ret = {}
1197 vnc_client = _auth(**kwargs)
1198 vm_ifaces = vnc_client._objects_list('virtual-machine-interface', detail=True)
1199 for vm_iface in vm_ifaces:
1200 ret[vm_iface.name] = vm_iface.__dict__
1201 return ret
1202
1203
1204def virtual_network_list(**kwargs):
1205 '''
1206 Return a list of all Contrail virtual network
1207
1208 CLI Example:
1209
1210 .. code-block:: bash
1211
1212 salt '*' contrail.virtual_network
1213 '''
1214
1215 ret = {}
1216 vnc_client = _auth(**kwargs)
1217 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1218 for virtual_network in virtual_networks:
1219 ret[virtual_network.name] = virtual_network.__dict__
1220 return ret