1dbd53c9095d9d0efd80eeb591f373a2e12b52a0
[osm/openvim.git] / vim_schema.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 ''' Definition of dictionaries schemas used by validating input
25 These dictionaries are validated using jsonschema library
26 '''
27 __author__="Alfonso Tierno"
28 __date__ ="$10-jul-2014 12:07:15$"
29
30 #
31 # SCHEMAS to validate input data
32 #
33
34 path_schema={"type":"string", "pattern":"^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
35 http_schema={"type":"string", "pattern":"^https?://[^'\"=]+$"}
36 port_schema={"type":"integer","minimum":1,"maximun":65534}
37 ip_schema={"type":"string","pattern":"^([0-9]{1,3}.){3}[0-9]{1,3}$"}
38 cidr_schema={"type":"string","pattern":"^([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{1,2}$"}
39 name_schema={"type" : "string", "minLength":1, "maxLength":255, "pattern" : "^[^,;()'\"]+$"}
40 nameshort_schema={"type" : "string", "minLength":1, "maxLength":64, "pattern" : "^[^,;()'\"]+$"}
41 nametiny_schema={"type" : "string", "minLength":1, "maxLength":12, "pattern" : "^[^,;()'\"]+$"}
42 xml_text_schema={"type" : "string", "minLength":1, "maxLength":1000, "pattern" : "^[^']+$"}
43 description_schema={"type" : ["string","null"], "maxLength":255, "pattern" : "^[^'\"]+$"}
44 id_schema_fake = {"type" : "string", "minLength":2, "maxLength":36 } #"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
45 id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
46 pci_schema={"type":"string", "pattern":"^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
47 bandwidth_schema={"type":"string", "pattern" : "^[0-9]+ *([MG]bps)?$"}
48 integer0_schema={"type":"integer","minimum":0}
49 integer1_schema={"type":"integer","minimum":1}
50 vlan_schema={"type":"integer","minimum":1,"maximun":4095}
51 vlan1000_schema={"type":"integer","minimum":1000,"maximun":4095}
52 mac_schema={"type":"string", "pattern":"^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} #must be unicast LSB bit of MSB byte ==0
53 net_bind_schema={"oneOf":[{"type":"null"},{"type":"string", "pattern":"^(default|((bridge|macvtap):[0-9a-zA-Z\.\-]{1,50})|openflow:[/0-9a-zA-Z\.\-]{1,50}(:vlan)?)$"}]}
54 yes_no_schema={"type":"string", "enum":["yes", "no"]}
55 log_level_schema={"type":"string", "enum":["DEBUG", "INFO", "WARNING","ERROR","CRITICAL"]}
56
57 config_schema = {
58 "title":"main configuration information schema",
59 "$schema": "http://json-schema.org/draft-04/schema#",
60 "type":"object",
61 "properties":{
62 "http_port": port_schema,
63 "http_admin_port": port_schema,
64 "http_host": nameshort_schema,
65 "http_url_prefix": path_schema, # it does not work yet; it's supposed to be the base path to be used by bottle, but it must be explicitly declared
66 "db_host": nameshort_schema,
67 "db_user": nameshort_schema,
68 "db_passwd": {"type":"string"},
69 "db_name": nameshort_schema,
70 "of_controller_ip": ip_schema,
71 "of_controller_port": port_schema,
72 "of_controller_dpid": nameshort_schema,
73 "of_controller_nets_with_same_vlan": {"type" : "boolean"},
74 "of_controller": nameshort_schema, #{"type":"string", "enum":["floodlight", "opendaylight"]},
75 "of_controller_module": {"type":"string"},
76 "of_user": nameshort_schema,
77 "of_password": nameshort_schema,
78 "test_mode": {"type": "boolean"}, #leave for backward compatibility
79 "mode": {"type":"string", "enum":["normal", "host only", "OF only", "development", "test"] },
80 "development_bridge": {"type":"string"},
81 "tenant_id": {"type" : "string"},
82 "image_path": path_schema,
83 "network_vlan_range_start": vlan_schema,
84 "network_vlan_range_end": vlan_schema,
85 "bridge_ifaces": {
86 "type": "object",
87 "patternProperties": {
88 "." : {
89 "type": "array",
90 "items": integer0_schema,
91 "minItems":2,
92 "maxItems":2,
93 },
94 },
95 "minProperties": 2
96 },
97 "dhcp_server": {
98 "type": "object",
99 "properties": {
100 "host" : name_schema,
101 "port" : port_schema,
102 "provider" : {"type": "string", "enum": ["isc-dhcp-server"]},
103 "user" : nameshort_schema,
104 "password" : {"type": "string"},
105 "key" : {"type": "string"},
106 "bridge_ifaces" :{
107 "type": "array",
108 "items": nameshort_schema,
109 },
110 "nets" :{
111 "type": "array",
112 "items": name_schema,
113 },
114 },
115 "required": ['host', 'provider', 'user']
116 },
117 "log_level": log_level_schema,
118 "log_level_db": log_level_schema,
119 "log_level_of": log_level_schema,
120 "network_type": {"type": "string", "enum": ["ovs", "bridge"]},
121 "ovs_controller_file_path": path_schema,
122 "ovs_controller_user": nameshort_schema,
123
124 "ovs_controller_ip": nameshort_schema
125 },
126 "patternProperties": {
127 "of_*" : {"type": ["string", "integer", "boolean"]}
128 },
129 "required": ['db_host', 'db_user', 'db_passwd', 'db_name'],
130 "additionalProperties": False
131 }
132
133
134
135 metadata_schema={
136 "type":"object",
137 "properties":{
138 "architecture": {"type":"string"},
139 "use_incremental": yes_no_schema,
140 "vpci": pci_schema,
141 "os_distro": {"type":"string"},
142 "os_type": {"type":"string"},
143 "os_version": {"type":"string"},
144 "bus": {"type":"string"},
145 "topology": {"type":"string", "enum": ["oneSocket"]}
146 }
147 }
148
149 tenant_new_schema = {
150 "title":"tenant creation information schema",
151 "$schema": "http://json-schema.org/draft-04/schema#",
152 "type":"object",
153 "properties":{
154 "tenant":{
155 "type":"object",
156 "properties":{
157 "id":id_schema,
158 "name": nameshort_schema,
159 "description":description_schema,
160 "enabled":{"type" : "boolean"}
161 },
162 "required": ["name"]
163 }
164 },
165 "required": ["tenant"],
166 "additionalProperties": False
167 }
168 tenant_edit_schema = {
169 "title":"tenant edition information schema",
170 "$schema": "http://json-schema.org/draft-04/schema#",
171 "type":"object",
172 "properties":{
173 "tenant":{
174 "type":"object",
175 "minProperties":1,
176 "properties":{
177 "name":nameshort_schema,
178 "description":description_schema,
179 "enabled":{"type" : "boolean"}
180 },
181 "additionalProperties": False,
182 }
183 },
184 "required": ["tenant"],
185 "additionalProperties": False
186 }
187 interfaces_schema={
188 "type":"array",
189 "minItems":0,
190 "items":{
191 "type":"object",
192 "properties":{
193 "name":name_schema,
194 "dedicated":{"type":"string","enum":["yes","no","yes:sriov"]},
195 "bandwidth":bandwidth_schema,
196 "vpci":pci_schema,
197 "uuid":id_schema,
198 "mac_address":mac_schema
199 },
200 "additionalProperties": False,
201 "required": ["dedicated", "bandwidth"]
202 }
203 }
204
205 extended_schema={
206 "type":"object",
207 "properties":{
208 "processor_ranking":integer0_schema,
209 "devices":{
210 "type": "array",
211 "items":{
212 "type": "object",
213 "properties":{
214 "type":{"type":"string", "enum":["usb","disk","cdrom","xml"]},
215 "vpci":pci_schema,
216 "imageRef":id_schema,
217 "xml":xml_text_schema,
218 "dev":nameshort_schema
219 },
220 "additionalProperties": False,
221 "required": ["type"]
222 }
223 },
224 "numas":{
225 "type": "array",
226 "items":{
227 "type": "object",
228 "properties":{
229 "memory":integer1_schema,
230 "cores":integer1_schema,
231 "paired-threads":integer1_schema,
232 "threads":integer1_schema,
233 "cores-id":{"type":"array","items":integer0_schema},
234 "paired-threads-id":{"type":"array","items":{"type":"array","minItems":2,"maxItems":2,"items":integer0_schema}},
235 "threads-id":{"type":"array","items":integer0_schema},
236 "interfaces":interfaces_schema
237 },
238 "additionalProperties": False,
239 "minProperties": 1,
240 #"required": ["memory"]
241 }
242 }
243 },
244 #"additionalProperties": False,
245 #"required": ["processor_ranking"]
246 }
247
248 host_data_schema={
249 "title":"hosts manual insertion information schema",
250 "type":"object",
251 "properties":{
252 "ip_name":nameshort_schema,
253 "name": name_schema,
254 "description":description_schema,
255 "user":nameshort_schema,
256 "password":nameshort_schema,
257 "features":description_schema,
258 "ranking":integer0_schema,
259 "devices":{
260 "type": "array",
261 "items":{
262 "type": "object",
263 "properties":{
264 "type":{"type":"string", "enum":["usb","disk"]},
265 "vpci":pci_schema
266 },
267 "additionalProperties": False,
268 "required": ["type"]
269 }
270 },
271 "numas":{
272 "type": "array",
273 "minItems":1,
274 "items":{
275 "type": "object",
276 "properties":{
277 "admin_state_up":{"type":"boolean"},
278 "hugepages":integer0_schema,
279 "cores":{
280 "type": "array",
281 "minItems":2,
282 "items":{
283 "type": "object",
284 "properties":{
285 "core_id":integer0_schema,
286 "thread_id":integer0_schema,
287 "status": {"type":"string", "enum":["noteligible"]}
288 },
289 "additionalProperties": False,
290 "required": ["core_id","thread_id"]
291 }
292 },
293 "interfaces":{
294 "type": "array",
295 "minItems":1,
296 "items":{
297 "type": "object",
298 "properties":{
299 "source_name":nameshort_schema,
300 "mac":mac_schema,
301 "Mbps":integer0_schema,
302 "pci":pci_schema,
303 "sriovs":{
304 "type": "array",
305 "minItems":1,
306 "items":{
307 "type": "object",
308 "properties":{
309 "source_name":{"oneOf":[integer0_schema, nameshort_schema]},
310 "mac":mac_schema,
311 "vlan":integer0_schema,
312 "pci":pci_schema,
313 },
314 "additionalProperties": False,
315 "required": ["source_name","mac","pci"]
316 }
317 },
318 "switch_port": nameshort_schema,
319 "switch_dpid": nameshort_schema,
320 },
321 "additionalProperties": False,
322 "required": ["source_name","mac","Mbps","pci"]
323 }
324 },
325 "numa_socket":integer0_schema,
326 "memory":integer1_schema
327 },
328 "additionalProperties": False,
329 "required": ["cores","numa_socket"]
330 }
331 }
332 },
333 "additionalProperties": False,
334 "required": ["ranking", "numas","ip_name","user"]
335 }
336
337 host_edit_schema={
338 "title":"hosts creation information schema",
339 "$schema": "http://json-schema.org/draft-04/schema#",
340 "type":"object",
341 "properties":{
342 "host":{
343 "type":"object",
344 "properties":{
345 "ip_name":nameshort_schema,
346 "name": name_schema,
347 "description":description_schema,
348 "user":nameshort_schema,
349 "password":nameshort_schema,
350 "admin_state_up":{"type":"boolean"},
351 "numas":{
352 "type":"array",
353 "items":{
354 "type": "object",
355 "properties":{
356 "numa_socket": integer0_schema,
357 "admin_state_up":{"type":"boolean"},
358 "interfaces":{
359 "type":"array",
360 "items":{
361 "type": "object",
362 "properties":{
363 "source_name": nameshort_schema,
364 "switch_dpid": nameshort_schema,
365 "switch_port": nameshort_schema,
366 },
367 "required": ["source_name"],
368 }
369 }
370 },
371 "required": ["numa_socket"],
372 "additionalProperties": False,
373 }
374 }
375 },
376 "minProperties": 1,
377 "additionalProperties": False
378 },
379 },
380 "required": ["host"],
381 "minProperties": 1,
382 "additionalProperties": False
383 }
384
385 host_new_schema = {
386 "title":"hosts creation information schema",
387 "$schema": "http://json-schema.org/draft-04/schema#",
388 "type":"object",
389 "properties":{
390 "host":{
391 "type":"object",
392 "properties":{
393 "id":id_schema,
394 "ip_name":nameshort_schema,
395 "name": name_schema,
396 "description":description_schema,
397 "user":nameshort_schema,
398 "password":nameshort_schema,
399 "admin_state_up":{"type":"boolean"},
400 },
401 "required": ["name","ip_name","user"]
402 },
403 "host-data":host_data_schema
404 },
405 "required": ["host"],
406 "minProperties": 1,
407 "maxProperties": 2,
408 "additionalProperties": False
409 }
410
411
412 flavor_new_schema = {
413 "title":"flavor creation information schema",
414 "$schema": "http://json-schema.org/draft-04/schema#",
415 "type":"object",
416 "properties":{
417 "flavor":{
418 "type":"object",
419 "properties":{
420 "id":id_schema,
421 "name":name_schema,
422 "description":description_schema,
423 "ram":integer0_schema,
424 "vcpus":integer0_schema,
425 "extended": extended_schema,
426 "public": yes_no_schema
427 },
428 "required": ["name"]
429 }
430 },
431 "required": ["flavor"],
432 "additionalProperties": False
433 }
434 flavor_update_schema = {
435 "title":"flavor update information schema",
436 "$schema": "http://json-schema.org/draft-04/schema#",
437 "type":"object",
438 "properties":{
439 "flavor":{
440 "type":"object",
441 "properties":{
442 "name":name_schema,
443 "description":description_schema,
444 "ram":integer0_schema,
445 "vcpus":integer0_schema,
446 "extended": extended_schema,
447 "public": yes_no_schema
448 },
449 "minProperties": 1,
450 "additionalProperties": False
451 }
452 },
453 "required": ["flavor"],
454 "additionalProperties": False
455 }
456
457 image_new_schema = {
458 "title":"image creation information schema",
459 "$schema": "http://json-schema.org/draft-04/schema#",
460 "type":"object",
461 "properties":{
462 "image":{
463 "type":"object",
464 "properties":{
465 "id":id_schema,
466 "path": {"oneOf": [path_schema, http_schema]},
467 "description":description_schema,
468 "name":name_schema,
469 "metadata":metadata_schema,
470 "public": yes_no_schema
471 },
472 "required": ["name","path"]
473 }
474 },
475 "required": ["image"],
476 "additionalProperties": False
477 }
478
479 image_update_schema = {
480 "title":"image update information schema",
481 "$schema": "http://json-schema.org/draft-04/schema#",
482 "type":"object",
483 "properties":{
484 "image":{
485 "type":"object",
486 "properties":{
487 "path":{"oneOf": [path_schema, http_schema]},
488 "description":description_schema,
489 "name":name_schema,
490 "metadata":metadata_schema,
491 "public": yes_no_schema
492 },
493 "minProperties": 1,
494 "additionalProperties": False
495 }
496 },
497 "required": ["image"],
498 "additionalProperties": False
499 }
500
501 networks_schema={
502 "type":"array",
503 "items":{
504 "type":"object",
505 "properties":{
506 "name":name_schema,
507 "bandwidth":bandwidth_schema,
508 "vpci":pci_schema,
509 "uuid":id_schema,
510 "mac_address": mac_schema,
511 "model": {"type":"string", "enum":["virtio","e1000","ne2k_pci","pcnet","rtl8139"]},
512 "type": {"type":"string", "enum":["virtual","PF","VF","VFnotShared"]}
513 },
514 "additionalProperties": False,
515 "required": ["uuid"]
516 }
517 }
518
519 server_new_schema = {
520 "title":"server creation information schema",
521 "$schema": "http://json-schema.org/draft-04/schema#",
522 "type":"object",
523 "properties":{
524 "server":{
525 "type":"object",
526 "properties":{
527 "id":id_schema,
528 "name":name_schema,
529 "description":description_schema,
530 "start":{"type":"string", "enum":["yes","no","paused"]},
531 "hostId":id_schema,
532 "flavorRef":id_schema,
533 "imageRef":id_schema,
534 "extended": extended_schema,
535 "networks":networks_schema
536 },
537 "required": ["name","flavorRef","imageRef"]
538 }
539 },
540 "required": ["server"],
541 "additionalProperties": False
542 }
543
544 server_action_schema = {
545 "title":"server action information schema",
546 "$schema": "http://json-schema.org/draft-04/schema#",
547 "type":"object",
548 "properties":{
549 "start":{"oneOf":[{"type": "null"}, {"type":"string", "enum":["rebuild","null"] }]},
550 "pause":{"type": "null"},
551 "resume":{"type": "null"},
552 "shutoff":{"type": "null"},
553 "shutdown":{"type": "null"},
554 "forceOff":{"type": "null"},
555 "terminate":{"type": "null"},
556 "createImage":{
557 "type":"object",
558 "properties":{
559 "path":path_schema,
560 "description":description_schema,
561 "name":name_schema,
562 "metadata":metadata_schema,
563 "imageRef": id_schema,
564 "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
565 },
566 "required": ["name"]
567 },
568 "rebuild":{"type": ["object","null"]},
569 "reboot":{
570 "type": ["object","null"],
571 # "properties": {
572 # "type":{"type":"string", "enum":["SOFT"] }
573 # },
574 # "minProperties": 1,
575 # "maxProperties": 1,
576 # "additionalProperties": False
577 }
578 },
579 "minProperties": 1,
580 "maxProperties": 1,
581 "additionalProperties": False
582 }
583
584 network_new_schema = {
585 "title":"network creation information schema",
586 "$schema": "http://json-schema.org/draft-04/schema#",
587 "type":"object",
588 "properties":{
589 "network":{
590 "type":"object",
591 "properties":{
592 "id":id_schema,
593 "name":name_schema,
594 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
595 "shared":{"type":"boolean"},
596 "tenant_id":id_schema,
597 "admin_state_up":{"type":"boolean"},
598 "provider:vlan":vlan_schema,
599 "provider:physical":net_bind_schema,
600 "cidr":cidr_schema,
601 "enable_dhcp": {"type":"boolean"},
602 "dhcp_first_ip": ip_schema,
603 "dhcp_last_ip": ip_schema,
604 "bind_net":name_schema, #can be name, or uuid
605 "bind_type":{"oneOf":[{"type":"null"},{"type":"string", "pattern":"^vlan:[0-9]{1,4}$"}]}
606 },
607 "required": ["name"]
608 }
609 },
610 "required": ["network"],
611 "additionalProperties": False
612 }
613
614 network_update_schema = {
615 "title":"network update information schema",
616 "$schema": "http://json-schema.org/draft-04/schema#",
617 "type":"object",
618 "properties":{
619 "network":{
620 "type":"object",
621 "properties":{
622 "name":name_schema,
623 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
624 "shared":{"type":"boolean"},
625 "tenant_id":id_schema,
626 "admin_state_up":{"type":"boolean"},
627 "provider:vlan":vlan_schema,
628 "provider:physical":net_bind_schema,
629 "cidr":cidr_schema,
630 "enable_dhcp": {"type":"boolean"},
631 # "dhcp_first_ip": ip_schema,
632 # "dhcp_last_ip": ip_schema,
633 "bind_net":name_schema, #can be name, or uuid
634 "bind_type":{"oneOf":[{"type":"null"},{"type":"string", "pattern":"^vlan:[0-9]{1,4}$"}]}
635 },
636 "minProperties": 1,
637 "additionalProperties": False
638 }
639 },
640 "required": ["network"],
641 "additionalProperties": False
642 }
643
644
645 port_new_schema = {
646 "title":"port creation information schema",
647 "$schema": "http://json-schema.org/draft-04/schema#",
648 "type":"object",
649 "properties":{
650 "port":{
651 "type":"object",
652 "properties":{
653 "id":id_schema,
654 "name":nameshort_schema,
655 "network_id":{"oneOf":[{"type": "null"}, id_schema ]},
656 "tenant_id":id_schema,
657 "mac_address": {"oneOf":[{"type": "null"}, mac_schema] },
658 "admin_state_up":{"type":"boolean"},
659 "bandwidth":bandwidth_schema,
660 "binding:switch_port":nameshort_schema,
661 "binding:vlan": {"oneOf":[{"type": "null"}, vlan_schema ]}
662 },
663 "required": ["name"]
664 }
665 },
666 "required": ["port"],
667 "additionalProperties": False
668 }
669
670 port_update_schema = {
671 "title":"port update information schema",
672 "$schema": "http://json-schema.org/draft-04/schema#",
673 "type":"object",
674 "properties":{
675 "port":{
676 "type":"object",
677 "properties":{
678 "name":nameshort_schema,
679 "network_id":{"anyOf":[{"type":"null"}, id_schema ] }
680 },
681 "minProperties": 1,
682 "additionalProperties": False
683 }
684 },
685 "required": ["port"],
686 "additionalProperties": False
687 }
688
689 localinfo_schema = {
690 "title":"localinfo information schema",
691 "$schema": "http://json-schema.org/draft-04/schema#",
692 "type":"object",
693 "properties":{
694 "files":{ "type": "object"},
695 "inc_files":{ "type": "object"},
696 "server_files":{ "type": "object"}
697 },
698 "required": ["files"]
699 }
700
701 hostinfo_schema = {
702 "title":"host information schema",
703 "$schema": "http://json-schema.org/draft-04/schema#",
704 "type":"object",
705 "properties":{
706 "iface_names":{
707 "type":"object",
708 "patternProperties":{
709 ".":{ "type": "string"}
710 },
711 "minProperties": 1
712 }
713 },
714 "required": ["iface_names"]
715 }
716
717 openflow_controller_schema = {
718 "title": "network creation information schema",
719 "$schema": "http://json-schema.org/draft-04/schema#",
720 "type": "object",
721 "properties": {
722 "ofc": {
723 "type": "object",
724 "properties": {
725 "name": name_schema,
726 "dpid": nameshort_schema,
727 "ip": nameshort_schema,
728 "port": port_schema,
729 "type": nameshort_schema,
730 "version": nametiny_schema,
731 "user": nameshort_schema,
732 "password": nameshort_schema
733 },
734 "required": ["dpid", "type", "ip", "port", "name"]
735 }
736 },
737 "required": ["ofc"],
738 "additionalProperties": False
739 }