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