blob: 3719653a282144ef9ee8c90a7e546c712dd8eaf2 [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)
149 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200150 vrouter = virtual_router_get(name)
151 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
152 changed = False
153 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
154 ret['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
155 vrouter_obj.set_virtual_router_ip_address(ip_address)
156 changed = True
157 if vrouter_obj.get_virtual_router_type() != router_type:
158 ret['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
159 vrouter_obj.set_virtual_router_type(router_type)
160 changed = True
161 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
162 ret['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
163 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
164 changed = True
165 if changed:
166 if __opts__['test']:
167 return "Virtual router " + name + " will be updated"
168 vnc_client.virtual_router_update(vrouter_obj)
169 return ret
170 return {'OK': 'Virtual router %s already exists and is updated' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100171 else:
172 vrouter_obj = VirtualRouter(
173 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200174 virtual_router_ip_address=ip_address,
175 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100176 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200177 if __opts__['test']:
178 return "Virtual router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100179 vnc_client.virtual_router_create(vrouter_obj)
180 ret = virtual_router_list(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200181 return "Create"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100182
183
184def virtual_router_delete(name, **kwargs):
185 '''
186 Delete specific Contrail virtual router
187
188 CLI Example:
189
190 .. code-block:: bash
191
192 salt '*' contrail.virtual_router_delete cmp01
193 '''
194 vnc_client = _auth(**kwargs)
195 gsc_obj = _get_config(vnc_client)
196 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200197 if __opts__['test']:
198 return "Virtual router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100199 vnc_client.virtual_router_delete(
200 fq_name=vrouter_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200201 return "Deleted"
202
203
204def physical_router_list(**kwargs):
205 '''
206 Return a list of all Contrail physical routers
207
208 CLI Example:
209
210 .. code-block:: bash
211
212 salt '*' contrail.physical_router_list
213 '''
214 ret = {}
215 vnc_client = _auth(**kwargs)
216 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
217 for prouter_obj in prouter_objs:
218 ret[prouter_obj.name] = {
219 'uuid': prouter_obj._uuid,
220 'management_ip': prouter_obj._physical_router_management_ip,
221 'product_name': prouter_obj._physical_router_product_name,
222 }
223
224 return ret
225
226
227def physical_router_get(name, **kwargs):
228 '''
229 Return a specific Contrail physical router
230
231 CLI Example:
232
233 .. code-block:: bash
234
235 salt '*' contrail.physical_router_get router_name
236 '''
237 ret = {}
238 vnc_client = _auth(**kwargs)
239 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
240 for prouter_obj in prouter_objs:
241 if name == prouter_obj.name:
242 ret[name] = prouter_obj.__dict__
243 if len(ret) == 0:
244 return {'Error': 'Error in retrieving physical router.'}
245 return ret
246
247
248def physical_router_create(name, parent_type=None,
249 management_ip=None,
250 dataplane_ip=None, # VTEP address in web GUI
251 vendor_name=None,
252 product_name=None,
253 vnc_managed=None,
254 junos_service_ports=None,
255 agents=None, **kwargs):
256 '''
257 Create specific Contrail physical router
258
259 CLI Example:
260
261 .. code-block:: bash
262
263 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']"
264 '''
265 ret = {}
266 vnc_client = _auth(**kwargs)
267 gsc_obj = _get_config(vnc_client)
268 prouter_objs = physical_router_list(**kwargs)
269 if name in prouter_objs:
270 prouter = physical_router_get(name)
271 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
272 if prouter_obj.physical_router_management_ip != management_ip:
273 ret['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
274 prouter_obj.set_physical_router_management_ip(management_ip)
275 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
276 ret['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
277 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
278 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
279 ret['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
280 prouter_obj.set_physical_router_vendor_name(vendor_name)
281 if prouter_obj.get_physical_router_product_name() != product_name:
282 ret['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
283 prouter_obj.set_physical_router_product_name(product_name)
284 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
285 ret['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
286 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
287 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
288 ret['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
289 "to": junos_service_ports}
290 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
291
292 if __opts__['test']:
293 if len(ret) != 0:
294 return "Physical router " + name + " will be updated"
295 return {"OK": "Physical router exists and is updated"}
296
297 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
298 c_agents = [] # referenced vrouters
299 for c_agent in prouter_obj.get_virtual_router_refs():
300 c_agents.append(c_agent['uuid'])
301 agent_objs = [] # required state of references
302 for vrouter_obj in vrouter_objs:
303 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
304 prouter_obj.add_virtual_router(vrouter_obj)
305 ret['vrouter ' + vrouter_obj._display_name] = "Reference added"
306 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
307 prouter_obj.del_virtual_router(vrouter_obj)
308 ret['vrouter ' + vrouter_obj._display_name] = "Reference removed"
309 vnc_client.physical_router_update(prouter_obj)
310
311 if len(ret) == 0:
312 return {"OK": "Physical router exists and is updated"}
313 return ret
314 else:
315 if __opts__['test']:
316 return "Physical router " + name + " will be created"
317 prouter_obj = PhysicalRouter(
318 name=name,
319 parent_obj=None,
320 physical_router_management_ip=management_ip,
321 physical_router_dataplane_ip=dataplane_ip,
322 physical_router_vendor_name=vendor_name,
323 physical_router_product_name=product_name,
324 physical_router_vnc_managed=vnc_managed,
325 physical_router_junos_service_ports=junos_service_ports,
326 )
327 for agent in agents:
328 vrouter = virtual_router_get(agent)
329 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
330 prouter_obj.add_virtual_router(vrouter_obj)
331 vnc_client.physical_router_create(prouter_obj)
332 return "Created"
333
334
335def physical_router_delete(name, **kwargs):
336 '''
337 Delete specific Contrail physical router
338
339 CLI Example:
340
341 .. code-block:: bash
342
343 salt '*' contrail.physical_router_delete router_name
344 '''
345 vnc_client = _auth(**kwargs)
346 gsc_obj = _get_config(vnc_client)
347 prouter_obj = PhysicalRouter(name, gsc_obj)
348 if __opts__['test']:
349 return "Physical router " + name + " will be deleted"
350 vnc_client.physical_router_delete(
351 fq_name=prouter_obj.get_fq_name())
352 return "Deleted"
353
354
355def physical_interface_list(**kwargs):
356 '''
357 Return a list of all Contrail physical interface
358
359 CLI Example:
360
361 .. code-block:: bash
362
363 salt '*' contrail.physical_interface_list
364 '''
365 ret = {}
366 vnc_client = _auth(**kwargs)
367 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
368 for pinterface_obj in pinterface_objs:
369 ret[pinterface_obj.name] = {
370 'uuid': pinterface_obj._uuid,
371 'fq_name': pinterface_obj.fq_name,
372 'parent_type': pinterface_obj.parent_type,
373 }
374
375 return ret
376
377
378def physical_interface_get(name, physical_router, **kwargs):
379 '''
380 Return a specific Contrail physical interface
381
382 CLI Example:
383
384 .. code-block:: bash
385
386 salt '*' contrail.physical_interface_get interface_name physical_router_name
387 '''
388 ret = {}
389 vnc_client = _auth(**kwargs)
390 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
391 for pinterf_obj in pinterf_objs:
392 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
393 ret[name] = pinterf_obj.__dict__
394 if len(ret) == 0:
395 return {'Error': 'Error in retrieving physical interface.'}
396 return ret
397
398
399def physical_interface_create(name, physical_router, **kwargs):
400 '''
401 Create specific Contrail physical interface
402
403 CLI Example:
404
405 .. code-block:: bash
406
407 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
408 '''
409 ret = {}
410 vnc_client = _auth(**kwargs)
411 gsc_obj = _get_config(vnc_client)
412 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
413 if 'Error' not in pinterf_obj:
414 return {'OK': 'Physical interface ' + name + ' on ' + physical_router + ' already exists'}
415 else:
416 if __opts__['test']:
417 return "Physical interface " + name + " will be created"
418 prouter = physical_router_get(physical_router)
419 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
420 pinterf_obj = PhysicalInterface(name, prouter_obj)
421 vnc_client.physical_interface_create(pinterf_obj)
422 return "Created"
423
424
425def physical_interface_delete(name, physical_router, **kwargs):
426 '''
427 Delete specific Contrail physical interface
428
429 CLI Example:
430 .. code-block:: bash
431
432 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
433 '''
434 vnc_client = _auth(**kwargs)
435 gsc_obj = _get_config(vnc_client)
436 piface = physical_interface_get(name, physical_router)
437 if __opts__['test']:
438 return "Physical interface " + name + " will be deleted"
439 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
440 return "Deleted"
441
442
443def logical_interface_list(**kwargs):
444 '''
445 Return a list of all Contrail logical interfaces
446
447 CLI Example:
448
449 .. code-block:: bash
450
451 salt '*' contrail.logical_interface_list
452 '''
453 ret = []
454 vnc_client = _auth(**kwargs)
455 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
456 for liface_obj in liface_objs:
457 ret.append({
458 'name': liface_obj.name,
459 'uuid': liface_obj._uuid,
460 'fq_name': liface_obj.fq_name,
461 'parent_type': liface_obj.parent_type,
462 })
463 return ret
464
465
466def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
467 '''
468 Return a specific Contrail logical interface
469
470 CLI Example:
471
472 .. code-block:: bash
473
474 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
475 or
476 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
477 or
478 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
479 '''
480 ret = {}
481 vnc_client = _auth(**kwargs)
482 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
483 count = 0
484 for liface_obj in liface_objs:
485 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
486 if parent_type and parent_type == liface_obj.parent_type:
487 count += 1
488 ret[liface_obj.name] = liface_obj.__dict__
489 if not parent_type:
490 count += 1
491 ret[liface_obj.name] = liface_obj.__dict__
492 if len(ret) == 0:
493 return {'Error': 'Error in retrieving logical interface.'}
494 if count > 1:
495 return {
496 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
497 return ret
498
499
500def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
501 **kwargs):
502 '''
503 Create specific Contrail logical interface
504
505 CLI Example:
506
507 .. code-block:: bash
508
509 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
510 '''
511 ret = {}
512 vnc_client = _auth(**kwargs)
513 gsc_obj = _get_config(vnc_client)
514 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
515 if 'Error' not in liface_obj:
516 return {'OK': 'Logical interface ' + name + ' already exists'}
517 else:
518 if __opts__['test']:
519 return "Logical interface " + name + " will be created"
520 parent_obj = None
521 for router in parent_names:
522 parent_router = physical_router_get(router)
523 if 'Error' not in parent_router:
524 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
525 break
526 if not parent_obj:
527 return {'Error': 'Physical router have to be defined'}
528 if parent_type == 'physical-interface':
529 for interface in parent_names:
530 parent_interface = physical_interface_get(interface, parent_obj.name)
531 if 'Error' not in parent_interface:
532 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
533 break
534 if interface_type.lower() == "l3":
535 return {'Error': "Virtual Network have to be defined for L3 interface type"}
536
537 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
538 vnc_client.logical_interface_create(liface_obj)
539 return "Created"
540
541
542def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
543 '''
544 Delete specific Contrail logical interface
545
546 CLI Example:
547
548 .. code-block:: bash
549
550 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
551 or
552 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
553
554 '''
555 vnc_client = _auth(**kwargs)
556 gsc_obj = _get_config(vnc_client)
557 liface = logical_interface_get(name, parent_names, parent_type)
558 if 'Error' not in liface:
559 if __opts__['test']:
560 return "Logical interface " + name + " will be deleted"
561 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
562 return "Deleted"
563 else:
564 return liface
565
566
567def global_vrouter_config_list(**kwargs):
568 '''
569 Return a list of all Contrail global vrouter configs
570
571 CLI Example:
572
573 .. code-block:: bash"
574
575 salt '*' global_vrouter_config_list
576 '''
577 ret = {}
578 vnc_client = _auth(**kwargs)
579 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
580 for vrouter_conf_obj in vrouter_conf_objs:
581 ret[vrouter_conf_obj._display_name] = vrouter_conf_obj.__dict__
582 return ret
583
584
585def global_vrouter_config_get(name, **kwargs):
586 '''
587 Return a specific Contrail global vrouter config
588
589 CLI Example:
590
591 .. code-block:: bash
592
593 salt '*' contrail.global_vrouter_get global-vrouter-config
594 '''
595 ret = {}
596 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
597 if name in vrouter_conf_objs:
598 ret[name] = vrouter_conf_objs.get(name)
599 if len(ret) == 0:
600 return {'Error': 'Error in retrieving global vrouter config.'}
601 return ret
602
603
604def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, *fq_names, **kwargs):
605 '''
606 Create specific Contrail global vrouter config
607
608 CLI Example:
609
610 .. code-block:: bash
611
612 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']"
613 '''
614 ret = {}
615 vnc_client = _auth(**kwargs)
616 gsc_obj = _get_config(vnc_client)
617 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
618 if name in vrouter_conf_objs:
619 return {'OK': 'Global vrouter config %s already exists' % name}
620 else:
621 vrouter_conf_obj = GlobalVrouterConfig(
622 name=name,
623 parent_obj=None,
624 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
625 fq_name=fq_names,
626 vxlan_network_identifier_mode=vxlan_vn_id_mode,
627 parent_type=parent_type,
628 )
629 if __opts__['test']:
630 return "Global vRouter config " + name + " will be created"
631 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
632 return "Created"
633
634
635def global_vrouter_config_delete(name, **kwargs):
636 '''
637 Delete specific Contrail global vrouter config
638
639 CLI Example:
640
641 .. code-block:: bash
642
643 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
644 '''
645 vnc_client = _auth(**kwargs)
646 gsc_obj = _get_config(vnc_client)
647 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
648 if __opts__['test']:
649 return "Global vRouter config " + name + " will be deleted"
650 vnc_client.global_vrouter_config_delete(
651 fq_name=vrouter_conf_obj.get_fq_name())
652 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100653
654
655def analytics_node_list(**kwargs):
656 '''
657 Return a list of all Contrail analytics nodes
658
659 CLI Example:
660
661 .. code-block:: bash
662
663 salt '*' contrail.analytics_node_list
664 '''
665 ret = {}
666 vnc_client = _auth(**kwargs)
667 node_objs = vnc_client._objects_list('analytics-node', detail=True)
668 for node_obj in node_objs:
669 ret[node_obj.name] = node_obj.__dict__
670 return ret
671
672
673def analytics_node_get(name, **kwargs):
674 '''
675 Return a specific Contrail analytics node
676
677 CLI Example:
678
679 .. code-block:: bash
680
681 salt '*' contrail.analytics_node_get nal01
682 '''
683 ret = {}
684 vrouter_objs = analytics_node_list(**kwargs)
685 if name in vrouter_objs:
686 ret[name] = vrouter_objs.get(name)
687 if len(ret) == 0:
688 return {'Error': 'Error in retrieving analytics node.'}
689 return ret
690
691
692def analytics_node_create(name, ip_address, **kwargs):
693 '''
694 Create specific Contrail analytics node
695
696 CLI Example:
697
698 .. code-block:: bash
699
700 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
701 '''
702 ret = {}
703 vnc_client = _auth(**kwargs)
704 gsc_obj = _get_config(vnc_client)
705 analytics_node_objs = analytics_node_list(**kwargs)
706 if name in analytics_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200707 return {'OK': 'Analytics node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100708 else:
709 analytics_node_obj = AnalyticsNode(
710 name, gsc_obj,
711 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200712 if __opts__['test']:
713 return "AnalyticsNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100714 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200715 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100716
717
718def analytics_node_delete(name, **kwargs):
719 '''
720 Delete specific Contrail analytics node
721
722 CLI Example:
723
724 .. code-block:: bash
725
726 salt '*' contrail.analytics_node_delete cmp01
727 '''
728 vnc_client = _auth(**kwargs)
729 gsc_obj = _get_config(vnc_client)
730 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200731 if __opts__['test']:
732 return "AnalyticsNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100733 vnc_client.analytics_node_delete(
734 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200735 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100736
737
738def config_node_list(**kwargs):
739 '''
740 Return a list of all Contrail config nodes
741
742 CLI Example:
743
744 .. code-block:: bash
745
746 salt '*' contrail.config_node_list
747 '''
748 ret = {}
749 vnc_client = _auth(**kwargs)
750 node_objs = vnc_client._objects_list('config-node', detail=True)
751 for node_obj in node_objs:
752 ret[node_obj.name] = node_obj.__dict__
753 return ret
754
755
756def config_node_get(name, **kwargs):
757 '''
758 Return a specific Contrail config node
759
760 CLI Example:
761
762 .. code-block:: bash
763
764 salt '*' contrail.config_node_get nal01
765 '''
766 ret = {}
767 vrouter_objs = config_node_list(**kwargs)
768 if name in vrouter_objs:
769 ret[name] = vrouter_objs.get(name)
770 if len(ret) == 0:
771 return {'Error': 'Error in retrieving config node.'}
772 return ret
773
774
775def config_node_create(name, ip_address, **kwargs):
776 '''
777 Create specific Contrail config node
778
779 CLI Example:
780
781 .. code-block:: bash
782
783 salt '*' contrail.config_node_create ntw03 10.10.10.103
784 '''
785 ret = {}
786 vnc_client = _auth(**kwargs)
787 gsc_obj = _get_config(vnc_client)
788 config_node_objs = config_node_list(**kwargs)
789 if name in config_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200790 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100791 else:
792 config_node_obj = ConfigNode(
793 name, gsc_obj,
794 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200795 if __opts__['test']:
796 return "ConfigNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100797 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200798 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100799
800
801def config_node_delete(name, **kwargs):
802 '''
803 Delete specific Contrail config node
804
805 CLI Example:
806
807 .. code-block:: bash
808
809 salt '*' contrail.config_node_delete cmp01
810 '''
811 vnc_client = _auth(**kwargs)
812 gsc_obj = _get_config(vnc_client)
813 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200814 if __opts__['test']:
815 return "ConfigNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100816 vnc_client.config_node_delete(
817 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200818 return "Deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100819
820
821def bgp_router_list(**kwargs):
822 '''
823 Return a list of all Contrail BGP routers
824
825 CLI Example:
826
827 .. code-block:: bash
828
829 salt '*' contrail.bgp_router_list
830 '''
831 ret = {}
832 vnc_client = _auth(**kwargs)
833 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
834 for bgp_router_obj in bgp_router_objs:
835 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
836 return ret
837
838
839def bgp_router_get(name, **kwargs):
840 '''
841 Return a specific Contrail BGP router
842
843 CLI Example:
844
845 .. code-block:: bash
846
847 salt '*' contrail.bgp_router_get nal01
848 '''
849 ret = {}
850 bgp_router_objs = bgp_router_list(**kwargs)
851 if name in bgp_router_objs:
852 ret[name] = bgp_router_objs.get(name)
853 if len(ret) == 0:
854 return {'Error': 'Error in retrieving BGP router.'}
855 return ret
856
857
858def bgp_router_create(name, type, ip_address, asn=64512, **kwargs):
859 '''
860 Create specific Contrail control node
861
862 CLI Example:
863
864 .. code-block:: bash
865
866 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
867 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
868 '''
869 ret = {}
870 vnc_client = _auth(**kwargs)
871
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200872 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
873 'inet6-vpn']
874 if type != 'control-node':
875 address_families.remove('erm-vpn')
876
877 bgp_addr_fams = AddressFamilies(address_families)
878 bgp_sess_attrs = [
879 BgpSessionAttributes(address_families=bgp_addr_fams)]
880 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
881 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
882 rt_inst_obj = _get_rt_inst_obj(vnc_client)
883
884 if type == 'control-node':
885 vendor = 'contrail'
886 elif type == 'router':
887 vendor = 'mx'
888 else:
889 vendor = 'unknown'
890
891 router_params = BgpRouterParams(router_type=type,
892 vendor=vendor, autonomous_system=int(asn),
893 identifier=_get_ip(ip_address),
894 address=_get_ip(ip_address),
895 port=179, address_families=bgp_addr_fams)
896
Ales Komarekad46d2e2017-03-09 17:16:38 +0100897 bgp_router_objs = bgp_router_list(**kwargs)
898 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200899 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
900 bgp_router_obj.set_bgp_router_parameters(router_params)
901 if __opts__['test']:
902 return "BGP router " + name + " will be updated"
903 vnc_client.bgp_router_update(bgp_router_obj)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100904 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200905 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
906 if __opts__['test']:
907 return "BGP router " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100908 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200909 return "Created"
910 return {'OK': 'Config node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100911
912
913def bgp_router_delete(name, **kwargs):
914 '''
915 Delete specific Contrail control node
916
917 CLI Example:
918
919 .. code-block:: bash
920
921 salt '*' contrail.bgp_router_delete mx01
922 '''
923 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200924 gsc_obj = _get_config(vnc_client)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100925 bgp_router_obj = BgpRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200926
927 if __opts__['test']:
928 return "BGP router " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100929 vnc_client.bgp_router_delete(
930 fq_name=bgp_router_obj.get_fq_name())
931
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200932 return "Deleted"
933
Ales Komarekad46d2e2017-03-09 17:16:38 +0100934
935def database_node_list(**kwargs):
936 '''
937 Return a list of all Contrail database nodes
938
939 CLI Example:
940
941 .. code-block:: bash
942
943 salt '*' contrail.database_node_list
944 '''
945 ret = {}
946 vnc_client = _auth(**kwargs)
947 node_objs = vnc_client._objects_list('database-node', detail=True)
948 for node_obj in node_objs:
949 ret[node_obj.name] = node_obj.__dict__
950 return ret
951
952
953def database_node_get(name, **kwargs):
954 '''
955 Return a specific Contrail database node
956
957 CLI Example:
958
959 .. code-block:: bash
960
961 salt '*' contrail.database_node_get nal01
962 '''
963 ret = {}
964 vrouter_objs = database_node_list(**kwargs)
965 if name in vrouter_objs:
966 ret[name] = vrouter_objs.get(name)
967 if len(ret) == 0:
968 return {'Error': 'Error in retrieving database node.'}
969 return ret
970
971
972def database_node_create(name, ip_address, **kwargs):
973 '''
974 Create specific Contrail database node
975
976 CLI Example:
977
978 .. code-block:: bash
979
980 salt '*' contrail.database_node_create ntw03 10.10.10.103
981 '''
982 ret = {}
983 vnc_client = _auth(**kwargs)
984 gsc_obj = _get_config(vnc_client)
985 database_node_objs = database_node_list(**kwargs)
986 if name in database_node_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200987 return {'OK': 'Database node %s already exists' % name}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100988 else:
989 database_node_obj = DatabaseNode(
990 name, gsc_obj,
991 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200992 if __opts__['test']:
993 return "DatabaseNode " + name + " will be created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100994 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200995 return "Created"
Ales Komarekad46d2e2017-03-09 17:16:38 +0100996
997
998def database_node_delete(name, **kwargs):
999 '''
1000 Delete specific Contrail database node
1001
1002 CLI Example:
1003
1004 .. code-block:: bash
1005
1006 salt '*' contrail.database_node_delete cmp01
1007 '''
1008 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001009 gsc_obj = _get_config(vnc_client)
1010 database_node_obj = DatabaseNode(name, gsc_obj)
1011 if __opts__['test']:
1012 return "DatabaseNode " + name + " will be deleted"
Ales Komarekad46d2e2017-03-09 17:16:38 +01001013 vnc_client.database_node_delete(
1014 fq_name=database_node_obj.get_fq_name())
Petr Jediný5f3efe32017-05-26 17:55:09 +02001015
1016
Petr Jediný5f3efe32017-05-26 17:55:09 +02001017def _get_vrouter_config(vnc_client):
1018 try:
1019 config = vnc_client.global_vrouter_config_read(
1020 fq_name=['default-global-system-config', 'default-global-vrouter-config'])
1021 except Exception:
1022 config = None
1023
1024 return config
1025
1026
Petr Jediný5f3efe32017-05-26 17:55:09 +02001027def linklocal_service_list(**kwargs):
1028 '''
1029 Return a list of all Contrail link local services
1030
1031 CLI Example:
1032
1033 .. code-block:: bash
1034
1035 salt '*' contrail.linklocal_service_list
1036 '''
1037 ret = {}
1038 vnc_client = _auth(**kwargs)
1039
1040 current_config = _get_vrouter_config(vnc_client)
1041 if current_config is None:
1042 return ret
1043
1044 service_list_res = current_config.get_linklocal_services()
1045 if service_list_res is None:
1046 service_list_obj = {'linklocal_service_entry': []}
1047 else:
1048 service_list_obj = service_list_res.__dict__
1049 for _, value in service_list_obj.iteritems():
1050 for entry in value:
1051 service = entry.__dict__
1052 if 'linklocal_service_name' in service:
1053 ret[service['linklocal_service_name']] = service
1054 return ret
1055
1056
1057def linklocal_service_get(name, **kwargs):
1058 '''
1059 Return a specific Contrail link local service
1060
1061 CLI Example:
1062
1063 .. code-block:: bash
1064
1065 salt '*' contrail.linklocal_service_get llservice
1066 '''
1067 ret = {}
1068 services = linklocal_service_list(**kwargs)
1069 if name in services:
1070 ret[name] = services.get(name)
1071 if len(ret) == 0:
1072 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1073 return ret
1074
1075
1076def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, **kwargs):
1077 '''
1078 Create specific Contrail link local service
1079
1080 CLI Example:
1081
1082 .. code-block:: bash
1083
1084 salt '*' contrail.linklocal_service_create \
1085 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1086 salt '*' contrail.linklocal_service_create \
1087 llservice 10.10.10.103 22 link-local.service.dns-name 22
1088 '''
1089 ret = {}
1090 vnc_client = _auth(**kwargs)
1091
1092 current_config = _get_vrouter_config(vnc_client)
1093
1094 service_entry = LinklocalServiceEntryType(
1095 linklocal_service_name=name,
1096 linklocal_service_ip=lls_ip,
1097 linklocal_service_port=lls_port,
1098 ip_fabric_service_port=ipf_port)
1099 if isinstance(ipf_dns_or_ip, basestring):
1100 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1101 elif isinstance(ipf_dns_or_ip, list):
1102 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1103 service_entry.ip_fabric_DNS_service_name = ''
1104
1105 if current_config is None:
1106 new_services = LinklocalServicesTypes([service_entry])
1107 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001108 if __opts__['test']:
1109 ret['GlobalVrouterConfig'] = "Global vRouter Config will be created"
1110 else:
1111 ret = "Created"
1112 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001113 else:
1114 _current_service_list = current_config.get_linklocal_services()
1115 if _current_service_list is None:
1116 service_list = {'linklocal_service_entry': []}
1117 else:
1118 service_list = _current_service_list.__dict__
1119 new_services = [service_entry]
1120 for key, value in service_list.iteritems():
1121 if key != 'linklocal_service_entry':
1122 continue
1123 for _entry in value:
1124 entry = _entry.__dict__
1125 if 'linklocal_service_name' in entry:
1126 if entry['linklocal_service_name'] == name:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001127 return {'OK': 'Link local service "{0}" already exists'.format(name)}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001128 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001129 if __opts__['test']:
1130 ret['Test'] = "LinkLocalSevices will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001131 service_list[key] = new_services
1132 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001133 if __opts__['test']:
1134 ret['GlobalVrouterConfig'] = "Global vRouter Config will be updated"
1135 else:
1136 vnc_client.global_vrouter_config_update(new_config)
1137 ret = "Created"
1138 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001139
1140
1141def linklocal_service_delete(name, **kwargs):
1142 '''
1143 Delete specific link local service entry
1144
1145 CLI Example:
1146
1147 .. code-block:: bash
1148
1149 salt '*' contrail.linklocal_service_delete llservice
1150 '''
1151 vnc_client = _auth(**kwargs)
1152
1153 current_config = _get_vrouter_config(vnc_client)
1154
1155 found = False
1156 if current_config is not None:
1157 _current_service_list = current_config.get_linklocal_services()
1158 if _current_service_list is None:
1159 service_list = {'linklocal_service_entry': []}
1160 else:
1161 service_list = _current_service_list.__dict__
1162 new_services = []
1163 for key, value in service_list.iteritems():
1164 if key != 'linklocal_service_entry':
1165 continue
1166 for _entry in value:
1167 entry = _entry.__dict__
1168 if 'linklocal_service_name' in entry:
1169 if entry['linklocal_service_name'] == name:
1170 found = True
1171 else:
1172 new_services.append(_entry)
1173 service_list[key] = new_services
1174 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001175 if __opts__['test']:
1176 return "Link local service " + name + " will be deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001177 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001178 return "Deleted"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001179 if not found:
1180 return {'Error': 'Link local service "{0}" not found'.format(name)}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001181
1182
1183def virtual_machine_interface_list(**kwargs):
1184 '''
1185 Return a list of all Contrail virtual machine interfaces
1186
1187 CLI Example:
1188
1189 .. code-block:: bash
1190
1191 salt '*' contrail.virtual_machine_interfaces
1192 '''
1193 ret = {}
1194 vnc_client = _auth(**kwargs)
1195 vm_ifaces = vnc_client._objects_list('virtual-machine-interface', detail=True)
1196 for vm_iface in vm_ifaces:
1197 ret[vm_iface.name] = vm_iface.__dict__
1198 return ret
1199
1200
1201def virtual_network_list(**kwargs):
1202 '''
1203 Return a list of all Contrail virtual network
1204
1205 CLI Example:
1206
1207 .. code-block:: bash
1208
1209 salt '*' contrail.virtual_network
1210 '''
1211
1212 ret = {}
1213 vnc_client = _auth(**kwargs)
1214 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1215 for virtual_network in virtual_networks:
1216 ret[virtual_network.name] = virtual_network.__dict__
1217 return ret