Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1 | #!/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 | |
| 16 | from netaddr import IPNetwork |
| 17 | |
| 18 | try: |
| 19 | from vnc_api import vnc_api |
| 20 | from vnc_api.gen.resource_client import VirtualRouter, AnalyticsNode, \ |
| 21 | ConfigNode, DatabaseNode, BgpRouter |
| 22 | from vnc_api.gen.resource_xsd import AddressFamilies, BgpSessionAttributes, \ |
| 23 | BgpSession, BgpPeeringAttributes, BgpRouterParams |
| 24 | HAS_CONTRAIL = True |
| 25 | except ImportError: |
| 26 | HAS_CONTRAIL = False |
| 27 | |
| 28 | __opts__ = {} |
| 29 | |
| 30 | |
| 31 | def __virtual__(): |
| 32 | ''' |
| 33 | Only load this module if vnc_api library is installed. |
| 34 | ''' |
| 35 | if HAS_CONTRAIL: |
| 36 | return 'contrail' |
| 37 | |
| 38 | return False |
| 39 | |
| 40 | |
| 41 | def _auth(**kwargs): |
| 42 | ''' |
| 43 | Set up Contrail API credentials. |
| 44 | ''' |
| 45 | user = kwargs.get('user') |
| 46 | password = kwargs.get('password') |
| 47 | tenant_name = kwargs.get('project') |
| 48 | api_host = kwargs.get('api_server_ip') |
| 49 | api_port = kwargs.get('api_server_port') |
| 50 | api_base_url = kwargs.get('api_base_url') |
| 51 | use_ssl = False |
| 52 | auth_host = kwargs.get('auth_host_ip') |
| 53 | vnc_lib = vnc_api.VncApi(user, password, tenant_name, |
| 54 | api_host, api_port, api_base_url, wait_for_connect=True, |
| 55 | api_server_use_ssl=use_ssl, auth_host=auth_host) |
| 56 | |
| 57 | return vnc_lib |
| 58 | |
| 59 | |
| 60 | def _get_config(vnc_client, global_system_config = 'default-global-system-config'): |
| 61 | try: |
| 62 | gsc_obj = vnc_client.global_system_config_read(id=global_system_config) |
| 63 | except vnc_api.NoIdError: |
| 64 | gsc_obj = vnc_client.global_system_config_read(fq_name_str=global_system_config) |
| 65 | except: |
| 66 | gsc_obj = None |
| 67 | |
| 68 | return gsc_obj |
| 69 | |
| 70 | |
| 71 | def _get_rt_inst_obj(vnc_client): |
| 72 | |
| 73 | # TODO pick fqname hardcode from common |
| 74 | rt_inst_obj = vnc_client.routing_instance_read( |
| 75 | fq_name=['default-domain', 'default-project', |
| 76 | 'ip-fabric', '__default__']) |
| 77 | |
| 78 | return rt_inst_obj |
| 79 | |
| 80 | |
| 81 | def _get_ip(ip_w_pfx): |
| 82 | return str(IPNetwork(ip_w_pfx).ip) |
| 83 | |
| 84 | |
| 85 | |
| 86 | def virtual_router_list(**kwargs): |
| 87 | ''' |
| 88 | Return a list of all Contrail virtual routers |
| 89 | |
| 90 | CLI Example: |
| 91 | |
| 92 | .. code-block:: bash |
| 93 | |
| 94 | salt '*' contrail.virtual_router_list |
| 95 | ''' |
| 96 | ret = {} |
| 97 | vnc_client = _auth(**kwargs) |
| 98 | vrouter_objs = vnc_client._objects_list('virtual-router', detail=True) |
| 99 | for vrouter_obj in vrouter_objs: |
| 100 | ret[vrouter_obj.name] = { |
| 101 | 'ip_address': vrouter_obj.virtual_router_ip_address, |
| 102 | 'dpdk_enabled': vrouter_obj.virtual_router_dpdk_enabled |
| 103 | } |
| 104 | return ret |
| 105 | |
| 106 | |
| 107 | def virtual_router_get(name, **kwargs): |
| 108 | ''' |
| 109 | Return a specific Contrail virtual router |
| 110 | |
| 111 | CLI Example: |
| 112 | |
| 113 | .. code-block:: bash |
| 114 | |
| 115 | salt '*' contrail.virtual_router_get cmp01 |
| 116 | ''' |
| 117 | ret = {} |
| 118 | vrouter_objs = virtual_router_list(**kwargs) |
| 119 | if name in vrouter_objs: |
| 120 | ret[name] = vrouter_objs.get(name) |
| 121 | if len(ret) == 0: |
| 122 | return {'Error': 'Error in retrieving virtual router.'} |
| 123 | return ret |
| 124 | |
| 125 | |
| 126 | def virtual_router_create(name, ip_address, dpdk_enabled=False, **kwargs): |
| 127 | ''' |
| 128 | Create specific Contrail virtual router |
| 129 | |
| 130 | CLI Example: |
| 131 | |
| 132 | .. code-block:: bash |
| 133 | |
| 134 | salt '*' contrail.virtual_router_create cmp02 10.10.10.102 |
| 135 | ''' |
| 136 | ret = {} |
| 137 | vnc_client = _auth(**kwargs) |
| 138 | gsc_obj = _get_config(vnc_client) |
| 139 | vrouter_objs = virtual_router_list(**kwargs) |
| 140 | if name in vrouter_objs: |
| 141 | return {'Error': 'Virtual router %s already exists' % name} |
| 142 | else: |
| 143 | vrouter_obj = VirtualRouter( |
| 144 | name, gsc_obj, |
| 145 | virtual_router_ip_address=ip_address) |
| 146 | vrouter_obj.set_virtual_router_dpdk_enabled(dpdk_enabled) |
| 147 | vnc_client.virtual_router_create(vrouter_obj) |
| 148 | ret = virtual_router_list(**kwargs) |
| 149 | return ret[name] |
| 150 | |
| 151 | |
| 152 | def virtual_router_delete(name, **kwargs): |
| 153 | ''' |
| 154 | Delete specific Contrail virtual router |
| 155 | |
| 156 | CLI Example: |
| 157 | |
| 158 | .. code-block:: bash |
| 159 | |
| 160 | salt '*' contrail.virtual_router_delete cmp01 |
| 161 | ''' |
| 162 | vnc_client = _auth(**kwargs) |
| 163 | gsc_obj = _get_config(vnc_client) |
| 164 | vrouter_obj = VirtualRouter(name, gsc_obj) |
| 165 | vnc_client.virtual_router_delete( |
| 166 | fq_name=vrouter_obj.get_fq_name()) |
| 167 | |
| 168 | |
| 169 | def analytics_node_list(**kwargs): |
| 170 | ''' |
| 171 | Return a list of all Contrail analytics nodes |
| 172 | |
| 173 | CLI Example: |
| 174 | |
| 175 | .. code-block:: bash |
| 176 | |
| 177 | salt '*' contrail.analytics_node_list |
| 178 | ''' |
| 179 | ret = {} |
| 180 | vnc_client = _auth(**kwargs) |
| 181 | node_objs = vnc_client._objects_list('analytics-node', detail=True) |
| 182 | for node_obj in node_objs: |
| 183 | ret[node_obj.name] = node_obj.__dict__ |
| 184 | return ret |
| 185 | |
| 186 | |
| 187 | def analytics_node_get(name, **kwargs): |
| 188 | ''' |
| 189 | Return a specific Contrail analytics node |
| 190 | |
| 191 | CLI Example: |
| 192 | |
| 193 | .. code-block:: bash |
| 194 | |
| 195 | salt '*' contrail.analytics_node_get nal01 |
| 196 | ''' |
| 197 | ret = {} |
| 198 | vrouter_objs = analytics_node_list(**kwargs) |
| 199 | if name in vrouter_objs: |
| 200 | ret[name] = vrouter_objs.get(name) |
| 201 | if len(ret) == 0: |
| 202 | return {'Error': 'Error in retrieving analytics node.'} |
| 203 | return ret |
| 204 | |
| 205 | |
| 206 | def analytics_node_create(name, ip_address, **kwargs): |
| 207 | ''' |
| 208 | Create specific Contrail analytics node |
| 209 | |
| 210 | CLI Example: |
| 211 | |
| 212 | .. code-block:: bash |
| 213 | |
| 214 | salt '*' contrail.analytics_node_create ntw03 10.10.10.103 |
| 215 | ''' |
| 216 | ret = {} |
| 217 | vnc_client = _auth(**kwargs) |
| 218 | gsc_obj = _get_config(vnc_client) |
| 219 | analytics_node_objs = analytics_node_list(**kwargs) |
| 220 | if name in analytics_node_objs: |
| 221 | return {'Error': 'Analytics node %s already exists' % name} |
| 222 | else: |
| 223 | analytics_node_obj = AnalyticsNode( |
| 224 | name, gsc_obj, |
| 225 | analytics_node_ip_address=ip_address) |
| 226 | vnc_client.analytics_node_create(analytics_node_obj) |
| 227 | ret = analytics_node_list(**kwargs) |
| 228 | return ret[name] |
| 229 | |
| 230 | |
| 231 | def analytics_node_delete(name, **kwargs): |
| 232 | ''' |
| 233 | Delete specific Contrail analytics node |
| 234 | |
| 235 | CLI Example: |
| 236 | |
| 237 | .. code-block:: bash |
| 238 | |
| 239 | salt '*' contrail.analytics_node_delete cmp01 |
| 240 | ''' |
| 241 | vnc_client = _auth(**kwargs) |
| 242 | gsc_obj = _get_config(vnc_client) |
| 243 | analytics_node_obj = AnalyticsNode(name, gsc_obj) |
| 244 | vnc_client.analytics_node_delete( |
| 245 | fq_name=analytics_node_obj.get_fq_name()) |
| 246 | |
| 247 | |
| 248 | def config_node_list(**kwargs): |
| 249 | ''' |
| 250 | Return a list of all Contrail config nodes |
| 251 | |
| 252 | CLI Example: |
| 253 | |
| 254 | .. code-block:: bash |
| 255 | |
| 256 | salt '*' contrail.config_node_list |
| 257 | ''' |
| 258 | ret = {} |
| 259 | vnc_client = _auth(**kwargs) |
| 260 | node_objs = vnc_client._objects_list('config-node', detail=True) |
| 261 | for node_obj in node_objs: |
| 262 | ret[node_obj.name] = node_obj.__dict__ |
| 263 | return ret |
| 264 | |
| 265 | |
| 266 | def config_node_get(name, **kwargs): |
| 267 | ''' |
| 268 | Return a specific Contrail config node |
| 269 | |
| 270 | CLI Example: |
| 271 | |
| 272 | .. code-block:: bash |
| 273 | |
| 274 | salt '*' contrail.config_node_get nal01 |
| 275 | ''' |
| 276 | ret = {} |
| 277 | vrouter_objs = config_node_list(**kwargs) |
| 278 | if name in vrouter_objs: |
| 279 | ret[name] = vrouter_objs.get(name) |
| 280 | if len(ret) == 0: |
| 281 | return {'Error': 'Error in retrieving config node.'} |
| 282 | return ret |
| 283 | |
| 284 | |
| 285 | def config_node_create(name, ip_address, **kwargs): |
| 286 | ''' |
| 287 | Create specific Contrail config node |
| 288 | |
| 289 | CLI Example: |
| 290 | |
| 291 | .. code-block:: bash |
| 292 | |
| 293 | salt '*' contrail.config_node_create ntw03 10.10.10.103 |
| 294 | ''' |
| 295 | ret = {} |
| 296 | vnc_client = _auth(**kwargs) |
| 297 | gsc_obj = _get_config(vnc_client) |
| 298 | config_node_objs = config_node_list(**kwargs) |
| 299 | if name in config_node_objs: |
| 300 | return {'Error': 'Config node %s already exists' % name} |
| 301 | else: |
| 302 | config_node_obj = ConfigNode( |
| 303 | name, gsc_obj, |
| 304 | config_node_ip_address=ip_address) |
| 305 | vnc_client.config_node_create(config_node_obj) |
| 306 | ret = config_node_list(**kwargs) |
| 307 | return ret[name] |
| 308 | |
| 309 | |
| 310 | def config_node_delete(name, **kwargs): |
| 311 | ''' |
| 312 | Delete specific Contrail config node |
| 313 | |
| 314 | CLI Example: |
| 315 | |
| 316 | .. code-block:: bash |
| 317 | |
| 318 | salt '*' contrail.config_node_delete cmp01 |
| 319 | ''' |
| 320 | vnc_client = _auth(**kwargs) |
| 321 | gsc_obj = _get_config(vnc_client) |
| 322 | config_node_obj = ConfigNode(name, gsc_obj) |
| 323 | vnc_client.config_node_delete( |
| 324 | fq_name=config_node_obj.get_fq_name()) |
| 325 | |
| 326 | |
| 327 | def bgp_router_list(**kwargs): |
| 328 | ''' |
| 329 | Return a list of all Contrail BGP routers |
| 330 | |
| 331 | CLI Example: |
| 332 | |
| 333 | .. code-block:: bash |
| 334 | |
| 335 | salt '*' contrail.bgp_router_list |
| 336 | ''' |
| 337 | ret = {} |
| 338 | vnc_client = _auth(**kwargs) |
| 339 | bgp_router_objs = vnc_client._objects_list('bgp-router', detail=True) |
| 340 | for bgp_router_obj in bgp_router_objs: |
| 341 | ret[bgp_router_obj.name] = bgp_router_obj.__dict__ |
| 342 | return ret |
| 343 | |
| 344 | |
| 345 | def bgp_router_get(name, **kwargs): |
| 346 | ''' |
| 347 | Return a specific Contrail BGP router |
| 348 | |
| 349 | CLI Example: |
| 350 | |
| 351 | .. code-block:: bash |
| 352 | |
| 353 | salt '*' contrail.bgp_router_get nal01 |
| 354 | ''' |
| 355 | ret = {} |
| 356 | bgp_router_objs = bgp_router_list(**kwargs) |
| 357 | if name in bgp_router_objs: |
| 358 | ret[name] = bgp_router_objs.get(name) |
| 359 | if len(ret) == 0: |
| 360 | return {'Error': 'Error in retrieving BGP router.'} |
| 361 | return ret |
| 362 | |
| 363 | |
| 364 | def bgp_router_create(name, type, ip_address, asn=64512, **kwargs): |
| 365 | ''' |
| 366 | Create specific Contrail control node |
| 367 | |
| 368 | CLI Example: |
| 369 | |
| 370 | .. code-block:: bash |
| 371 | |
| 372 | salt '*' contrail.bgp_router_create ntw03 control-node 10.10.10.103 |
| 373 | salt '*' contrail.bgp_router_create mx01 router 10.10.10.105 |
| 374 | ''' |
| 375 | ret = {} |
| 376 | vnc_client = _auth(**kwargs) |
| 377 | |
| 378 | bgp_router_objs = bgp_router_list(**kwargs) |
| 379 | if name in bgp_router_objs: |
| 380 | return {'Error': 'control node %s already exists' % name} |
| 381 | else: |
| 382 | address_families = ['route-target', 'inet-vpn', 'e-vpn', 'erm-vpn', |
| 383 | 'inet6-vpn'] |
| 384 | if type != 'control-node': |
| 385 | address_families.remove('erm-vpn') |
| 386 | |
| 387 | bgp_addr_fams = AddressFamilies(address_families) |
| 388 | bgp_sess_attrs = [ |
| 389 | BgpSessionAttributes(address_families=bgp_addr_fams)] |
| 390 | bgp_sessions = [BgpSession(attributes=bgp_sess_attrs)] |
| 391 | bgp_peering_attrs = BgpPeeringAttributes(session=bgp_sessions) |
| 392 | rt_inst_obj = _get_rt_inst_obj(vnc_client) |
| 393 | |
| 394 | if type == 'control-node': |
| 395 | vendor = 'contrail' |
| 396 | elif type == 'router': |
| 397 | vendor = 'mx' |
| 398 | else: |
| 399 | vendor = 'unknown' |
| 400 | |
| 401 | router_params = BgpRouterParams(router_type=type, |
| 402 | vendor=vendor, autonomous_system=int(asn), |
| 403 | identifier=_get_ip(ip_address), |
| 404 | address=_get_ip(ip_address), |
| 405 | port=179, address_families=bgp_addr_fams) |
| 406 | bgp_router_obj = BgpRouter(name, rt_inst_obj, |
| 407 | bgp_router_parameters=router_params) |
| 408 | vnc_client.bgp_router_create(bgp_router_obj) |
| 409 | ret = bgp_router_list(**kwargs) |
| 410 | return ret[name] |
| 411 | |
| 412 | |
| 413 | def bgp_router_delete(name, **kwargs): |
| 414 | ''' |
| 415 | Delete specific Contrail control node |
| 416 | |
| 417 | CLI Example: |
| 418 | |
| 419 | .. code-block:: bash |
| 420 | |
| 421 | salt '*' contrail.bgp_router_delete mx01 |
| 422 | ''' |
| 423 | vnc_client = _auth(**kwargs) |
| 424 | gsc_obj = _get_control(vnc_client) |
| 425 | bgp_router_obj = BgpRouter(name, gsc_obj) |
| 426 | vnc_client.bgp_router_delete( |
| 427 | fq_name=bgp_router_obj.get_fq_name()) |
| 428 | |
| 429 | |
| 430 | def database_node_list(**kwargs): |
| 431 | ''' |
| 432 | Return a list of all Contrail database nodes |
| 433 | |
| 434 | CLI Example: |
| 435 | |
| 436 | .. code-block:: bash |
| 437 | |
| 438 | salt '*' contrail.database_node_list |
| 439 | ''' |
| 440 | ret = {} |
| 441 | vnc_client = _auth(**kwargs) |
| 442 | node_objs = vnc_client._objects_list('database-node', detail=True) |
| 443 | for node_obj in node_objs: |
| 444 | ret[node_obj.name] = node_obj.__dict__ |
| 445 | return ret |
| 446 | |
| 447 | |
| 448 | def database_node_get(name, **kwargs): |
| 449 | ''' |
| 450 | Return a specific Contrail database node |
| 451 | |
| 452 | CLI Example: |
| 453 | |
| 454 | .. code-block:: bash |
| 455 | |
| 456 | salt '*' contrail.database_node_get nal01 |
| 457 | ''' |
| 458 | ret = {} |
| 459 | vrouter_objs = database_node_list(**kwargs) |
| 460 | if name in vrouter_objs: |
| 461 | ret[name] = vrouter_objs.get(name) |
| 462 | if len(ret) == 0: |
| 463 | return {'Error': 'Error in retrieving database node.'} |
| 464 | return ret |
| 465 | |
| 466 | |
| 467 | def database_node_create(name, ip_address, **kwargs): |
| 468 | ''' |
| 469 | Create specific Contrail database node |
| 470 | |
| 471 | CLI Example: |
| 472 | |
| 473 | .. code-block:: bash |
| 474 | |
| 475 | salt '*' contrail.database_node_create ntw03 10.10.10.103 |
| 476 | ''' |
| 477 | ret = {} |
| 478 | vnc_client = _auth(**kwargs) |
| 479 | gsc_obj = _get_config(vnc_client) |
| 480 | database_node_objs = database_node_list(**kwargs) |
| 481 | if name in database_node_objs: |
| 482 | return {'Error': 'Database node %s already exists' % name} |
| 483 | else: |
| 484 | database_node_obj = DatabaseNode( |
| 485 | name, gsc_obj, |
| 486 | database_node_ip_address=ip_address) |
| 487 | vnc_client.database_node_create(database_node_obj) |
| 488 | ret = database_node_list(**kwargs) |
| 489 | return ret[name] |
| 490 | |
| 491 | |
| 492 | def database_node_delete(name, **kwargs): |
| 493 | ''' |
| 494 | Delete specific Contrail database node |
| 495 | |
| 496 | CLI Example: |
| 497 | |
| 498 | .. code-block:: bash |
| 499 | |
| 500 | salt '*' contrail.database_node_delete cmp01 |
| 501 | ''' |
| 502 | vnc_client = _auth(**kwargs) |
| 503 | gsc_obj = _get_database(vnc_client) |
| 504 | database_node_obj = databaseNode(name, gsc_obj) |
| 505 | vnc_client.database_node_delete( |
| 506 | fq_name=database_node_obj.get_fq_name()) |