Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame^] | 1 | # |
| 2 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 3 | # |
| 4 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 5 | # "License"); you may not use this file except in compliance with the |
| 6 | # License. You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.eclipse.org/legal/epl-v10.html |
| 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 |
| 13 | # implied. See the License for the specific language governing |
| 14 | # permissions and limitations under the License. |
| 15 | # |
| 16 | |
| 17 | # |
| 18 | # |
| 19 | |
| 20 | import command |
| 21 | def tenant_origin_external(data): |
| 22 | #Return origin-name when the tenant wasn't created by the cli, |
| 23 | #return None otherwise. |
| 24 | pk = command.mi.pk('tenant') |
| 25 | if not pk in data: |
| 26 | return None; |
| 27 | |
| 28 | tenant = command.sdnsh.get_table_from_store('tenant', |
| 29 | pk, |
| 30 | data[pk]) |
| 31 | if len(tenant) == 0: |
| 32 | return None |
| 33 | |
| 34 | local = ['cli', 'rest'] |
| 35 | if 'origin' in tenant[0] and not tenant[0]['origin'] in local: |
| 36 | return tenant[0]['origin'] |
| 37 | return None |
| 38 | |
| 39 | def tenant_warn_external_config(data): |
| 40 | """ |
| 41 | From the named tenant, look up the entry, if it exists in the |
| 42 | database, validate the 'origin' is either null, or 'cli', |
| 43 | otherwise provide a warning about this particular tenant |
| 44 | (along with the originator name) |
| 45 | """ |
| 46 | external_origin = tenant_origin_external(data) |
| 47 | if external_origin: |
| 48 | command.sdnsh.warning('tenant %s may not be intended for cli update, ' |
| 49 | 'origin/creator "%s" ' % (data['name'], external_origin)) |
| 50 | |
| 51 | def tenant_show_preprocess(obj_type, data,scoped=None): |
| 52 | data['tenant']='default' |
| 53 | settingscope=True |
| 54 | if 'name' in data: |
| 55 | data['tenant']= data['name'] |
| 56 | del data['name'] |
| 57 | else: |
| 58 | current_mode=command.sdnsh.current_mode() |
| 59 | if not current_mode.startswith('config-tenant'): |
| 60 | data['tenant']='default' |
| 61 | if current_mode.startswith('config-tenant'): |
| 62 | settingscope=False |
| 63 | for x in command.sdnsh.mode_stack: |
| 64 | if x['mode_name'] == 'config-tenant': |
| 65 | data['tenant'] = x['obj'] |
| 66 | if obj_type=='vns-definition' and data['tenant']=='default' and scoped=='True' and settingscope: |
| 67 | data['scoped']=False |
| 68 | if 'vnsname' in data and data['vnsname']!='all': |
| 69 | if (obj_type!='vns-definition'): |
| 70 | data['vns']=data['tenant'] +'|'+data['vnsname'] |
| 71 | if 'vnsname' in data and data['vnsname']=='all': |
| 72 | data['vns']='all' |
| 73 | del data['vnsname'] |
| 74 | if 'vns' in data and data['vns']=='all': |
| 75 | if scoped=='True': |
| 76 | data['scoped']=False |
| 77 | del data['tenant'] |
| 78 | if 'vrname' in data: |
| 79 | if (obj_type!='virtualrouter'): |
| 80 | data['virtual-router']=data['tenant'] +'|'+data['vrname'] |
| 81 | if 'vriname' in data: |
| 82 | if (obj_type!='virtualrouter-interface'): |
| 83 | data['virtual-router-interface']=data['tenant'] +'|'+data['vrname'] +'|' + data['vriname'] |
| 84 | if 'vrgwname' in data: |
| 85 | if (obj_type!='virtualrouter-gwpool'): |
| 86 | data['virtual-router-gwpool']=data['tenant'] +'|'+data['vrname'] +'|' + data['vrgwname'] |
| 87 | |
| 88 | """ |
| 89 | command.add_action('tenant-show-preprocess', tenant_show_preprocess, |
| 90 | {'kwargs': {'obj_type': '$obj-type', |
| 91 | 'data' : '$data', |
| 92 | 'scoped' : '$scoped',}}) |
| 93 | command.add_action('tenant-warn-external-config', tenant_warn_external_config, |
| 94 | {'kwargs': {'data' : '$data',}}) |
| 95 | |
| 96 | def complete_tenant_preprocess(data): |
| 97 | if 'name' in data: |
| 98 | data['tenant'] =data['name'] |
| 99 | |
| 100 | command.add_completion('complete-tenant-preprocess', complete_tenant_preprocess, |
| 101 | {'kwargs': {'data': '$data', |
| 102 | }}) |
| 103 | # |
| 104 | # ---------------------------------------------------------------------- |
| 105 | # tenant submode commands |
| 106 | # |
| 107 | TENANT_COMMAND_DESCRIPTION = { |
| 108 | 'name' : 'tenant', |
| 109 | 'help' : 'Enter tenant definition submode', |
| 110 | 'mode' : 'config*', |
| 111 | 'command-type' : 'config-submode', |
| 112 | 'obj-type' : 'tenant', |
| 113 | 'parent-field' : None, |
| 114 | 'current-mode-obj-id' : None, |
| 115 | 'submode-name' : 'config-tenant', |
| 116 | 'feature' : 'vns', |
| 117 | 'short-help' : 'Enter tenant definition submode', |
| 118 | 'doc' : 'vns|tenant', |
| 119 | 'doc-example' : 'vns|tenant-example', |
| 120 | 'args' : ( |
| 121 | { |
| 122 | 'field' : 'name', |
| 123 | 'type' : 'identifier', |
| 124 | 'completion' : 'complete-object-field', |
| 125 | 'syntax-help' : 'Enter a tenant name', |
| 126 | 'doc' : 'tenant|tenant-id', |
| 127 | 'doc-example' : 'tenant|tenant-id-example', |
| 128 | 'doc-include' : [ 'type-doc' ], |
| 129 | 'action' : ( |
| 130 | { |
| 131 | 'proc' : 'tenant-warn-external-config', |
| 132 | }, |
| 133 | { |
| 134 | 'proc' : 'push-mode-stack', |
| 135 | }, |
| 136 | ), |
| 137 | 'no-action' : ( |
| 138 | { |
| 139 | 'proc' : 'tenant-warn-external-config', |
| 140 | }, |
| 141 | { |
| 142 | 'proc' : 'delete-objects', |
| 143 | }, |
| 144 | ) |
| 145 | } |
| 146 | ), |
| 147 | } |
| 148 | |
| 149 | |
| 150 | tenant_show_action = ( |
| 151 | { |
| 152 | 'proc' : 'query-table', |
| 153 | }, |
| 154 | { |
| 155 | 'proc' : 'join-table', |
| 156 | 'obj-type' : 'virtualrouter', |
| 157 | 'key' : 'tenant', |
| 158 | 'join-field' : 'name', |
| 159 | 'add-field' : 'virtualrouter|vrname', |
| 160 | }, |
| 161 | { |
| 162 | 'proc' : 'display', |
| 163 | 'format' : 'tenant', |
| 164 | }, |
| 165 | ) |
| 166 | |
| 167 | TENANT_SHOW_COMMAND_DESCRIPTION = { |
| 168 | 'name' : 'show', |
| 169 | 'mode' : 'login', |
| 170 | 'all-help' : 'Show tenant details', |
| 171 | 'short-help' : 'Show defined tenants', |
| 172 | 'command-type' : 'display-table', |
| 173 | 'obj-type' : 'tenant', |
| 174 | 'action' : tenant_show_action, |
| 175 | 'args' : ( |
| 176 | 'tenant', |
| 177 | ) |
| 178 | } |
| 179 | |
| 180 | TENANT_SHOW_ID_INFO_COMMAND_DESCRIPTION = { |
| 181 | 'name' : 'show', |
| 182 | 'obj-type' : 'tenant', |
| 183 | 'mode' : 'login', |
| 184 | 'command-type' : 'display-table', |
| 185 | 'action' : tenant_show_action, |
| 186 | 'short-help' : 'Show specific tenant, identified by name', |
| 187 | 'doc' : 'vns|show-tenant', |
| 188 | 'doc-example' : 'vns|show-tenant-example', |
| 189 | 'args' : ( |
| 190 | 'tenant', |
| 191 | { |
| 192 | 'choices' : ( |
| 193 | { |
| 194 | 'field' : 'name', |
| 195 | 'type' : 'identifier', |
| 196 | 'completion' : 'complete-object-field', |
| 197 | 'help-name' : "tenant-id", |
| 198 | }, |
| 199 | { |
| 200 | 'field' : 'name', |
| 201 | 'type' : 'enum', |
| 202 | 'values' : 'all', |
| 203 | } |
| 204 | ), |
| 205 | }, |
| 206 | ) |
| 207 | } |
| 208 | |
| 209 | TENANT_SHOW_ID_COMMAND_DESCRIPTION = { |
| 210 | 'name' : 'show', |
| 211 | 'obj-type' : 'tenant', |
| 212 | 'mode' : 'login', |
| 213 | 'command-type' : 'display-table', |
| 214 | 'action' : tenant_show_action, |
| 215 | 'short-help' : 'Show specific tenant, identified by name', |
| 216 | 'doc' : 'vns|show-tenant', |
| 217 | 'args' : ( |
| 218 | 'tenant', |
| 219 | { |
| 220 | 'field' : 'name', |
| 221 | 'type' : 'identifier', |
| 222 | 'completion' : 'complete-object-field', |
| 223 | 'help-name' : "tenant-id", |
| 224 | }, |
| 225 | { |
| 226 | 'choices' : ( |
| 227 | ( |
| 228 | { |
| 229 | 'field' : 'vns-field', |
| 230 | 'type' : 'enum', |
| 231 | 'values' : 'vns', |
| 232 | 'action' : ({ 'proc' : 'tenant-show-preprocess'}, |
| 233 | { 'proc' : 'display-table'}, |
| 234 | ), |
| 235 | 'obj-type' : 'vns-definition', |
| 236 | 'short-help' : 'Show VNS belonged to this tenant', |
| 237 | 'doc' : 'vns|show-tenant-id-vns', |
| 238 | }, |
| 239 | { 'optional' : True, |
| 240 | 'args' : ( |
| 241 | { |
| 242 | 'field' : 'vnsname', |
| 243 | 'type' : 'identifier', |
| 244 | 'scoped' : 'tenant', |
| 245 | 'explicit' : True, |
| 246 | 'completion' : ['complete-tenant-preprocess', |
| 247 | 'complete-from-another'], |
| 248 | 'other' : 'vns-definition|vnsname', |
| 249 | 'help-name' : "vns-id", |
| 250 | }, |
| 251 | { 'optional' : True, |
| 252 | 'choices' : ( |
| 253 | { |
| 254 | 'field' : 'vns-interface', |
| 255 | 'type' : 'enum', |
| 256 | 'values' : 'interfaces', |
| 257 | 'obj-type' : 'vns-interface', |
| 258 | 'action' : ( |
| 259 | {'proc' : 'tenant-show-preprocess'}, |
| 260 | 'legacy-cli'), |
| 261 | 'short-help' : 'Show VNS associated interfaces', |
| 262 | }, |
| 263 | { |
| 264 | 'field' : 'mac-address-table', |
| 265 | 'type' : 'enum', |
| 266 | 'values' : 'mac-address-table', |
| 267 | 'obj-type' : 'host-vns-interface-vns', |
| 268 | 'action' : ({'proc' : 'tenant-show-preprocess'}, |
| 269 | 'legacy-cli'), |
| 270 | 'short-help' : 'Show VNS associated mac addresses', |
| 271 | }, |
| 272 | { |
| 273 | 'field' : 'interface-rules', |
| 274 | 'type' : 'enum', |
| 275 | 'values' : 'interface-rules', |
| 276 | 'action' : ( |
| 277 | { |
| 278 | 'proc' : 'tenant-show-preprocess', |
| 279 | }, |
| 280 | 'display-table', |
| 281 | ), |
| 282 | 'obj-type' : 'vns-interface-rule', |
| 283 | 'short-help' : 'Show VNS configured interfaces-rules', |
| 284 | 'doc' : 'vns|show-id-interface-rules', |
| 285 | }, |
| 286 | { |
| 287 | 'field' : 'access-lists', |
| 288 | 'type' : 'enum', |
| 289 | 'values' : 'access-lists', |
| 290 | 'action' : ( |
| 291 | { |
| 292 | 'proc' : 'tenant-show-preprocess', |
| 293 | }, |
| 294 | 'display-table', |
| 295 | ), |
| 296 | 'obj-type' : 'vns-access-list', |
| 297 | 'detail' : 'acl-brief', |
| 298 | 'short-help' : 'Show VNS configured access-lists', |
| 299 | 'doc' : 'vns|show-id-access-lists', |
| 300 | }, |
| 301 | { |
| 302 | 'field' : 'running-config', |
| 303 | 'type' : 'enum', |
| 304 | 'values' : 'running-config', |
| 305 | 'action' : ({'proc' : 'tenant-show-preprocess'}, |
| 306 | 'legacy-cli'), |
| 307 | 'short-help' : 'Show VNS running-config', |
| 308 | }, |
| 309 | { |
| 310 | 'field' : 'legacy-cli', |
| 311 | 'type' : 'enum', |
| 312 | 'values' : 'switch', |
| 313 | 'action' : ({'proc' : 'tenant-show-preprocess'}, |
| 314 | 'legacy-cli'), |
| 315 | 'obj-type' : 'vns-switch-ports', |
| 316 | 'short-help' : 'Show VNS associated switches', |
| 317 | }, |
| 318 | ( |
| 319 | { |
| 320 | 'field' : 'legacy-cli', |
| 321 | 'type' : 'enum', |
| 322 | 'values' : 'flow', |
| 323 | 'obj-type' : 'vns-flow', |
| 324 | 'action' : ({'proc' : 'tenant-show-preprocess'}, |
| 325 | 'legacy-cli'), |
| 326 | 'short-help' : 'Show VNS associated flows', |
| 327 | 'doc' : 'vns|show-id-flow', |
| 328 | }, |
| 329 | { |
| 330 | 'field' : 'detail', |
| 331 | 'type' : 'enum', |
| 332 | 'values' : ('brief', |
| 333 | 'full-detail', |
| 334 | 'details', |
| 335 | 'summary', |
| 336 | ), |
| 337 | 'optional' : True, |
| 338 | 'short-help' : 'Detail level', |
| 339 | 'doc' : 'format|+' |
| 340 | } |
| 341 | ) |
| 342 | ) |
| 343 | } |
| 344 | ) |
| 345 | } |
| 346 | ), |
| 347 | ( |
| 348 | { |
| 349 | 'field' : 'router', |
| 350 | 'type' : 'enum', |
| 351 | 'values' : 'router', |
| 352 | 'doc' : 'vns|show-tenant-id-router', |
| 353 | }, |
| 354 | { 'optional' : True, |
| 355 | 'args' : ( |
| 356 | { |
| 357 | 'field' : 'vrname', |
| 358 | 'type' : 'identifier', |
| 359 | 'scoped' : 'tenant', |
| 360 | 'explicit' : True, |
| 361 | 'completion' : ['complete-tenant-preprocess', |
| 362 | 'complete-from-another'], |
| 363 | 'other' : 'virtualrouter|vrname', |
| 364 | 'help-name' : "virtualrouter-id", |
| 365 | }, |
| 366 | {'optional' : True, |
| 367 | 'choices' : ( |
| 368 | { |
| 369 | 'token' : 'ip-address-pool', |
| 370 | 'obj-type' : 'interface-address-pool', |
| 371 | 'doc' : 'vns|show-tenant-id-router-ippool', |
| 372 | 'action' : ( |
| 373 | { 'proc' : 'tenant-show-preprocess'}, |
| 374 | { 'proc' : 'query-table'}, |
| 375 | { 'proc' : 'display', |
| 376 | 'format' : 'interface-address-pool'}, |
| 377 | ), |
| 378 | }, |
| 379 | { |
| 380 | 'field' : 'route', |
| 381 | 'type' : 'enum', |
| 382 | 'values' : 'route', |
| 383 | 'obj-type' : 'virtualrouter-routingrule', |
| 384 | 'doc' : 'vns|show-tenant-id-router-route', |
| 385 | 'action' : ( |
| 386 | { 'proc' : 'tenant-show-preprocess'}, |
| 387 | { 'proc' : 'query-table',}, |
| 388 | { |
| 389 | 'proc' : 'display', |
| 390 | 'format' : 'virtualrouter-routingrule'}, |
| 391 | ), |
| 392 | }, |
| 393 | { |
| 394 | 'field' : 'interfaces', |
| 395 | 'type' : 'enum', |
| 396 | 'values' : 'interfaces', |
| 397 | 'obj-type' : 'virtualrouter-interface', |
| 398 | 'doc' : 'vns|show-tenant-id-router-interfaces', |
| 399 | 'action' : ( |
| 400 | { 'proc' : 'tenant-show-preprocess'}, |
| 401 | { 'proc' : 'query-table'}, |
| 402 | { 'proc' : 'display', |
| 403 | 'format' : 'virtualrouter-interface'}, |
| 404 | ), |
| 405 | }, |
| 406 | { |
| 407 | 'field' : 'gateway-pools', |
| 408 | 'type' : 'enum', |
| 409 | 'values' : 'gateway-pools', |
| 410 | 'obj-type' : 'virtualrouter-gwpool', |
| 411 | 'doc' : 'vns|show-tenant-id-router-gwpools', |
| 412 | 'action' : ( |
| 413 | { 'proc' : 'tenant-show-preprocess'}, |
| 414 | { 'proc' : 'query-table'}, |
| 415 | { 'proc' : 'display', |
| 416 | 'format' : 'virtualrouter-gwpool'}, |
| 417 | ), |
| 418 | }, |
| 419 | { |
| 420 | 'token' : 'gw-address-pool', |
| 421 | 'obj-type' : 'gateway-address-pool', |
| 422 | 'doc' : 'vns|show-tenant-id-router-gwippool', |
| 423 | 'action' : ( |
| 424 | { 'proc' : 'tenant-show-preprocess'}, |
| 425 | { 'proc' : 'query-table'}, |
| 426 | { 'proc' : 'display', |
| 427 | 'format' : 'gateway-address-pool'}, |
| 428 | ), |
| 429 | }, |
| 430 | ), |
| 431 | }, |
| 432 | ), |
| 433 | }, |
| 434 | ), |
| 435 | ( |
| 436 | { 'field' :'running-config', |
| 437 | 'type' : 'enum', |
| 438 | 'values' : 'running-config', |
| 439 | 'action' : ({'proc' : 'tenant-show-preprocess'}, |
| 440 | 'legacy-cli'), |
| 441 | } |
| 442 | ), |
| 443 | ( |
| 444 | { |
| 445 | 'field' : 'detail', |
| 446 | 'type' : 'enum', |
| 447 | 'values' : ('details', 'brief'), |
| 448 | 'doc' : 'format|+', |
| 449 | } |
| 450 | ) |
| 451 | ) |
| 452 | } |
| 453 | ) |
| 454 | } |
| 455 | |
| 456 | TENANT_DEF_DESCRIPTION_COMMAND_DESCRIPTION = { |
| 457 | 'name' : 'description', |
| 458 | 'mode' : 'config-tenant', |
| 459 | 'command-type' : 'config', |
| 460 | 'short-help' : 'Provide description for a tenant instance', |
| 461 | 'doc' : 'vns|tenant-description', |
| 462 | 'doc-example' : 'vns|tenant-description-example', |
| 463 | 'args' : ( |
| 464 | { |
| 465 | 'field' : 'description', |
| 466 | 'type' : 'string', |
| 467 | } |
| 468 | ), |
| 469 | } |
| 470 | |
| 471 | |
| 472 | TENANT_DEF_ACTIVE_COMMAND_DESCRIPTION = { |
| 473 | 'name' : 'active', |
| 474 | 'mode' : 'config-tenant', |
| 475 | 'short-help' : 'Set tenant active', |
| 476 | 'doc' : 'vns|tenant-active', |
| 477 | 'doc-example' : 'vns|tenant-active-example', |
| 478 | 'doc-include' : [ 'default' ], |
| 479 | 'args' : (), |
| 480 | 'action' : ( |
| 481 | { |
| 482 | 'proc' : 'write-fields', |
| 483 | 'data' : { 'active' : True }, |
| 484 | 'syntax-help' : 'mark the tenant as active', |
| 485 | } |
| 486 | ), |
| 487 | 'no-action' : ( |
| 488 | { |
| 489 | 'proc' : 'write-fields', |
| 490 | 'data' : { 'active' : False }, |
| 491 | 'syntax-help' : 'mark the tenant as inactive', |
| 492 | } |
| 493 | ) |
| 494 | } |
| 495 | |
| 496 | TENANT_DEF_ORIGIN_COMMAND_DESCRIPTION = { |
| 497 | 'name' : 'origin', |
| 498 | 'mode' : 'config-tenant', |
| 499 | 'command-type' : 'config', |
| 500 | 'short-help' : 'Describe tenant origin', |
| 501 | 'doc' : 'tenant|origin', |
| 502 | 'doc-example' : 'tenant|origin-example', |
| 503 | 'args' : ( |
| 504 | { |
| 505 | 'field' : 'origin', |
| 506 | 'type' : 'string', |
| 507 | 'action' : ( |
| 508 | { |
| 509 | 'proc' : 'tenant-warn-external-config', |
| 510 | }, |
| 511 | { |
| 512 | 'proc' : 'write-fields', |
| 513 | }, |
| 514 | ), |
| 515 | }, |
| 516 | ), |
| 517 | } |
| 518 | |
| 519 | # |
| 520 | # FORMATS |
| 521 | TENANT_FORMAT = { |
| 522 | 'tenant' : { |
| 523 | 'field-orderings' : { |
| 524 | 'default' : [ 'Idx', 'name', 'active', 'description', 'virtualrouter'], |
| 525 | 'details' : [ 'Idx', 'name', 'active', 'description', 'virtualrouter'], |
| 526 | 'brief' : [ 'Idx', 'name', 'active'], |
| 527 | }, |
| 528 | 'fields' : { |
| 529 | 'name' : { 'verbose-name' : 'Tenant ID', |
| 530 | }, |
| 531 | 'active' : { |
| 532 | }, |
| 533 | 'description' : { |
| 534 | }, |
| 535 | 'virtualrouter' : {'verbose-name' : 'Router ID', |
| 536 | } |
| 537 | |
| 538 | } |
| 539 | }, |
| 540 | } |
| 541 | """ |