blob: df33d78ea14778ce23bc5e5b7b2c8bd2051d7f6b [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
Vasyl Saienkob10b7202017-09-05 14:19:03 +030019from vnc_api.vnc_api import VirtualMachineInterface, MacAddressesType
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020020from vnc_api.vnc_api import ServiceApplianceSet, KeyValuePairs, KeyValuePair
Ales Komarekad46d2e2017-03-09 17:16:38 +010021
22try:
23 from vnc_api import vnc_api
Petr Jediný5f3efe32017-05-26 17:55:09 +020024 from vnc_api.vnc_api import LinklocalServiceEntryType, \
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +010025 LinklocalServicesTypes, GlobalVrouterConfig, GlobalSystemConfig
Ales Komarekad46d2e2017-03-09 17:16:38 +010026 from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \
Jan Cacha859e6b2018-01-09 17:34:18 +010027 ConfigNode, DatabaseNode, BgpRouter, VirtualNetwork
Ales Komarekad46d2e2017-03-09 17:16:38 +010028 from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \
Marek Celoud3097e5b2018-01-09 13:52:14 +010029 BgpSession, BgpPeeringAttributes, BgpRouterParams, AuthenticationData, \
Jan Cacha859e6b2018-01-09 17:34:18 +010030 AuthenticationKeyItem, VirtualNetworkType, IpamSubnetType, SubnetType, \
31 VnSubnetsType, RouteTargetList
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020032
Ales Komarekad46d2e2017-03-09 17:16:38 +010033 HAS_CONTRAIL = True
34except ImportError:
35 HAS_CONTRAIL = False
36
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +010037
38try:
39 from vnc_api.gen.resource_xsd import GracefulRestartParametersType
40 HAS_OLD = False
41except ImportError:
42 HAS_OLD = True
43
Ales Komarekad46d2e2017-03-09 17:16:38 +010044__opts__ = {}
45
46
47def __virtual__():
48 '''
49 Only load this module if vnc_api library is installed.
50 '''
51 if HAS_CONTRAIL:
52 return 'contrail'
53
54 return False
55
56
57def _auth(**kwargs):
58 '''
59 Set up Contrail API credentials.
60 '''
61 user = kwargs.get('user')
62 password = kwargs.get('password')
63 tenant_name = kwargs.get('project')
64 api_host = kwargs.get('api_server_ip')
65 api_port = kwargs.get('api_server_port')
66 api_base_url = kwargs.get('api_base_url')
67 use_ssl = False
68 auth_host = kwargs.get('auth_host_ip')
69 vnc_lib = vnc_api.VncApi(user, password, tenant_name,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020070 api_host, api_port, api_base_url, wait_for_connect=True,
71 api_server_use_ssl=use_ssl, auth_host=auth_host)
Ales Komarekad46d2e2017-03-09 17:16:38 +010072
73 return vnc_lib
74
75
Pavel Svimbersky483a19e2017-08-22 09:50:29 +020076def _get_config(vnc_client, global_system_config='default-global-system-config'):
Ales Komarekad46d2e2017-03-09 17:16:38 +010077 try:
78 gsc_obj = vnc_client.global_system_config_read(id=global_system_config)
79 except vnc_api.NoIdError:
80 gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config)
81 except:
82 gsc_obj = None
83
84 return gsc_obj
85
86
87def _get_rt_inst_obj(vnc_client):
Ales Komarekad46d2e2017-03-09 17:16:38 +010088 # TODO pick fqname hardcode from common
89 rt_inst_obj = vnc_client.routing_instance_read(
90 fq_name=['default-domain', 'default-project',
91 'ip-fabric', '__default__'])
92
93 return rt_inst_obj
94
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +020095
Vasyl Saienkob10b7202017-09-05 14:19:03 +030096def _get_fq_name(vnc_client, resource_name, project_name, domain='default-domain'):
97 res = [domain]
98 if project_name:
99 res.append(project_name)
100 if resource_name:
101 res.append(resource_name)
102 return res
103
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +0200104
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300105def _get_project_obj(vnc_client, name, domain='default-domain'):
106 return vnc_client.project_read(fq_name=[domain, name])
107
Ales Komarekad46d2e2017-03-09 17:16:38 +0100108
109def _get_ip(ip_w_pfx):
110 return str(IPNetwork(ip_w_pfx).ip)
111
112
Ales Komarekad46d2e2017-03-09 17:16:38 +0100113def virtual_router_list(**kwargs):
114 '''
115 Return a list of all Contrail virtual routers
116
117 CLI Example:
118
119 .. code-block:: bash
120
121 salt '*' contrail.virtual_router_list
122 '''
123 ret = {}
124 vnc_client = _auth(**kwargs)
125 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True)
126 for vrouter_obj in vrouter_objs:
127 ret[vrouter_obj.name] = {
128 'ip_address': vrouter_obj.virtual_router_ip_address,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200129 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled,
130 'uuid': vrouter_obj.uuid
131
Ales Komarekad46d2e2017-03-09 17:16:38 +0100132 }
133 return ret
134
135
136def virtual_router_get(name, **kwargs):
137 '''
138 Return a specific Contrail virtual router
139
140 CLI Example:
141
142 .. code-block:: bash
143
144 salt '*' contrail.virtual_router_get cmp01
145 '''
146 ret = {}
147 vrouter_objs = virtual_router_list(**kwargs)
148 if name in vrouter_objs:
149 ret[name] = vrouter_objs.get(name)
150 if len(ret) == 0:
151 return {'Error': 'Error in retrieving virtual router.'}
152 return ret
153
154
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200155def virtual_router_create(name, ip_address, router_type=None, dpdk_enabled=False, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +0100156 '''
157 Create specific Contrail virtual router
158
159 CLI Example:
160
161 .. code-block:: bash
162
163 salt '*' contrail.virtual_router_create cmp02 10.10.10.102
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200164 router_types:
165 - tor-agent
166 - tor-service-node
167 - embedded
Ales Komarekad46d2e2017-03-09 17:16:38 +0100168 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200169 ret = {'name': name,
170 'changes': {},
171 'result': True,
172 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100173 vnc_client = _auth(**kwargs)
174 gsc_obj = _get_config(vnc_client)
175 vrouter_objs = virtual_router_list(**kwargs)
Pavel Svimberskya3140552017-08-28 16:55:44 +0200176 router_types = ['tor-agent', 'tor-service-node', 'embedded']
177 if router_type not in router_types:
178 router_type = None
Ales Komarekad46d2e2017-03-09 17:16:38 +0100179 if name in vrouter_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200180 vrouter = virtual_router_get(name)
181 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[name]['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200182 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200183 if vrouter_obj.get_virtual_router_ip_address() != ip_address:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200184 changes['ip_address'] = {'from': vrouter_obj.get_virtual_router_ip_address(), "to": ip_address}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200185 vrouter_obj.set_virtual_router_ip_address(ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200186 if vrouter_obj.get_virtual_router_type() != router_type:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200187 changes['router_type'] = {"from": vrouter_obj.get_virtual_router_type(), "to": router_type}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200188 vrouter_obj.set_virtual_router_type(router_type)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200189 if vrouter_obj.get_virtual_router_dpdk_enabled() != dpdk_enabled:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200190 changes['dpdk_enabled'] = {"from": vrouter_obj.get_virtual_router_dpdk_enabled(), "to": dpdk_enabled}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200191 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200192 if len(changes) != 0:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200193 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200194 ret['result'] = None
195 ret['comment'] = "Virtual router " + name + " will be updated"
196 else:
197 ret['comment'] = "VirtualRouter " + name + " has been updated"
198 ret['changes'] = changes
199 vnc_client.virtual_router_update(vrouter_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200200 return ret
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200201 ret['comment'] = 'Virtual router ' + name + ' already exists and is updated'
202 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100203 else:
204 vrouter_obj = VirtualRouter(
205 name, gsc_obj,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200206 virtual_router_ip_address=ip_address,
207 virtual_router_type=router_type)
Ales Komarekad46d2e2017-03-09 17:16:38 +0100208 vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200209 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200210 ret['result'] = None
211 ret['comment'] = "VirtualRouter " + name + " will be created"
212 else:
213 vnc_client.virtual_router_create(vrouter_obj)
214 ret['comment'] = "VirtualRouter " + name + " has been created"
215 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
216 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100217
218
219def virtual_router_delete(name, **kwargs):
220 '''
221 Delete specific Contrail virtual router
222
223 CLI Example:
224
225 .. code-block:: bash
226
227 salt '*' contrail.virtual_router_delete cmp01
228 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200229 ret = {'name': name,
230 'changes': {},
231 'result': True,
232 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100233 vnc_client = _auth(**kwargs)
234 gsc_obj = _get_config(vnc_client)
235 vrouter_obj = VirtualRouter(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200236 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200237 ret['result'] = None
238 ret['comment'] = "VirtualRouter " + name + " will be deleted"
239 else:
240 vnc_client.virtual_router_delete(fq_name=vrouter_obj.get_fq_name())
241 ret['comment'] = "VirtualRouter " + name + " has been deleted"
242 ret['changes'] = {'VirtualRouter': {'old': name, 'new': ''}}
243 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200244
245
246def physical_router_list(**kwargs):
247 '''
248 Return a list of all Contrail physical routers
249
250 CLI Example:
251
252 .. code-block:: bash
253
254 salt '*' contrail.physical_router_list
255 '''
256 ret = {}
257 vnc_client = _auth(**kwargs)
258 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
259 for prouter_obj in prouter_objs:
260 ret[prouter_obj.name] = {
261 'uuid': prouter_obj._uuid,
262 'management_ip': prouter_obj._physical_router_management_ip,
263 'product_name': prouter_obj._physical_router_product_name,
264 }
265
266 return ret
267
268
269def physical_router_get(name, **kwargs):
270 '''
271 Return a specific Contrail physical router
272
273 CLI Example:
274
275 .. code-block:: bash
276
277 salt '*' contrail.physical_router_get router_name
278 '''
279 ret = {}
280 vnc_client = _auth(**kwargs)
281 prouter_objs = vnc_client._objects_list('physical-router', detail=True)
282 for prouter_obj in prouter_objs:
283 if name == prouter_obj.name:
284 ret[name] = prouter_obj.__dict__
285 if len(ret) == 0:
286 return {'Error': 'Error in retrieving physical router.'}
287 return ret
288
289
290def physical_router_create(name, parent_type=None,
291 management_ip=None,
292 dataplane_ip=None, # VTEP address in web GUI
293 vendor_name=None,
294 product_name=None,
295 vnc_managed=None,
296 junos_service_ports=None,
297 agents=None, **kwargs):
298 '''
299 Create specific Contrail physical router
300
301 CLI Example:
302
303 .. code-block:: bash
304
305 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']"
306 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200307 ret = {'name': name,
308 'changes': {},
309 'result': True,
310 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200311 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200312 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200313 prouter_objs = physical_router_list(**kwargs)
314 if name in prouter_objs:
315 prouter = physical_router_get(name)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200316 changes = {}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200317 prouter_obj = vnc_client._object_read('physical-router', id=prouter[name]['_uuid'])
318 if prouter_obj.physical_router_management_ip != management_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200319 changes['management_ip'] = {'from': prouter_obj.physical_router_management_ip, "to": management_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200320 prouter_obj.set_physical_router_management_ip(management_ip)
321 if prouter_obj.physical_router_dataplane_ip != dataplane_ip:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200322 changes['dataplane_ip'] = {'from': prouter_obj.physical_router_dataplane_ip, "to": dataplane_ip}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200323 prouter_obj.set_physical_router_dataplane_ip(dataplane_ip)
324 if prouter_obj.get_physical_router_vendor_name() != vendor_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200325 changes['vendor_name'] = {'from': prouter_obj.get_physical_router_vendor_name(), "to": vendor_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200326 prouter_obj.set_physical_router_vendor_name(vendor_name)
327 if prouter_obj.get_physical_router_product_name() != product_name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200328 changes['product_name'] = {'from': prouter_obj.get_physical_router_product_name(), "to": product_name}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200329 prouter_obj.set_physical_router_product_name(product_name)
330 if prouter_obj.get_physical_router_vnc_managed() != vnc_managed:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200331 changes['vnc_managed'] = {'from': prouter_obj.get_physical_router_vnc_managed(), "to": vnc_managed}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200332 prouter_obj.set_physical_router_vnc_managed(vnc_managed)
333 if prouter_obj.get_physical_router_junos_service_ports() != junos_service_ports:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200334 changes['junos_service_ports'] = {'from': prouter_obj.get_physical_router_junos_service_ports(),
335 'to': junos_service_ports}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200336 prouter_obj.set_physical_router_junos_service_ports(junos_service_ports)
337
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200338 if len(changes) != 0:
339 if __opts__['test']:
340 ret['result'] = None
341 ret['comment'] = "Physical router " + name + " will be updated"
342 else:
343 ret['comment'] = 'Physical router ' + name + ' already exists and is updated'
344 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200345
346 vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) # all vrouter objects
347 c_agents = [] # referenced vrouters
348 for c_agent in prouter_obj.get_virtual_router_refs():
349 c_agents.append(c_agent['uuid'])
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200350 # agent_objs = [] # required state of references
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200351 for vrouter_obj in vrouter_objs:
352 if vrouter_obj._display_name in agents and vrouter_obj._uuid not in c_agents:
353 prouter_obj.add_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200354 changes['vrouter ' + vrouter_obj._display_name] = "Reference added"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200355 if vrouter_obj._display_name not in agents and vrouter_obj._uuid in c_agents:
356 prouter_obj.del_virtual_router(vrouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200357 changes['vrouter ' + vrouter_obj._display_name] = "Reference removed"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200358 vnc_client.physical_router_update(prouter_obj)
359
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200360 if __opts__['test']:
361 ret['result'] = None
362 ret['comment'] = "VirtualRouter " + name + " will be created"
363 else:
364 vnc_client.virtual_router_create(vrouter_obj)
365 ret['comment'] = "VirtualRouter " + name + " has been created"
366 ret['changes'] = {'VirtualRouter': {'old': '', 'new': name}}
367
368 if len(changes) == 0:
369 ret['comment'] = "Physical router exists and is updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200370 return ret
371 else:
372 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200373 ret['result'] = None
374 ret['comment'] = "Physical router " + name + " will be created"
375 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200376 prouter_obj = PhysicalRouter(
377 name=name,
378 parent_obj=None,
379 physical_router_management_ip=management_ip,
380 physical_router_dataplane_ip=dataplane_ip,
381 physical_router_vendor_name=vendor_name,
382 physical_router_product_name=product_name,
383 physical_router_vnc_managed=vnc_managed,
384 physical_router_junos_service_ports=junos_service_ports,
385 )
386 for agent in agents:
387 vrouter = virtual_router_get(agent)
388 vrouter_obj = vnc_client._object_read('virtual-router', id=vrouter[agent]['uuid'])
389 prouter_obj.add_virtual_router(vrouter_obj)
390 vnc_client.physical_router_create(prouter_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200391 ret['comment'] = "Physical router " + name + " has been created"
392 ret['changes'] = {'PhysicalRouter': {'old': '', 'new': name}}
393 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200394
395
396def physical_router_delete(name, **kwargs):
397 '''
398 Delete specific Contrail physical router
399
400 CLI Example:
401
402 .. code-block:: bash
403
404 salt '*' contrail.physical_router_delete router_name
405 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200406 ret = {'name': name,
407 'changes': {},
408 'result': True,
409 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200410 vnc_client = _auth(**kwargs)
411 gsc_obj = _get_config(vnc_client)
412 prouter_obj = PhysicalRouter(name, gsc_obj)
413 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200414 ret['result'] = None
415 ret['comment'] = "Physical router " + name + " will be deleted"
416 else:
417 vnc_client.physical_router_delete(fq_name=prouter_obj.get_fq_name())
418 ret['comment'] = "Physical router " + name + " has been deleted"
419 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
420 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200421
422
423def physical_interface_list(**kwargs):
424 '''
425 Return a list of all Contrail physical interface
426
427 CLI Example:
428
429 .. code-block:: bash
430
431 salt '*' contrail.physical_interface_list
432 '''
433 ret = {}
434 vnc_client = _auth(**kwargs)
435 pinterface_objs = vnc_client._objects_list('physical-interface', detail=True)
436 for pinterface_obj in pinterface_objs:
437 ret[pinterface_obj.name] = {
438 'uuid': pinterface_obj._uuid,
439 'fq_name': pinterface_obj.fq_name,
440 'parent_type': pinterface_obj.parent_type,
441 }
442
443 return ret
444
445
446def physical_interface_get(name, physical_router, **kwargs):
447 '''
448 Return a specific Contrail physical interface
449
450 CLI Example:
451
452 .. code-block:: bash
453
454 salt '*' contrail.physical_interface_get interface_name physical_router_name
455 '''
456 ret = {}
457 vnc_client = _auth(**kwargs)
458 pinterf_objs = vnc_client._objects_list('physical-interface', detail=True)
459 for pinterf_obj in pinterf_objs:
460 if name == pinterf_obj.name and physical_router in pinterf_obj.fq_name:
461 ret[name] = pinterf_obj.__dict__
462 if len(ret) == 0:
463 return {'Error': 'Error in retrieving physical interface.'}
464 return ret
465
466
467def physical_interface_create(name, physical_router, **kwargs):
468 '''
469 Create specific Contrail physical interface
470
471 CLI Example:
472
473 .. code-block:: bash
474
475 salt '*' contrail.physical_interface_create ge-0/0/10 physical_router_name
476 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200477 ret = {'name': name,
478 'changes': {},
479 'result': True,
480 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200481 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200482 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200483 pinterf_obj = physical_interface_get(name, physical_router, **kwargs)
484 if 'Error' not in pinterf_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200485 ret['comment'] = 'Physical interface ' + name + ' on ' + physical_router + ' already exists'
486 return ret
487
488 if __opts__['test']:
489 ret['result'] = None
490 ret['comment'] = "Physical interface " + name + " will be created"
491 return ret
492
493 prouter = physical_router_get(physical_router)
494 prouter_obj = vnc_client._object_read('physical-router', id=prouter[physical_router]['_uuid'])
495 pinterf_obj = PhysicalInterface(name, prouter_obj)
496 vnc_client.physical_interface_create(pinterf_obj)
497 ret['comment'] = "Physical interface " + name + " has been created"
498 ret['changes'] = {'Physical interface': {'old': '', 'new': name}}
499 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200500
501
502def physical_interface_delete(name, physical_router, **kwargs):
503 '''
504 Delete specific Contrail physical interface
505
506 CLI Example:
507 .. code-block:: bash
508
509 salt '*' contrail.physical_interface_delete ge-0/0/0 phr01
510 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200511 ret = {'name': name,
512 'changes': {},
513 'result': True,
514 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200515 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200516 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200517 piface = physical_interface_get(name, physical_router)
518 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200519 ret['result'] = None
520 ret['comment'] = "Physical interface " + name + " will be deleted"
521 else:
522 vnc_client.physical_interface_delete(id=piface[name]['_uuid'])
523 ret['comment'] = "Physical router " + name + " has been deleted"
524 ret['changes'] = {'Physical router': {'old': name, 'new': ''}}
525 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200526
527
528def logical_interface_list(**kwargs):
529 '''
530 Return a list of all Contrail logical interfaces
531
532 CLI Example:
533
534 .. code-block:: bash
535
536 salt '*' contrail.logical_interface_list
537 '''
538 ret = []
539 vnc_client = _auth(**kwargs)
540 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
541 for liface_obj in liface_objs:
542 ret.append({
543 'name': liface_obj.name,
544 'uuid': liface_obj._uuid,
545 'fq_name': liface_obj.fq_name,
546 'parent_type': liface_obj.parent_type,
547 })
548 return ret
549
550
551def logical_interface_get(name, parent_names, parent_type=None, **kwargs):
552 '''
553 Return a specific Contrail logical interface
554
555 CLI Example:
556
557 .. code-block:: bash
558
559 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01']
560 or
561 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['ge-0/0/0','phr01']
562 or
563 salt '*' contrail.logical_interface_get ge-0/0/0.10 ['phr01'] parent_type=physcal-interface
564 '''
565 ret = {}
566 vnc_client = _auth(**kwargs)
567 liface_objs = vnc_client._objects_list('logical-interface', detail=True)
568 count = 0
569 for liface_obj in liface_objs:
570 if name == liface_obj.name and set(parent_names).issubset(liface_obj.fq_name):
571 if parent_type and parent_type == liface_obj.parent_type:
572 count += 1
573 ret[liface_obj.name] = liface_obj.__dict__
574 if not parent_type:
575 count += 1
576 ret[liface_obj.name] = liface_obj.__dict__
577 if len(ret) == 0:
578 return {'Error': 'Error in retrieving logical interface.'}
579 if count > 1:
580 return {
581 'Error': 'Error Was found more then one logical interface. Please put more parent_name or put parent_type to chose one of them.'}
582 return ret
583
584
585def logical_interface_create(name, parent_names, parent_type='physical-interface', vlan_tag=None, interface_type="l2",
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300586 vmis=None, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200587 '''
588 Create specific Contrail logical interface
589
590 CLI Example:
591
592 .. code-block:: bash
593
594 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
595 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200596 ret = {'name': name,
597 'changes': {},
598 'result': True,
599 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200600 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200601 # gsc_obj = _get_config(vnc_client)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300602
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200603 liface_obj = logical_interface_get(name, parent_names, parent_type, **kwargs)
604 if 'Error' not in liface_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200605 ret['comment'] = 'Logical interface ' + name + ' already exists'
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200606 else:
607 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200608 ret['result'] = None
609 ret['comment'] = "Logical interface " + name + " will be created"
610 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200611 parent_obj = None
612 for router in parent_names:
613 parent_router = physical_router_get(router)
614 if 'Error' not in parent_router:
615 parent_obj = vnc_client._object_read('physical-router', id=parent_router[router]['_uuid'])
616 break
617 if not parent_obj:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200618 ret['result'] = False
619 ret['comment'] = 'Physical router have to be defined'
620 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200621 if parent_type == 'physical-interface':
622 for interface in parent_names:
623 parent_interface = physical_interface_get(interface, parent_obj.name)
624 if 'Error' not in parent_interface:
625 parent_obj = vnc_client._object_read('physical-interface', id=parent_interface[interface]['_uuid'])
626 break
627 if interface_type.lower() == "l3":
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200628 ret['result'] = False
629 ret['comment'] = "Virtual Network have to be defined for L3 interface type"
630 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200631
632 liface_obj = LogicalInterface(name, parent_obj, vlan_tag, interface_type.lower())
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300633
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200634 if vmis:
635 for vmi_name, vmi in vmis.iteritems():
636 vmi = vnc_client.virtual_machine_interface_read(
637 fq_name=_get_fq_name(vnc_client, resource_name=vmi_name,
638 project_name=kwargs.get('tenant', 'admin')))
639 liface_obj.add_virtual_machine_interface(vmi)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200640 vnc_client.logical_interface_create(liface_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +0300641
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200642 ret['comment'] = "Logical interface " + name + " has been created"
643 ret['changes'] = {'Logical interface': {'old': '', 'new': name}}
644 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200645
646
647def logical_interface_delete(name, parent_names, parent_type=None, **kwargs):
648 '''
649 Delete specific Contrail logical interface
650
651 CLI Example:
652
653 .. code-block:: bash
654
655 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['ge-0/0/0','phr01']
656 or
657 salt '*' contrail.logical_interface_delete ge-0/0/0.12 ['phr01'] parent_type=physical-router
658
659 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200660 ret = {'name': name,
661 'changes': {},
662 'result': True,
663 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200664 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200665 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200666 liface = logical_interface_get(name, parent_names, parent_type)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200667
668 if __opts__['test']:
669 ret['result'] = None
670 ret['comment'] = "Logical interface " + name + " will be deleted"
671 return ret
672 vnc_client.logical_interface_delete(id=liface[name]['_uuid'])
673 ret['comment'] = "Logical interface " + name + " has been deleted"
674 ret['changes'] = {'LogicalInterface ': {'old': name, 'new': ''}}
675 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200676
677
678def global_vrouter_config_list(**kwargs):
679 '''
680 Return a list of all Contrail global vrouter configs
681
682 CLI Example:
683
684 .. code-block:: bash"
685
686 salt '*' global_vrouter_config_list
687 '''
688 ret = {}
689 vnc_client = _auth(**kwargs)
690 vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
691 for vrouter_conf_obj in vrouter_conf_objs:
692 ret[vrouter_conf_obj._display_name] = vrouter_conf_obj.__dict__
693 return ret
694
695
696def global_vrouter_config_get(name, **kwargs):
697 '''
698 Return a specific Contrail global vrouter config
699
700 CLI Example:
701
702 .. code-block:: bash
703
704 salt '*' contrail.global_vrouter_get global-vrouter-config
705 '''
706 ret = {}
707 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
708 if name in vrouter_conf_objs:
709 ret[name] = vrouter_conf_objs.get(name)
710 if len(ret) == 0:
711 return {'Error': 'Error in retrieving global vrouter config.'}
712 return ret
713
714
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100715def global_vrouter_config_create(name, parent_type, encap_priority, vxlan_vn_id_mode, flow_export_rate, *fq_names, **kwargs):
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200716 '''
717 Create specific Contrail global vrouter config
718
719 CLI Example:
720
721 .. code-block:: bash
722
723 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']"
724 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200725 ret = {'name': name,
726 'changes': {},
727 'result': True,
728 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200729 vnc_client = _auth(**kwargs)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200730 # gsc_obj = _get_config(vnc_client)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200731 vrouter_conf_objs = global_vrouter_config_list(**kwargs)
732 if name in vrouter_conf_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200733 ret['comment'] = 'Global vrouter config ' + name + ' already exists'
734 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200735 else:
736 vrouter_conf_obj = GlobalVrouterConfig(
737 name=name,
738 parent_obj=None,
739 encapsulation_priorities=EncapsulationPrioritiesType(encapsulation=encap_priority.split(",")),
740 fq_name=fq_names,
741 vxlan_network_identifier_mode=vxlan_vn_id_mode,
742 parent_type=parent_type,
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +0100743 flow_export_rate=flow_export_rate,
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200744 )
745 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200746 ret['result'] = None
747 ret['comment'] = "Global vRouter config " + name + " will be created"
748 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200749 vnc_client.global_vrouter_config_create(vrouter_conf_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200750 ret['comment'] = "Global vRouter config " + name + " has been created"
751 ret['changes'] = {'Global vRouter config': {'old': '', 'new': name}}
752 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200753
754
755def global_vrouter_config_delete(name, **kwargs):
756 '''
757 Delete specific Contrail global vrouter config
758
759 CLI Example:
760
761 .. code-block:: bash
762
763 salt '*' contrail.global_vrouter_config_delete global-vrouter-config
764 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200765 ret = {'name': name,
766 'changes': {},
767 'result': True,
768 'comment': ''}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200769 vnc_client = _auth(**kwargs)
770 gsc_obj = _get_config(vnc_client)
771 vrouter_conf_obj = GlobalVrouterConfig(name, gsc_obj)
772 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200773 ret['result'] = None
774 ret['comment'] = "Global vRouter config " + name + " will be deleted"
775 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200776 vnc_client.global_vrouter_config_delete(
777 fq_name=vrouter_conf_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200778 ret['comment'] = "Global vRouter config " + name + " has been deleted"
779 ret['changes'] = {'Global vRouter config': {'old': name, 'new': ''}}
780 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100781
782
783def analytics_node_list(**kwargs):
784 '''
785 Return a list of all Contrail analytics nodes
786
787 CLI Example:
788
789 .. code-block:: bash
790
791 salt '*' contrail.analytics_node_list
792 '''
793 ret = {}
794 vnc_client = _auth(**kwargs)
795 node_objs = vnc_client._objects_list('analytics-node', detail=True)
796 for node_obj in node_objs:
797 ret[node_obj.name] = node_obj.__dict__
798 return ret
799
800
801def analytics_node_get(name, **kwargs):
802 '''
803 Return a specific Contrail analytics node
804
805 CLI Example:
806
807 .. code-block:: bash
808
809 salt '*' contrail.analytics_node_get nal01
810 '''
811 ret = {}
812 vrouter_objs = analytics_node_list(**kwargs)
813 if name in vrouter_objs:
814 ret[name] = vrouter_objs.get(name)
815 if len(ret) == 0:
816 return {'Error': 'Error in retrieving analytics node.'}
817 return ret
818
819
820def analytics_node_create(name, ip_address, **kwargs):
821 '''
822 Create specific Contrail analytics node
823
824 CLI Example:
825
826 .. code-block:: bash
827
828 salt '*' contrail.analytics_node_create ntw03 10.10.10.103
829 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200830
831 ret = {'name': name,
832 'changes': {},
833 'result': True,
834 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100835 vnc_client = _auth(**kwargs)
836 gsc_obj = _get_config(vnc_client)
837 analytics_node_objs = analytics_node_list(**kwargs)
838 if name in analytics_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200839 ret['comment'] = 'Analytics node %s already exists'
840 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100841 else:
842 analytics_node_obj = AnalyticsNode(
843 name, gsc_obj,
844 analytics_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200845 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200846 ret['result'] = None
847 ret['comment'] = "AnalyticsNode " + name + " will be created"
848 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100849 vnc_client.analytics_node_create(analytics_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200850 ret['comment'] = "AnalyticsNode " + name + " has been created"
851 ret['changes'] = {'Analytics Node': {'old': '', 'new': name}}
852 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100853
854
855def analytics_node_delete(name, **kwargs):
856 '''
857 Delete specific Contrail analytics node
858
859 CLI Example:
860
861 .. code-block:: bash
862
863 salt '*' contrail.analytics_node_delete cmp01
864 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200865 ret = {'name': name,
866 'changes': {},
867 'result': True,
868 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100869 vnc_client = _auth(**kwargs)
870 gsc_obj = _get_config(vnc_client)
871 analytics_node_obj = AnalyticsNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200872 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200873 ret['result'] = None
874 ret['comment'] = "AnalyticsNode " + name + " will be deleted"
875 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100876 vnc_client.analytics_node_delete(
877 fq_name=analytics_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200878 ret['comment'] = "AnalyticsNode " + name + " has been deleted"
879 ret['changes'] = {'Analytics Node': {'old': name, 'new': ''}}
880 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100881
882
883def config_node_list(**kwargs):
884 '''
885 Return a list of all Contrail config nodes
886
887 CLI Example:
888
889 .. code-block:: bash
890
891 salt '*' contrail.config_node_list
892 '''
893 ret = {}
894 vnc_client = _auth(**kwargs)
895 node_objs = vnc_client._objects_list('config-node', detail=True)
896 for node_obj in node_objs:
897 ret[node_obj.name] = node_obj.__dict__
898 return ret
899
900
901def config_node_get(name, **kwargs):
902 '''
903 Return a specific Contrail config node
904
905 CLI Example:
906
907 .. code-block:: bash
908
909 salt '*' contrail.config_node_get nal01
910 '''
911 ret = {}
912 vrouter_objs = config_node_list(**kwargs)
913 if name in vrouter_objs:
914 ret[name] = vrouter_objs.get(name)
915 if len(ret) == 0:
916 return {'Error': 'Error in retrieving config node.'}
917 return ret
918
919
920def config_node_create(name, ip_address, **kwargs):
921 '''
922 Create specific Contrail config node
923
924 CLI Example:
925
926 .. code-block:: bash
927
928 salt '*' contrail.config_node_create ntw03 10.10.10.103
929 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200930 ret = {'name': name,
931 'changes': {},
932 'result': True,
933 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100934 vnc_client = _auth(**kwargs)
935 gsc_obj = _get_config(vnc_client)
936 config_node_objs = config_node_list(**kwargs)
937 if name in config_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200938 ret['comment'] = 'Config node ' + name + ' already exists'
939 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100940 else:
941 config_node_obj = ConfigNode(
942 name, gsc_obj,
943 config_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200944 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200945 ret['comment'] = "ConfigNode " + name + " will be created"
946 ret['result'] = None
947 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100948 vnc_client.config_node_create(config_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200949 ret['comment'] = "ConfigNode " + name + " has been created"
950 ret['changes'] = {'ConfigNode': {'old': '', 'new': name}}
951 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100952
953
954def config_node_delete(name, **kwargs):
955 '''
956 Delete specific Contrail config node
957
958 CLI Example:
959
960 .. code-block:: bash
961
962 salt '*' contrail.config_node_delete cmp01
963 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200964 ret = {'name': name,
965 'changes': {},
966 'result': True,
967 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +0100968 vnc_client = _auth(**kwargs)
969 gsc_obj = _get_config(vnc_client)
970 config_node_obj = ConfigNode(name, gsc_obj)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +0200971 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200972 ret['comment'] = "ConfigNode " + name + " will be deleted"
973 ret['result'] = None
974 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100975 vnc_client.config_node_delete(
976 fq_name=config_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +0200977 ret['comment'] = "ConfigNode " + name + " has been deleted"
978 ret['changes'] = {'ConfigNode': {'old': name, 'new': ''}}
979 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +0100980
981
982def bgp_router_list(**kwargs):
983 '''
984 Return a list of all Contrail BGP routers
985
986 CLI Example:
987
988 .. code-block:: bash
989
990 salt '*' contrail.bgp_router_list
991 '''
992 ret = {}
993 vnc_client = _auth(**kwargs)
994 bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True)
995 for bgp_router_obj in bgp_router_objs:
996 ret[bgp_router_obj.name] = bgp_router_obj.__dict__
997 return ret
998
999
1000def bgp_router_get(name, **kwargs):
1001 '''
1002 Return a specific Contrail BGP router
1003
1004 CLI Example:
1005
1006 .. code-block:: bash
1007
1008 salt '*' contrail.bgp_router_get nal01
1009 '''
1010 ret = {}
1011 bgp_router_objs = bgp_router_list(**kwargs)
1012 if name in bgp_router_objs:
1013 ret[name] = bgp_router_objs.get(name)
1014 if len(ret) == 0:
1015 return {'Error': 'Error in retrieving BGP router.'}
1016 return ret
1017
1018
Marek Celoud3097e5b2018-01-09 13:52:14 +01001019def bgp_router_create(name, type, ip_address, asn=64512, key_type=None, key=None, **kwargs):
Ales Komarekad46d2e2017-03-09 17:16:38 +01001020 '''
1021 Create specific Contrail control node
1022
1023 CLI Example:
1024
1025 .. code-block:: bash
1026
1027 salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103
1028 salt '*' contrail.bgp_router_create mx01 router 10.10.10.105
1029 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001030 ret = {'name': name,
1031 'changes': {},
1032 'result': True,
1033 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001034 vnc_client = _auth(**kwargs)
1035
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001036 address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn',
1037 'inet6-vpn']
1038 if type != 'control-node':
1039 address_families.remove('erm-vpn')
1040
Marek Celoud3097e5b2018-01-09 13:52:14 +01001041 key_type = None if key_type == 'None' else key_type
1042 key = None if key == 'None' else key
1043
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001044 bgp_addr_fams = AddressFamilies(address_families)
1045 bgp_sess_attrs = [
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001046 BgpSessionAttributes(address_families=bgp_addr_fams)]
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001047 bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)]
1048 bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions)
1049 rt_inst_obj = _get_rt_inst_obj(vnc_client)
1050
Marek Celoud3097e5b2018-01-09 13:52:14 +01001051 bgp_auth_data = None
1052
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001053 if type == 'control-node':
1054 vendor = 'contrail'
1055 elif type == 'router':
1056 vendor = 'mx'
Marek Celoud3097e5b2018-01-09 13:52:14 +01001057 if key_type == 'md5':
1058 key_id = 0
1059 key_items = AuthenticationKeyItem(key_id, key)
1060 bgp_auth_data = AuthenticationData(key_type, [key_items])
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001061 else:
1062 vendor = 'unknown'
1063
1064 router_params = BgpRouterParams(router_type=type,
1065 vendor=vendor, autonomous_system=int(asn),
1066 identifier=_get_ip(ip_address),
1067 address=_get_ip(ip_address),
Marek Celoud3097e5b2018-01-09 13:52:14 +01001068 port=179, address_families=bgp_addr_fams,
1069 auth_data=bgp_auth_data)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001070
Ales Komarekad46d2e2017-03-09 17:16:38 +01001071 bgp_router_objs = bgp_router_list(**kwargs)
1072 if name in bgp_router_objs:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001073 bgp_router_obj = vnc_client._object_read('bgp-router', id=bgp_router_objs[name]['_uuid'])
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001074
1075 if bgp_router_obj.bgp_router_parameters.autonomous_system != asn:
1076 ret['changes'].update({"autonomous_system": {'old': bgp_router_obj.bgp_router_parameters.autonomous_system, 'new': asn}})
1077 if bgp_router_obj.bgp_router_parameters.vendor != vendor:
1078 ret['changes'].update({"vendor": {'old': bgp_router_obj.bgp_router_parameters.vendor, 'new': vendor}})
1079 if bgp_router_obj.bgp_router_parameters.address != ip_address:
1080 ret['changes'].update({"ip_address": {'old': bgp_router_obj.bgp_router_parameters.address, 'new': ip_address}})
Marek Celoud3097e5b2018-01-09 13:52:14 +01001081 try:
1082 if bgp_router_obj.bgp_router_parameters.auth_data.key_type != key_type:
1083 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_type, 'new': key_type}})
1084 except:
1085 if key_type != None:
1086 ret['changes'].update({"key_type": {'old': None, 'new': key_type}})
1087 if key_type == 'md5':
1088 try:
1089 if bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key != key:
1090 ret['changes'].update({"key_type": {'old': bgp_router_obj.bgp_router_parameters.auth_data.key_items[0].key, 'new': key}})
1091 except:
1092 ret['changes'].update({"key_type": {'old': None, 'new': key}})
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001093
1094 if len(ret['changes']) == 0:
1095 return ret
1096
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001097 bgp_router_obj.set_bgp_router_parameters(router_params)
1098 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001099 ret['result'] = None
1100 ret['comment'] = "BGP router " + name + " will be updated"
1101 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001102 vnc_client.bgp_router_update(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001103 ret['comment'] = "BGP router " + name + " has been updated"
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001104 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001105 else:
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001106 bgp_router_obj = BgpRouter(name, rt_inst_obj, bgp_router_parameters=router_params)
1107 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001108 ret['result'] = None
1109 ret['comment'] = "BGP router " + name + " will be created"
1110 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001111 vnc_client.bgp_router_create(bgp_router_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001112 ret['comment'] = "BGP router " + name + " has been created"
1113 ret['changes'] = {'BGP router': {'old': name, 'new': ''}}
1114 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001115
1116
1117def bgp_router_delete(name, **kwargs):
1118 '''
1119 Delete specific Contrail control node
1120
1121 CLI Example:
1122
1123 .. code-block:: bash
1124
1125 salt '*' contrail.bgp_router_delete mx01
1126 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001127 ret = {'name': name,
1128 'changes': {},
1129 'result': True,
1130 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001131 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001132
1133 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001134 ret['result'] = None
1135 ret['comment'] = "BGP router " + name + " will be deleted"
1136 return ret
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001137
1138 bgp_router = bgp_router_get(name)
1139 if name in bgp_router:
1140 vnc_client.bgp_router_delete(fq_name=bgp_router[name]['fq_name'])
1141 ret['comment'] = "BGP router " + name + " has been deleted"
1142 ret['changes'] = {'BGP router': {'old': '', 'new': name}}
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001143 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001144
Ales Komarekad46d2e2017-03-09 17:16:38 +01001145
1146def database_node_list(**kwargs):
1147 '''
1148 Return a list of all Contrail database nodes
1149
1150 CLI Example:
1151
1152 .. code-block:: bash
1153
1154 salt '*' contrail.database_node_list
1155 '''
1156 ret = {}
1157 vnc_client = _auth(**kwargs)
1158 node_objs = vnc_client._objects_list('database-node', detail=True)
1159 for node_obj in node_objs:
1160 ret[node_obj.name] = node_obj.__dict__
1161 return ret
1162
1163
1164def database_node_get(name, **kwargs):
1165 '''
1166 Return a specific Contrail database node
1167
1168 CLI Example:
1169
1170 .. code-block:: bash
1171
1172 salt '*' contrail.database_node_get nal01
1173 '''
1174 ret = {}
1175 vrouter_objs = database_node_list(**kwargs)
1176 if name in vrouter_objs:
1177 ret[name] = vrouter_objs.get(name)
1178 if len(ret) == 0:
1179 return {'Error': 'Error in retrieving database node.'}
1180 return ret
1181
1182
1183def database_node_create(name, ip_address, **kwargs):
1184 '''
1185 Create specific Contrail database node
1186
1187 CLI Example:
1188
1189 .. code-block:: bash
1190
1191 salt '*' contrail.database_node_create ntw03 10.10.10.103
1192 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001193 ret = {'name': name,
1194 'changes': {},
1195 'result': True,
1196 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001197 vnc_client = _auth(**kwargs)
1198 gsc_obj = _get_config(vnc_client)
1199 database_node_objs = database_node_list(**kwargs)
1200 if name in database_node_objs:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001201 ret['comment'] = 'Database node ' + name + ' already exists'
1202 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001203 else:
1204 database_node_obj = DatabaseNode(
1205 name, gsc_obj,
1206 database_node_ip_address=ip_address)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001207 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001208 ret['result'] = None
1209 ret['comment'] = "DatabaseNode " + name + " will be created"
1210 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001211 vnc_client.database_node_create(database_node_obj)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001212 ret['comment'] = "DatabaseNode " + name + " has been created"
1213 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1214 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001215
1216
1217def database_node_delete(name, **kwargs):
1218 '''
1219 Delete specific Contrail database node
1220
1221 CLI Example:
1222
1223 .. code-block:: bash
1224
1225 salt '*' contrail.database_node_delete cmp01
1226 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001227 ret = {'name': name,
1228 'changes': {},
1229 'result': True,
1230 'comment': ''}
Ales Komarekad46d2e2017-03-09 17:16:38 +01001231 vnc_client = _auth(**kwargs)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001232 gsc_obj = _get_config(vnc_client)
1233 database_node_obj = DatabaseNode(name, gsc_obj)
1234 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001235 ret['result'] = None
1236 ret['comment'] = "DatabaseNode " + name + " will be deleted"
1237 return ret
Ales Komarekad46d2e2017-03-09 17:16:38 +01001238 vnc_client.database_node_delete(
1239 fq_name=database_node_obj.get_fq_name())
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001240 ret['comment'] = "DatabaseNode " + name + " has been deleted"
1241 ret['changes'] = {'DatabaseNode': {'old': '', 'new': name}}
1242 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001243
1244
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001245def _get_vrouter_config(vnc_client, gvc_name=None):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001246 try:
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001247 if not gvc_name:
1248 gvc_list = global_vrouter_config_list()
1249 gvc_name = gvc_list.values()[0]['name']
1250
Petr Jediný5f3efe32017-05-26 17:55:09 +02001251 config = vnc_client.global_vrouter_config_read(
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001252 fq_name=['default-global-system-config', gvc_name])
Petr Jediný5f3efe32017-05-26 17:55:09 +02001253 except Exception:
1254 config = None
1255
1256 return config
1257
1258
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001259def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001260 '''
1261 Return a list of all Contrail link local services
1262
1263 CLI Example:
1264
1265 .. code-block:: bash
1266
1267 salt '*' contrail.linklocal_service_list
1268 '''
1269 ret = {}
1270 vnc_client = _auth(**kwargs)
1271
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001272 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001273 if current_config is None:
1274 return ret
1275
1276 service_list_res = current_config.get_linklocal_services()
1277 if service_list_res is None:
1278 service_list_obj = {'linklocal_service_entry': []}
1279 else:
1280 service_list_obj = service_list_res.__dict__
1281 for _, value in service_list_obj.iteritems():
1282 for entry in value:
1283 service = entry.__dict__
1284 if 'linklocal_service_name' in service:
1285 ret[service['linklocal_service_name']] = service
1286 return ret
1287
1288
1289def linklocal_service_get(name, **kwargs):
1290 '''
1291 Return a specific Contrail link local service
1292
1293 CLI Example:
1294
1295 .. code-block:: bash
1296
1297 salt '*' contrail.linklocal_service_get llservice
1298 '''
1299 ret = {}
1300 services = linklocal_service_list(**kwargs)
1301 if name in services:
1302 ret[name] = services.get(name)
1303 if len(ret) == 0:
1304 return {'Error': 'Error in retrieving link local service "{0}"'.format(name)}
1305 return ret
1306
1307
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001308def linklocal_service_create(name, lls_ip, lls_port, ipf_dns_or_ip, ipf_port, global_vrouter_config_name=None, **kwargs):
Petr Jediný5f3efe32017-05-26 17:55:09 +02001309 '''
1310 Create specific Contrail link local service
1311
1312 CLI Example:
1313
1314 .. code-block:: bash
1315
1316 salt '*' contrail.linklocal_service_create \
1317 llservice 10.10.10.103 22 '["20.20.20.20", "30.30.30.30"]' 22
1318 salt '*' contrail.linklocal_service_create \
1319 llservice 10.10.10.103 22 link-local.service.dns-name 22
1320 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001321 ret = {'name': name,
1322 'changes': {},
1323 'result': True,
1324 'comment': ''}
Petr Jediný5f3efe32017-05-26 17:55:09 +02001325 vnc_client = _auth(**kwargs)
Pavel Svimbersky2fe0c272017-11-30 14:58:16 +01001326 current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001327 service_entry = LinklocalServiceEntryType(
1328 linklocal_service_name=name,
1329 linklocal_service_ip=lls_ip,
1330 linklocal_service_port=lls_port,
1331 ip_fabric_service_port=ipf_port)
1332 if isinstance(ipf_dns_or_ip, basestring):
1333 service_entry.ip_fabric_DNS_service_name = ipf_dns_or_ip
1334 elif isinstance(ipf_dns_or_ip, list):
1335 service_entry.ip_fabric_service_ip = ipf_dns_or_ip
1336 service_entry.ip_fabric_DNS_service_name = ''
1337
1338 if current_config is None:
1339 new_services = LinklocalServicesTypes([service_entry])
1340 new_config = GlobalVrouterConfig(linklocal_services=new_services)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001341 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001342 ret['result'] = None
1343 ret['comment'] = "Link local service " + name + " will be created"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001344 else:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001345 ret['comment'] = "Link local service " + name + " has been created"
1346 ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001347 vnc_client.global_vrouter_config_create(new_config)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001348 else:
1349 _current_service_list = current_config.get_linklocal_services()
1350 if _current_service_list is None:
1351 service_list = {'linklocal_service_entry': []}
1352 else:
1353 service_list = _current_service_list.__dict__
1354 new_services = [service_entry]
1355 for key, value in service_list.iteritems():
1356 if key != 'linklocal_service_entry':
1357 continue
1358 for _entry in value:
1359 entry = _entry.__dict__
1360 if 'linklocal_service_name' in entry:
1361 if entry['linklocal_service_name'] == name:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001362 ret['comment'] = 'Link local service ' + name + ' already exists'
1363 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001364 new_services.append(_entry)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001365 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001366 ret['result'] = None
1367 ret['comment'] = "LinkLocalSevices " + name + " will be created"
Petr Jediný5f3efe32017-05-26 17:55:09 +02001368 service_list[key] = new_services
1369 new_config = GlobalVrouterConfig(linklocal_services=service_list)
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001370 if __opts__['test']:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001371 ret['result'] = None
1372 ret['comment'] = "LinkLocalSevices " + name + " will be updated"
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001373 else:
1374 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001375 ret['comment'] = "LinkLocalSevices " + name + " has been created"
1376 ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001377 return ret
Petr Jediný5f3efe32017-05-26 17:55:09 +02001378
1379
1380def linklocal_service_delete(name, **kwargs):
1381 '''
1382 Delete specific link local service entry
1383
1384 CLI Example:
1385
1386 .. code-block:: bash
1387
1388 salt '*' contrail.linklocal_service_delete llservice
1389 '''
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001390 ret = {'name': name,
1391 'changes': {},
1392 'result': True,
1393 'comment': ''}
1394 lls = linklocal_service_get(name)
1395 print (lls)
1396 if name in lls:
1397 if __opts__['test']:
1398 print " ------------ Test only ------------"
1399 ret['result'] = None
1400 ret['comment'] = "Link local service " + name + " will be deleted"
1401 return ret
1402 else:
1403 return ret
1404
Petr Jediný5f3efe32017-05-26 17:55:09 +02001405 vnc_client = _auth(**kwargs)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001406 current_config = _get_vrouter_config(vnc_client)
Petr Jediný5f3efe32017-05-26 17:55:09 +02001407 if current_config is not None:
1408 _current_service_list = current_config.get_linklocal_services()
1409 if _current_service_list is None:
1410 service_list = {'linklocal_service_entry': []}
1411 else:
1412 service_list = _current_service_list.__dict__
1413 new_services = []
1414 for key, value in service_list.iteritems():
1415 if key != 'linklocal_service_entry':
1416 continue
1417 for _entry in value:
1418 entry = _entry.__dict__
1419 if 'linklocal_service_name' in entry:
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001420 if entry['linklocal_service_name'] != name:
Petr Jediný5f3efe32017-05-26 17:55:09 +02001421 new_services.append(_entry)
1422 service_list[key] = new_services
1423 new_config = GlobalVrouterConfig(linklocal_services=service_list)
1424 vnc_client.global_vrouter_config_update(new_config)
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001425 ret['comment'] = "Link local service " + name + " will be deleted"
1426 ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
1427 return ret
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001428
1429
1430def virtual_machine_interface_list(**kwargs):
1431 '''
1432 Return a list of all Contrail virtual machine interfaces
1433
1434 CLI Example:
1435
1436 .. code-block:: bash
1437
1438 salt '*' contrail.virtual_machine_interfaces
1439 '''
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001440 ret = []
1441 vnc_client = _auth(**kwargs)
1442 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1443 project_uuid = project.get_uuid()
1444
1445 vm_ifaces = vnc_client.virtual_machine_interfaces_list(
1446 detail=True, parent_id=project_uuid)
1447
1448 for vm_iface in vm_ifaces:
1449 ret.append(vm_iface.__dict__)
1450
1451 return ret
1452
1453
1454def virtual_machine_interface_create(name,
1455 virtual_network,
1456 mac_address=None,
1457 ip_address=None,
1458 security_group=None,
1459 **kwargs):
1460 '''
1461 Create specific Contrail virtual machine interface (Port)
1462
1463 CLI Example:
1464
1465 .. code-block:: bash
1466
1467 salt '*' contrail.virtual_machine_interface_create port01 net01 mac_address='01:02:03:04:05:06'
1468 router_types:
1469 - tor-agent
1470 - tor-service-node
1471 - embedded
1472 '''
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001473 ret = {}
1474 vnc_client = _auth(**kwargs)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001475 project = _get_project_obj(vnc_client, name=kwargs.get('tenant', 'admin'))
1476
1477 vm_int = VirtualMachineInterface(name, parent_obj=project)
1478
1479 if mac_address:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001480 mac_address_obj = MacAddressesType([mac_address])
1481 vm_int.set_virtual_machine_interface_mac_addresses(mac_address_obj)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001482
1483 if security_group:
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001484 sgo = vnc_client.security_group_read(fq_name=_get_fq_name(
Pavel Svimbersky4358c352017-09-27 13:23:46 +02001485 vnc_client, security_group, kwargs.get('tenant', 'admin')))
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001486 vm_int.set_security_group(sgo)
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001487
1488 vnet_uuid = virtual_network_get(virtual_network, **kwargs)[virtual_network]['_uuid']
1489 vnet_obj = vnc_client.virtual_network_read(id=vnet_uuid)
1490 vm_int.set_virtual_network(vnet_obj)
1491
1492 vmi_uuid = vnc_client.virtual_machine_interface_create(vm_int)
1493 vmi = vnc_client.virtual_machine_interface_read(id=vmi_uuid)
1494
1495 vm_int.set_port_security_enabled(False)
1496 vnc_client.virtual_machine_interface_update(vm_int)
1497
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001498 # Allocate IP to VMI
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001499 ip = vnc_api.InstanceIp(name + '.ip')
1500 ip.set_virtual_machine_interface(vmi)
1501 ip.set_virtual_network(vnet_obj)
1502
1503 ip_uuid = vnc_client.instance_ip_create(ip)
1504
1505 if ip_address:
1506 ip.set_instance_ip_address(ip_address)
1507 vnc_client.instance_ip_update(ip)
1508
1509 return vmi.__dict__
Pavel Svimbersky483a19e2017-08-22 09:50:29 +02001510
1511
1512def virtual_network_list(**kwargs):
1513 '''
1514 Return a list of all Contrail virtual network
1515
1516 CLI Example:
1517
1518 .. code-block:: bash
1519
1520 salt '*' contrail.virtual_network
1521 '''
1522
1523 ret = {}
1524 vnc_client = _auth(**kwargs)
1525 virtual_networks = vnc_client._objects_list('virtual-network', detail=True)
1526 for virtual_network in virtual_networks:
1527 ret[virtual_network.name] = virtual_network.__dict__
1528 return ret
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001529
1530
1531def virtual_network_get(name, **kwargs):
1532 '''
1533 Return a specific Contrail virtual network
1534
1535 CLI Example:
1536
1537 .. code-block:: bash
1538
1539 salt '*' contrail.virtual_network_get net01
1540 '''
1541 ret = {}
1542 vnet_objs = virtual_network_list(**kwargs)
1543 if name in vnet_objs:
1544 ret[name] = vnet_objs.get(name)
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001545 if len(ret) != 1:
Vasyl Saienkob10b7202017-09-05 14:19:03 +03001546 return {'result': False,
1547 'Error': 'Error in retrieving virtual networks.'}
1548 return ret
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001549
1550
Jan Cacha859e6b2018-01-09 17:34:18 +01001551def virtual_network_create(name, conf=None, **kwargs):
1552 '''
1553 Create Contrail virtual network
1554 CLI Example:
1555 .. code-block:: bash
1556 salt '*' contrail.virtual_network_create name
1557
1558 salt.cmdRun(pepperEnv, 'ntw01*', 'salt-call contrail.virtual_network_create
1559 "testicek" "{"external":"True","ip":"172.16.111.0","prefix":24,
1560 "asn":64512,"target":10000}" ')
1561
1562 Parameters:
1563 name required - name of the new network
1564
1565 conf (dict) optional:
1566 domain (string) optional - which domain use for vn creation
1567 project (string) optional - which project use for vn creation
1568 ipam_domain (string) optional - domain for ipam
1569 ipam_project (string) optional - project for ipam
1570 ipam_name (string) optional - ipam name
1571 ip_prefix (string) optional - format is xxx.xxx.xxx.xxx
1572 ip_prefix_len (int) optional - format is xx
1573 asn (int) optional - autonomus system number
1574 target (int) optional - route target number
1575 external (boolean) optional - set if network is external
1576
1577 allow_transit (boolean) optional - enable allow transit
1578 forwarding_mode (any of ['l2_l3','l2','l3']) optional
1579 - packet forwarding mode for this virtual network
1580 rpf (any of ['enabled','disabled']) optional
1581 - Enable or disable Reverse Path Forwarding check
1582 for this network
1583 mirror_destination (boolean) optional
1584 - Mark the vn as mirror destination network
1585 '''
1586 if conf is None:
1587 conf = {}
1588
1589 # check for domain, is missing set to default-domain
1590 if 'domain' in conf:
1591 vn_domain = str(conf['domain'])
1592 else:
1593 vn_domain = 'default-domain'
1594 # check for project, is missing set to admin
1595 if 'project' in conf:
1596 vn_project = str(conf['project'])
1597 else:
1598 vn_project = 'admin'
1599 # check for ipam domain,default is default-domain
1600 if 'ipam_domain' in conf:
1601 ipam_domain = str(conf['ipam_domain'])
1602 else:
1603 ipam_domain = 'default-domain'
1604 # check for ipam domain,default is default-domain
1605 if 'ipam_project' in conf:
1606 ipam_project = str(conf['ipam_project'])
1607 else:
1608 ipam_project = 'default-project'
1609
1610 if 'ipam_name' in conf:
1611 ipam_name = conf['ipam_name']
1612 else:
1613 ipam_name = 'default-network-ipam'
1614
1615 ret = {'name': name,
1616 'changes': {},
1617 'result': True,
1618 'comment': ''}
1619
1620 # list of existing vn networks
1621 vn_networks = []
1622 vnc_client = _auth(**kwargs)
Jan Cach03412162018-01-12 10:29:22 +01001623 prj_obj = vnc_client.project_read(fq_name=[vn_domain,
1624 vn_project])
Jan Cacha859e6b2018-01-09 17:34:18 +01001625 # check if the network exists
1626 vn_networks_list = vnc_client._objects_list('virtual_network')
Jan Cachece0d242018-01-12 14:58:36 +01001627 fq = [vn_domain, vn_project, name]
Jan Cacha859e6b2018-01-09 17:34:18 +01001628 for network in vn_networks_list['virtual-networks']:
1629 if fq == network['fq_name']:
1630 ret['comment'] = ("Virtual network with name "
1631 + name + " already exists")
1632 return ret
1633
Jan Cach03412162018-01-12 10:29:22 +01001634 vn_obj = VirtualNetwork(name, prj_obj)
Jan Cacha859e6b2018-01-09 17:34:18 +01001635 vn_type_obj = VirtualNetworkType()
1636 # get ipam from default project and domain
1637 ipam = vnc_client.network_ipam_read(fq_name=[ipam_domain,
1638 ipam_project,
1639 ipam_name])
1640
1641 # create subnet
1642 if 'ip_prefix' in conf and 'ip_prefix_len' in conf:
1643 ipam_subnet_type = IpamSubnetType(subnet=SubnetType(
1644 ip_prefix=conf['ip_prefix'],
1645 ip_prefix_len=conf['ip_prefix_len']))
1646
1647 vn_subnets_type_obj = VnSubnetsType(ipam_subnets=[ipam_subnet_type])
1648 vn_obj.add_network_ipam(ipam, vn_subnets_type_obj)
1649
1650 # add route target to the network
1651 if 'asn' in conf and 'target' in conf:
1652 route_target_list_obj = RouteTargetList(["target:{0}:{1}"
1653 .format(conf['asn'],
1654 conf['target'])])
1655 vn_obj.set_route_target_list(route_target_list_obj)
1656
1657 if 'external' in conf:
1658 vn_obj.set_router_external(conf['external'])
1659
1660 if 'allow_transit' in conf:
1661 vn_type_obj.set_allow_transit(conf['allow_transit'])
1662
1663 if 'forwarding_mode' in conf:
1664 if conf['forwarding_mode'] in ['l2_l3', 'l2', 'l3']:
1665 vn_type_obj.set_forwarding_mode(conf['forwarding_mode'])
1666
1667 if 'rpf' in conf:
1668 vn_type_obj.set_rpf(conf['rpf'])
1669
1670 if 'mirror_destination' in conf:
1671 vn_type_obj.set_mirror_destination(conf['mirror_destination'])
1672
1673 vn_obj.set_virtual_network_properties(vn_type_obj)
1674
1675 # create virtual network
1676 if __opts__['test']:
1677 ret['result'] = None
1678 ret['comment'] = ("Virtual network with name {0} will be created"
1679 .format(name))
1680 else:
1681 vnc_client.virtual_network_create(vn_obj)
1682 ret['comment'] = ("Virtual network with name {0} was created"
1683 .format(name))
1684 return ret
1685
1686
Pavel Svimbersky5ba8a7b2017-09-21 11:07:48 +02001687def service_appliance_set_list(**kwargs):
1688 '''
1689 Return a list of Contrail service appliance set
1690
1691 CLI Example:
1692
1693 .. code-block:: bash
1694
1695 salt '*' contrail.service_appliance_set_list
1696 '''
1697 ret = {}
1698 vnc_client = _auth(**kwargs)
1699 service_appliance_sets = vnc_client._objects_list('service-appliance-set', detail=True)
1700 for service_appliance_set in service_appliance_sets:
1701 ret[service_appliance_set.name] = service_appliance_set.__dict__
1702 return ret
1703
1704
1705def service_appliance_set_get(name, **kwargs):
1706 '''
1707 Return a specific Contrail service appliance set
1708
1709 CLI Example:
1710
1711 .. code-block:: bash
1712
1713 salt '*' contrail.service_appliance_set_get name
1714 '''
1715 ret = {}
1716 sas_objs = service_appliance_set_list(**kwargs)
1717 if name in sas_objs:
1718 ret[name] = sas_objs.get(name)
1719 if len(ret) != 1:
1720 return {'result': False,
1721 'Error': "Error in the retrieving service apliance set."}
1722 return ret
1723
1724
1725def service_appliance_set_create(name, properties=None, driver=None, ha_mode=None, **kwargs):
1726 '''
1727 Create Contrail service appliance set
1728
1729 CLI Example:
1730
1731 .. code-block:: bash
1732
1733 salt '*' contrail.service_appliance_set_create name
1734 '''
1735 ret = {'name': name,
1736 'changes': {},
1737 'result': True,
1738 'comment': ''}
1739 vnc_client = _auth(**kwargs)
1740 gsc_obj = _get_config(vnc_client)
1741 sas_objs = service_appliance_set_list(**kwargs)
1742 if name in sas_objs:
1743 ret['commnet'] = 'Service appliance set ' + name + ' already exists'
1744 else:
1745 service_appliance_set_obj = ServiceApplianceSet(
1746 name, gsc_obj)
1747 if properties:
1748 pairs = KeyValuePairs()
1749 for k, v in properties.items():
1750 pairs.add_key_value_pair(KeyValuePair(k, v))
1751 service_appliance_set_obj.set_service_appliance_set_properties(pairs)
1752 if driver:
1753 service_appliance_set_obj.set_service_appliance_driver(driver)
1754 if ha_mode:
1755 service_appliance_set_obj.set_service_appliance_ha_mode(ha_mode)
1756 if __opts__['test']:
1757 ret['result'] = None
1758 ret['comment'] = "ServiceApplianceSet " + name + " will be created"
1759 else:
1760 vnc_client.service_appliance_set_create(service_appliance_set_obj)
1761 ret['comment'] = "ServiceApplianceSet " + name + " has been created"
1762 ret['changes'] = {'ServiceApplianceSet': {'old': '', 'new': name}}
1763 return ret
1764
1765
1766def service_appliance_set_delete(name, **kwargs):
1767 '''
1768 Delete specific Contrail service appliance set
1769
1770 CLI Example:
1771
1772 .. code-block:: bash
1773
1774 salt '*' contrail.service_appliance_set_delete name
1775 '''
1776 ret = {'name': name,
1777 'changes': {},
1778 'result': True,
1779 'comment': ''}
1780 vnc_client = _auth(**kwargs)
1781 gsc_obj = _get_config(vnc_client)
1782 sas_obj = ServiceApplianceSet(name, gsc_obj)
1783 if __opts__['test']:
1784 ret['result'] = None
1785 ret['comment'] = "Service appliance set " + name + " will be deleted"
1786 else:
1787 vnc_client.service_appliance_set_delete(fq_name=sas_obj.get_fq_name())
1788 ret['comment'] = "ServiceApplianceSet " + name + " has been deleted"
1789 ret['changes'] = {'ServiceApplianceSet': {'old': name, 'new': ''}}
1790 return ret
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001791
1792def global_system_config_list(**kwargs):
1793 '''
1794 Return a list of all global system configs
1795
1796 CLI Example:
1797
1798 .. code-block:: bash
1799
1800 salt '*' contrail.global_system_config_list
1801 '''
1802
1803 ret = {}
1804 vnc_client = _auth(**kwargs)
1805 gsysconfs = vnc_client._objects_list('global-system-config', detail=True)
1806 for gsysconf in gsysconfs:
1807 ret[gsysconf.name] = gsysconf.__dict__
1808 return ret
1809
1810
1811def global_system_config_get(name, **kwargs):
1812 '''
1813 Return a specific Contrail global system config
1814
1815 CLI Example:
1816
1817 .. code-block:: bash
1818
1819 salt '*' contrail.global_system_config_get name
1820 '''
1821 ret = {}
1822 vnc_client = _auth(**kwargs)
1823 gsc_objs = vnc_client._objects_list('global-system-config', detail=True)
1824 for gsc_obj in gsc_objs:
1825 if name == gsc_obj.name:
1826 ret[name] = gsc_obj.__dict__
1827 if len(ret) == 0:
1828 return {'Error': 'Error in retrieving global system config.'}
1829 return ret
1830
1831
1832def global_system_config_create(name, ans=64512, grp=None, **kwargs):
1833 '''
1834 Create Contrail global system config
1835
1836 CLI Example:
1837
1838 .. code-block:: bash
1839
1840 salt '*' contrail.global_system_config_create name=default-global-system-config ans=64512
1841 '''
1842 ret = {'name': name,
1843 'changes': {},
1844 'result': True,
1845 'comment': ''}
1846 vnc_client = _auth(**kwargs)
1847
1848 gsc_objs = global_system_config_list(**kwargs)
1849 if name in gsc_objs:
1850 config_obj = vnc_client.global_system_config_read(fq_name=[name])
1851 if config_obj.graceful_restart_parameters and not HAS_OLD:
1852 curr_grp = str(config_obj.graceful_restart_parameters).replace(" ", "").split(",")
1853 curr_grpd = dict(item.split('=') for item in curr_grp)
psvimbersky3c84e272018-01-02 10:34:29 +01001854 else:
1855 curr_grpd = None
Pavel Svimberskydbd52ea2017-12-05 18:05:25 +01001856
1857 if grp and 'enable' in grp and not HAS_OLD:
1858 grp_obj = GracefulRestartParametersType()
1859 if 'enable' in grp:
1860 grp_obj.enable = grp['enable']
1861 if curr_grpd and str(grp['enable']) != str(curr_grpd['enable']):
1862 ret['changes']['enable'] = {"from": str(curr_grpd['enable']), "to": str(grp['enable'])}
1863 elif not curr_grpd:
1864 ret['changes']['enable'] = {"from": None, "to": grp['enable']}
1865 if 'restart_time' in grp:
1866 grp_obj.restart_time = grp['restart_time']
1867 if curr_grpd and grp['restart_time'] != int(curr_grpd['restart_time']):
1868 ret['changes']['restart_time'] = {"from": int(curr_grpd['restart_time']), "to": grp['restart_time']}
1869 elif not curr_grpd:
1870 ret['changes']['restart_time'] = {"from": None, "to": grp['restart_time']}
1871 if 'end_of_rib_timeout' in grp:
1872 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1873 if curr_grpd and grp['end_of_rib_timeout'] != int(curr_grpd['end_of_rib_timeout']):
1874 ret['changes']['end_of_rib_timeout'] = {"from": int(curr_grpd['end_of_rib_timeout']), "to": grp['end_of_rib_timeout']}
1875 elif not curr_grpd:
1876 ret['changes']['end_of_rib_timeout'] = {"from": None, "to": grp['end_of_rib_timeout']}
1877 if 'bgp_helper_enable' in grp:
1878 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1879 if curr_grpd and str(grp['bgp_helper_enable']) != str(curr_grpd['bgp_helper_enable']):
1880 ret['changes']['bgp_helper_enable'] = {"from": str(curr_grpd['bgp_helper_enable']), "to": grp['bgp_helper_enable']}
1881 elif not curr_grpd:
1882 ret['changes']['bgp_helper_enable'] = {"from": None, "to": grp['bgp_helper_enable']}
1883 if 'xmpp_helper_enable' in grp:
1884 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1885 if curr_grpd and str(grp['xmpp_helper_enable']) != str(curr_grpd['xmpp_helper_enable']):
1886 ret['changes']['xmpp_helper_enable'] = {"from": str(curr_grpd['xmpp_helper_enable']), "to": grp['xmpp_helper_enable']}
1887 elif not curr_grpd:
1888 ret['changes']['xmpp_helper_enable'] = {"from": None, "to": grp['xmpp_helper_enable']}
1889 if 'long_lived_restart_time' in grp:
1890 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1891 if curr_grpd and grp['long_lived_restart_time'] != int(curr_grpd['long_lived_restart_time']):
1892 ret['changes']['long_lived_restart_time'] = {"from": int(curr_grpd['long_lived_restart_time']), "to": grp['long_lived_restart_time']}
1893 elif not curr_grpd:
1894 ret['changes']['long_lived_restart_time'] = {"from": None, "to": grp['long_lived_restart_time']}
1895 else:
1896 grp_obj = None
1897
1898 config_obj.graceful_restart_parameters = grp_obj
1899
1900 if ans:
1901 if config_obj.autonomous_system != ans:
1902 ret['changes']['autonomous_system'] = {"from": config_obj.autonomous_system, "to": ans}
1903 config_obj.autonomous_system = ans
1904
1905 vnc_client.global_system_config_update(config_obj)
1906 ret['comment'] = 'Global system config ' + name + ' has been updated'
1907 else:
1908 config_obj = GlobalSystemConfig(name=name)
1909 if grp and not HAS_OLD:
1910 grp_obj = GracefulRestartParametersType()
1911 if 'enable' in grp:
1912 grp_obj.enable = grp['enable']
1913 if 'restart_time' in grp:
1914 grp_obj.restart_time = grp['restart_time']
1915 if 'end_of_rib_timeout' in grp:
1916 grp_obj.end_of_rib_timeout = grp['end_of_rib_timeout']
1917 if 'bgp_helper_enable' in grp:
1918 grp_obj.bgp_helper_enable = grp['bgp_helper_enable']
1919 if 'xmpp_helper_enable' in grp:
1920 grp_obj.xmpp_helper_enable = grp['xmpp_helper_enable']
1921 if 'long_lived_restart_time' in grp:
1922 grp_obj.long_lived_restart_time = grp['long_lived_restart_time']
1923 config_obj.graceful_restart_parameters = grp_obj
1924 if ans:
1925 config_obj.autonomous_system = ans
1926
1927 vnc_client.global_system_config_create(config_obj)
1928 ret['changes'] = {"created": "new"}
1929 ret['comment'] = 'Global system config ' + name + ' has been created '
1930
1931 return ret
1932
1933
1934def global_system_config_delete(name, **kwargs):
1935 '''
1936 Delete specific Contrail global system config
1937
1938 CLI Example:
1939
1940 .. code-block:: bash
1941
1942 salt '*' contrail.global_system_config_delete name
1943 '''
1944 ret = {'name': name,
1945 'changes': {},
1946 'result': True,
1947 'comment': ''}
1948 vnc_client = _auth(**kwargs)
1949
1950 gsc_obj = GlobalSystemConfig(name)
1951 if __opts__['test']:
1952 ret['result'] = None
1953 ret['comment'] = "Global system config " + name + " will be deleted"
1954 else:
1955 vnc_client.global_system_config_delete(fq_name=gsc_obj.get_fq_name())
1956 ret['comment'] = "GlobalSystemConfig " + name + " has been deleted"
1957 ret['changes'] = {'GlobalSystemConfig': {'old': name, 'new': ''}}
1958 return ret