blob: 9dfe17f90a2ccf8fc48f7aec860eeb8e8aab1c0f [file] [log] [blame]
Srikanth Vavilapalli1725e492014-12-01 17:50:52 -08001#
2# Copyright (c) 2011,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
17import command
18import fmtcnv
19'''
20def address_space_origin_external (data):
21 """
22 Return origin-name when the address space wasn't created by the cli,
23 return None otherwise.
24 """
25 pk = command.mi.pk('address-space')
26 if not pk in data:
27 return;
28
29 address_space = command.sdnsh.get_table_from_store('address-space', pk,
30 data[pk])
31 if len(address_space) == 0:
32 return None
33
34 local = ['cli', 'rest']
35 if 'origin' in address_space[0] and not address_space[0]['origin'] in local:
36 return address_space[0]['origin']
37 return None
38
39
40def address_space_warn_external_config (data):
41 """
42 From the named address-space, look up the entry, if it exists in the
43 database, validate the 'origin' is either null, or 'cli',
44 otherwise provide a warning about this particular address-space
45 (along with the originator name)
46 """
47 external_origin = address_space_origin_external(data)
48 if external_origin:
49 command.sdnsh.warning(
50 'address-space %s may not be intended for cli update, '
51 'origin/creator "%s" ' % (data['name'], external_origin))
52
53
54command.add_action('address-space-warn-external-config',
55 address_space_warn_external_config,
56 {'kwargs': {'data' : '$data',}})
57
58
59def address_space_confirm_external_delete (data):
60 """
61 From the named address-space, look up the entry, if it exists in the
62 database, validate the 'origin' is either null, or 'cli',
63 otherwise provide a warning about this particular address-space
64 (along with the originator name)
65 """
66 external_origin = address_space_origin_external(data)
67 if external_origin:
68 confirm = command.action_registry['confirm'][0] # XXX accessor?
69 confirm('address-space %s may not be intended for cli delete, '
70 'origin/creator "%s"\nEnter y or yes to continue delete: '
71 % (data['name'], external_origin))
72
73command.add_action('address-space-confirm-external-delete',
74 address_space_confirm_external_delete,
75 {'kwargs': {'data' : '$data',}})
76'''
77"""
78#
79# ----------------------------------------------------------------------
80# address-space submode configuration commands
81#
82
83#
84# conf: [no] address-space <address-space-name>
85#
86ADDRESS_SPACE_COMMAND_DESCRIPTION = {
87 'name' : 'address-space',
88 'help' : 'Enter Address-space submode',
89 'mode' : 'config*',
90 'command-type' : 'config-submode',
91 'obj-type' : 'address-space',
92 'submode-name' : 'config-address-space',
93 'short-help' : 'Enter address space submode',
94 'doc' : 'address-space|address-space',
95 'doc-example' : 'address-space|address-space-example',
96 'args' : (
97 {
98 'field' : 'name',
99 'type' : 'identifier',
100 'completion' : 'complete-object-field',
101 'syntax-help' : 'Enter an address-space name',
102 'doc' : 'address-space|identifier',
103 'doc-include' : [ 'type-doc' ],
104 'action' : (
105 {
106 'proc' : 'address-space-warn-external-config',
107 },
108 {
109 'proc' : 'push-mode-stack',
110 },
111 ),
112 'no-action' : (
113 {
114 'proc' : 'address-space-confirm-external-delete',
115 },
116 {
117 'proc' : 'delete-objects',
118 },
119 )
120 }
121 ),
122}
123
124#
125# conf/address-space <as>/active {True | False}
126#
127ADDRESS_SPACE_ACTIVE_COMMAND_DESCRIPTION = {
128 'name' : 'active',
129 'short-help' : 'Set address-space active',
130 'mode' : 'config-address-space',
131 'doc' : 'address-space|active',
132 'doc-example' : 'address-space|active-example',
133 'doc-include' : [ 'default' ],
134 'args' : (),
135 'action' : (
136 {
137 'proc' : 'write-fields',
138 'data' : { 'active' : True },
139 'syntax-help' : 'mark the address-space as active',
140 }
141 ),
142 'no-action' : (
143 {
144 'proc' : 'write-fields',
145 'data' : { 'active' : False },
146 'syntax-help' : 'mark the address-space as inactive',
147 }
148 )
149}
150
151#
152# conf/address-space <as>/origin {cli | rest | ... }
153#
154ADDRESS_SPACE_ORIGIN_COMMAND_DESCRIPTION = {
155 'name' : 'origin',
156 'short-help' : 'Describe address-space origin',
157 'mode' : 'config-address-space',
158 'command-type' : 'config',
159 'doc' : 'address-space|origin',
160 'doc-example' : 'address-space|origin-example',
161 'args' : (
162 {
163 'field' : 'origin',
164 'type' : 'string',
165 'action' : (
166 {
167 'proc' : 'address-space-warn-external-config',
168 },
169 {
170 'proc' : 'write-fields',
171 },
172 ),
173 },
174 ),
175}
176
177#
178# conf/address-space <as>/priority <pri>
179#
180ADDRESS_SPACE_PRIORITY_COMMAND_DESCRIPTION = {
181 'name' : 'priority',
182 'short-help' : 'Set address-space priority',
183 'mode' : 'config-address-space',
184 'command-type' : 'config',
185 'doc' : 'address-space|priority',
186 'doc-example' : 'address-space|priority-example',
187 'doc-include' : [ 'range', 'default' ],
188 'args' : (
189 {
190 'field' : 'priority',
191 'base-type' : 'integer',
192 'range' : (0, 65535),
193 }
194 )
195}
196"""
197
198def address_space_check_unique_vlan(data):
199 """
200 Peek to see if the vlan is in use by any other address-space
201 """
202 vlan_field = 'vlan-tag-on-egress'
203 in_use = command.sdnsh.get_table_from_store('address-space',
204 vlan_field,
205 str(data[vlan_field]))
206 vlan_field = 'vlan-tag-on-egress'
207 if len(in_use) > 0:
208 current_as = command.sdnsh.get_current_mode_obj()
209 other_as = [x['name'] for x in in_use if x['name'] != current_as]
210 if len(other_as):
211 command.sdnsh.warning(
212 'vlan %s currently in use by other address-spaces: %s' %
213 (data[vlan_field], ', '.join(other_as)))
214
215
216command.add_action('address-space-check-unique-vlan',
217 address_space_check_unique_vlan,
218 {'kwargs': {'data' : '$data',}})
219
220"""
221#
222# conf/address-space <as>/vlan-tag-on-egress <tag>
223#
224ADDRESS_SPACE_VLAN_TAG_ON_EGRESS_COMMAND_DESCRIPTION = {
225 'name' : 'vlan-tag-on-egress',
226 'short-help' : 'Egress VLAN tag',
227 'mode' : 'config-address-space',
228 'command-type' : 'config',
229 'doc' : 'address-space|vlan-tag-on-egress',
230 'doc-example' : 'address-space|vlan-tag-on-egress-example',
231 'args' : (
232 {
233 'field' : 'vlan-tag-on-egress',
234 'base-type' : 'integer',
235 'range' : (1, 4096),
236 'action' : (
237 {
238 'proc' : 'address-space-check-unique-vlan',
239 },
240 {
241 'proc' : 'write-fields',
242 },
243 ),
244 }
245 )
246}
247
248#
249# conf/address-space <as>/description <descr>
250#
251ADDRESS_SPACE_DESCRIPTION_COMMAND_DESCRIPTION = {
252 'name' : 'description',
253 'short-help' : 'Provide description for this address-space',
254 'mode' : 'config-address-space',
255 'command-type' : 'config',
256 'doc' : 'address-space|description',
257 'doc-example' : 'address-space|description-example',
258 'args' : (
259 {
260 'field' : 'description',
261 'type' : 'string',
262 }
263 ),
264}
265
266#
267# conf/address-space <as>/identifier-rule <ir>
268#
269ADDRESS_SPACE_IDENTIFIER_RULE_COMMAND_DESCRIPTION = {
270 'name' : 'identifier-rule',
271 'short-help' : 'Set an address-space identifier rule',
272 'mode' : 'config-address-space*',
273 'command-type' : 'config-submode',
274 'obj-type' : 'address-space-identifier-rule',
275 'parent-field' : 'address-space',
276 'submode-name' : 'config-address-space-id-rule',
277 'doc' : 'address-space|identifier-rule',
278 'doc-example' : 'address-space|identifier-rule-example',
279 'args' : (
280 {
281 'field' : 'rule',
282 'type' : 'identifier',
283 'syntax-help' : 'Enter an address-space identifier rule name',
284 'completion' : 'complete-object-field',
285 'doc-include' : [ 'type-doc' ],
286 'doc' : 'address-space|id-rule-identifier',
287 'scoped' : True,
288 }
289 )
290}
291
292#
293# ----------------------------------------------------------------------
294# address-space identifier rules submode
295#
296
297#
298# conf/address-space <as>/identifier-rule <ir>/active {True | False}
299#
300ADDRESS_SPACE_ID_ACTIVE_COMMAND_DESCRIPTION = {
301 'name' : 'active',
302 'mode' : 'config-address-space-id-rule',
303 'short-help' : 'Set rule to Active',
304 'command-type' : 'config',
305 'doc' : 'address-space|id-active',
306 'doc-example' : 'address-space|id-active-example',
307 'doc-include' : [ 'default' ],
308 'args' : (),
309 'action' : (
310 {
311 'proc' : 'write-fields',
312 'data' : { 'active' : True }
313 }
314 ),
315 'no-action' : (
316 {
317 'proc' : 'write-fields',
318 'data' : { 'active' : False }
319 }
320 )
321}
322
323#
324# conf/address-space <as>/identifier-rule <ir>/description <descr>
325#
326ADDRESS_SPACE_ID_DESCRIPTION_COMMAND_DESCRIPTION = {
327 'name' : 'description',
328 'short-help' : 'Provide description for identifier rule',
329 'mode' : 'config-address-space-id-rule',
330 'command-type' : 'config',
331 'doc' : 'address-space|id-description',
332 'doc-example' : 'address-space|id-description-example',
333 'args' : (
334 {
335 'field' : 'description',
336 'type' : 'string',
337 }
338 ),
339}
340
341#
342# conf/address-space <as>/identifier-rule <ir>/priority <pri>
343#
344ADDRESS_SPACE_ID_PRIORITY_COMMAND_DESCRIPTION = {
345 'name' : 'priority',
346 'short-help' : 'Describe priority for identifier rule',
347 'mode' : 'config-address-space-id-rule',
348 'command-type' : 'config',
349 'doc' : 'address-space|id-priority',
350 'doc-include' : [ 'range', 'default' ],
351 'doc-example' : 'address-space|id-priority-example',
352 'args' : (
353 {
354 'field' : 'priority',
355 'value' : 'integer',
356 }
357 ),
358}
359
360#
361# conf/address-space <as>/identifier-rule <ir>/switch <swt-dpid>
362#
363ADDRESS_SPACE_ID_MATCH_SWITCH_COMMAND_DESCRIPTION = {
364 'name' : 'match',
365 'short-help' : 'Associate switch with identifier rule',
366 'all-help' : 'Configure a match for this identifier rule',
367 'mode' : 'config-address-space-id-rule',
368 'command-type' : 'config',
369 'obj-type' : 'address-space-identifier-rule',
370 'doc-all' : 'address-space|match',
371 'doc-example' : 'address-space|match-switch-example',
372 'data' : { # for no command reset
373 'switch' : None,
374 'ports' : None,
375 },
376 'args' : (
377 {
378 'token' : 'switch',
379 'doc' : 'address-space|match-switch',
380 },
381 {
382 # constructed as args to ensure that the ports isn't
383 # completed along with swtich values for 'no match switch'
384 'optional-for-no' : True,
385 'args' : (
386 {
387 'field' : 'switch',
388 'type' : 'string',
389 'parent-field' : None,
390 'completion' : [
391 'complete-object-field',
392 'complete-alias-choice',
393 ],
394 'help-name' : 'switch dpid or switch alias',
395 'data-handler' : 'alias-to-value',
396 'other' : 'switches|dpid',
397 },
398 {
399 'field' : 'ports',
400 'optional' : True,
401 'type' : 'string',
402 'help-name' : 'switch interface, or range, or list',
403 'completion' : 'complete-interface-list',
404 'optional-for-no' : True,
405 'doc' : 'address-space|match-switch-ports',
406 },
407 ),
408 },
409 ),
410}
411
412
413#
414# conf/address-space <as>/identifier-rule <ir>/tag <name-val-pair>
415#
416ADDRESS_SPACE_ID_MATCH_TAG_COMMAND_DESCRIPTION = {
417 'name' : 'match',
418 'short-help' : 'Associate tag with identifier rule',
419 'mode' : 'config-address-space-id-rule',
420 'command-type' : 'config',
421 'data' : { # for no command reset
422 'tag' : None,
423 },
424 'doc-example' : 'address-space|match-tag-example',
425 'args' : (
426 {
427 'token' : 'tags',
428 'doc' : 'address-space|match-tag',
429 },
430 {
431 'field' : 'tag',
432 'type' : 'string',
433 'optional-for-no' : True,
434 }
435 )
436}
437
438#
439# conf/address-space <as>/identifier-rule <ir>/vlans <vl>
440#
441ADDRESS_SPACE_ID_MATCH_VLANS_COMMAND_DESCRIPTION = {
442 'name' : 'match',
443 'short-help' : 'Associate vlans with identifier rule',
444 'mode' : 'config-address-space-id-rule',
445 'command-type' : 'config',
446 'data' : { # for no command reset
447 'vlans' : None,
448 },
449 'doc-example' : 'address-space|match-vlan-example',
450 'args' : (
451 {
452 'token' : 'vlans',
453 'doc' : 'address-space|match-vlan',
454 },
455 {
456 'field' : 'vlans',
457 'value' : 'string',
458 'help-name' : 'Vlan number (1-4095) or range, or list',
459 'optional-for-no' : True,
460 }
461 ),
462}
463
464#
465# ----------------------------------------------------------------------
466# show address-space commands
467#
468
469#
470# show address-space <cr>
471#
472ADDRESS_SPACE_SHOW_COMMAND_DESCRIPTION = {
473 'name' : 'show',
474 'obj-type' : 'address-space',
475 'mode' : 'login',
476 'command-type' : 'display-table',
477 'all-help' : 'Show address space information',
478 'short-help' : 'Show all address spaces',
479 'doc' : 'address-space|show',
480 'doc-example' : 'address-space|show-example',
481 'args' : (
482 'address-space',
483 ),
484}
485
486#
487# show address-space <as> [brief | details] <cr>
488#
489ADDRESS_SPACE_SHOW_ID_COMMAND_DESCRIPTION = {
490 'name' : 'show',
491 'obj-type' : 'address-space',
492 'mode' : 'login',
493 'command-type' : 'display-table',
494 'short-help' : 'Show a specific address space',
495 'doc' : 'address-space|show-id',
496 'doc-example' : 'address-space|show-id-example',
497 'args' : (
498 'address-space',
499 {
500 'choices' : (
501 {
502 'field' : 'name',
503 'type' : 'identifier',
504 'completion' : 'complete-object-field',
505 'help-name' : "address-space-name",
506 },
507 {
508 'field' : 'name',
509 'type' : 'enum',
510 'values' : 'all',
511 }
512 ),
513 },
514 {
515 'field' : 'detail',
516 'optional' : True,
517 'type' : 'enum',
518 'values' : ('details', 'brief'),
519 'doc' : 'format|+',
520 }
521 )
522}
523
524#
525# show address-space <as> [ identifier-rules ] ...
526#
527ADDRESS_SPACE_SHOW_ID_DETAILS_COMMAND_DESCRIPTION = {
528 'name' : 'show',
529 'obj-type' : 'address-space',
530 'mode' : 'login',
531 'command-type' : 'display-table',
532 'short-help' : 'Show the configured identifier-rules for a specific address space',
533 'doc' : 'address-space|show-id-rules',
534 'doc-example' : 'address-space|show-id-rules-example',
535 'args' : (
536 'address-space',
537 {
538 'choices' : (
539 {
540 'field' : 'name',
541 'type' : 'identifier',
542 'completion' : 'complete-object-field',
543 'help-name' : "address-space-name",
544 },
545 {
546 'field' : 'name',
547 'type' : 'enum',
548 'values' : 'all',
549 }
550 ),
551 },
552 {
553 # show address-space <as> { identifier-rules }
554 'choices' : (
555 {
556 'field' : 'identifier-rules',
557 'type' : 'enum',
558 'values' : 'identifier-rules',
559 'action' : 'display-table',
560 'obj-type' : 'address-space-identifier-rule',
561 },
562 )
563 }
564 ),
565}
566
567#
568# FORMATS
569# perhaps these ought be moved next to their respective show commands
570#
571
572ADDRESS_SPACE_FORMAT = {
573 'address-space' : {
574 'field-orderings' : {
575 'default' : ['Idx', 'name', 'active', 'priority',
576 'vlan-tag-on-egress'],
577 'address-space-config' : ['Idx', 'name', 'active',
578 'vlan-tag-on-egress'],
579 'brief' : ['Idx', 'name', 'vlan-tag-on-egress'],
580 'details' : ['Idx', 'name', 'active', 'priority',
581 'description', 'vlan-tag-on-egress'],
582 },
583 'fields' : {
584 'name' : { 'verbose-name' : 'Address Space',
585 },
586 'active' : {
587 },
588 'priority' : {
589 },
590 'vlan-tag-on-egress': {
591 'verbose-name' : 'Vlan Tag on Egress',
592 },
593 'description' : {
594 },
595 }
596 },
597}
598
599
600ADDRESS_SPACE_IDENTIFIER_RULE_FORMAT = {
601 'address-space-identifier-rule' : {
602 'field-orderings': {
603 'default' : [ 'Idx', 'address-space', 'rule',
604 'active', 'priority', 'switch', 'ports',
605 'tag', 'vlans'
606 ],
607 'address-space-config' : [ 'Idx', 'rule', 'description',
608 'active', 'priority', 'switch', 'ports',
609 'tag', 'vlans'
610 ],
611 },
612 'fields': {
613 'rule' : { 'verbose-name' : 'Address Space Rule ID',
614 },
615 'address-space' : {
616 'verbose-name' : 'Address Space'
617 },
618 'active' : {
619 },
620 'description' : {
621 },
622 'ports' : {
623 },
624 'priority' : {
625 },
626 'rule' : {
627 },
628 'switch' : {
629 },
630 'tag' : {
631 },
632 'vlans' : {
633 },
634 }
635 },
636}
637"""