| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | from jsonschema import validate as js_v, exceptions as js_e |
| 4 | |
| 5 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 6 | __version__ = "0.1" |
| 7 | version_date = "Mar 2018" |
| 8 | |
| 9 | """ |
| 10 | Validator of input data using JSON schemas for those items that not contains an OSM yang information model |
| 11 | """ |
| 12 | |
| 13 | # Basis schemas |
| 14 | patern_name = "^[ -~]+$" |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 15 | nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\.\$'\"]+$"} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 16 | passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 17 | name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"} |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 18 | string_schema = {"type": "string", "minLength": 1, "maxLength": 255} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 19 | xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"} |
| 20 | description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"} |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 21 | id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36} |
| 22 | bool_schema = {"type": "boolean"} |
| 23 | null_schema = {"type": "null"} |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 24 | # "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}$" |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 25 | id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 26 | time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 27 | pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"} |
| 28 | http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"} |
| 29 | bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"} |
| 30 | memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"} |
| 31 | integer0_schema = {"type": "integer", "minimum": 0} |
| 32 | integer1_schema = {"type": "integer", "minimum": 1} |
| 33 | path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"} |
| 34 | vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095} |
| 35 | vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095} |
| 36 | mac_schema = {"type": "string", |
| 37 | "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0 |
| 38 | # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"} |
| 39 | ip_schema = {"type": "string", |
| 40 | "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"} |
| 41 | ip_prefix_schema = {"type": "string", |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 42 | "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}" |
| 43 | "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 44 | port_schema = {"type": "integer", "minimum": 1, "maximum": 65534} |
| 45 | object_schema = {"type": "object"} |
| 46 | schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2} |
| 47 | # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]} |
| 48 | log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]} |
| 49 | checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"} |
| 50 | size_schema = {"type": "integer", "minimum": 1, "maximum": 100} |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 51 | array_edition_schema = { |
| 52 | "type": "object", |
| 53 | "patternProperties": { |
| 54 | "^\$": "Any" |
| 55 | }, |
| 56 | "additionalProperties": False, |
| 57 | "minProperties": 1, |
| 58 | } |
| 59 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 60 | |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 61 | ns_instantiate_vdu = { |
| 62 | "title": "ns action instantiate input schema for vdu", |
| 63 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 64 | "type": "object", |
| 65 | "properties": { |
| 66 | "id": name_schema, |
| 67 | "volume": { |
| 68 | "type": "array", |
| 69 | "minItems": 1, |
| 70 | "items": { |
| 71 | "type": "object", |
| 72 | "properties": { |
| 73 | "name": name_schema, |
| 74 | "vim-volume-id": name_schema, |
| 75 | }, |
| 76 | "required": ["name", "vim-volume-id"], |
| 77 | "additionalProperties": False |
| 78 | } |
| 79 | }, |
| 80 | "interface": { |
| 81 | "type": "array", |
| 82 | "minItems": 1, |
| 83 | "items": { |
| 84 | "type": "object", |
| 85 | "properties": { |
| 86 | "name": name_schema, |
| 87 | "ip-address": ip_schema, |
| 88 | "mac-address": mac_schema, |
| 89 | "floating-ip-required": bool_schema, |
| 90 | }, |
| 91 | "required": ["name"], |
| 92 | "additionalProperties": False |
| 93 | } |
| 94 | } |
| 95 | }, |
| 96 | "required": ["id"], |
| 97 | "additionalProperties": False |
| 98 | } |
| 99 | |
| 100 | ip_profile_dns_schema = { |
| 101 | "type": "array", |
| 102 | "minItems": 1, |
| 103 | "items": { |
| 104 | "type": "object", |
| 105 | "properties": { |
| 106 | "address": ip_schema, |
| 107 | }, |
| 108 | "required": ["address"], |
| 109 | "additionalProperties": False |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | ip_profile_dhcp_schema = { |
| 114 | "type": "object", |
| 115 | "properties": { |
| 116 | "enabled": {"type": "boolean"}, |
| 117 | "count": integer1_schema, |
| 118 | "start-address": ip_schema |
| 119 | }, |
| 120 | "additionalProperties": False, |
| 121 | } |
| 122 | |
| 123 | ip_profile_schema = { |
| 124 | "title": "ip profile validation schame", |
| 125 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 126 | "type": "object", |
| 127 | "properties": { |
| 128 | "ip-version": {"enum": ["ipv4", "ipv6"]}, |
| 129 | "subnet-address": ip_prefix_schema, |
| 130 | "gateway-address": ip_schema, |
| 131 | "dns-server": ip_profile_dns_schema, |
| 132 | "dhcp-params": ip_profile_dhcp_schema, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | ip_profile_update_schema = { |
| 137 | "title": "ip profile validation schame", |
| 138 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 139 | "type": "object", |
| 140 | "properties": { |
| 141 | "ip-version": {"enum": ["ipv4", "ipv6"]}, |
| 142 | "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]}, |
| 143 | "gateway-address": {"oneOf": [null_schema, ip_schema]}, |
| 144 | "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]}, |
| 145 | |
| 146 | "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]}, |
| 147 | }, |
| 148 | "additionalProperties": False |
| 149 | } |
| 150 | |
| 151 | ns_instantiate_internal_vld = { |
| 152 | "title": "ns action instantiate input schema for vdu", |
| 153 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 154 | "type": "object", |
| 155 | "properties": { |
| 156 | "name": name_schema, |
| 157 | "vim-network-name": name_schema, |
| 158 | "ip-profile": ip_profile_update_schema, |
| 159 | "internal-connection-point": { |
| 160 | "type": "array", |
| 161 | "minItems": 1, |
| 162 | "items": { |
| 163 | "type": "object", |
| 164 | "properties": { |
| 165 | "id-ref": name_schema, |
| 166 | "ip-address": ip_schema, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame] | 167 | # "mac-address": mac_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 168 | }, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame] | 169 | "required": ["id-ref"], |
| 170 | "minProperties": 2, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 171 | "additionalProperties": False |
| 172 | }, |
| 173 | } |
| 174 | }, |
| 175 | "required": ["name"], |
| 176 | "minProperties": 2, |
| 177 | "additionalProperties": False |
| 178 | } |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 179 | |
| 180 | ns_instantiate = { |
| 181 | "title": "ns action instantiate input schema", |
| 182 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 183 | "type": "object", |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 184 | "properties": { |
| 185 | "nsName": name_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 186 | "nsDescription": {"oneOf": [description_schema, {"type": "null"}]}, |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 187 | "nsdId": id_schema, |
| 188 | "vimAccountId": id_schema, |
| 189 | "ssh_keys": {"type": "string"}, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 190 | "nsr_id": id_schema, |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 191 | "vnf": { |
| 192 | "type": "array", |
| 193 | "minItems": 1, |
| 194 | "items": { |
| 195 | "type": "object", |
| 196 | "properties": { |
| 197 | "member-vnf-index": name_schema, |
| 198 | "vimAccountId": id_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 199 | "vdu": { |
| 200 | "type": "array", |
| 201 | "minItems": 1, |
| 202 | "items": ns_instantiate_vdu, |
| 203 | }, |
| 204 | "internal-vld": { |
| 205 | "type": "array", |
| 206 | "minItems": 1, |
| 207 | "items": ns_instantiate_internal_vld |
| 208 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 209 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 210 | "required": ["member-vnf-index"], |
| 211 | "minProperties": 2, |
| 212 | "additionalProperties": False |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 213 | } |
| 214 | }, |
| 215 | "vld": { |
| 216 | "type": "array", |
| 217 | "minItems": 1, |
| 218 | "items": { |
| 219 | "type": "object", |
| 220 | "properties": { |
| 221 | "name": string_schema, |
| 222 | "vim-network-name": {"OneOf": [string_schema, object_schema]}, |
| 223 | "ip-profile": object_schema, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame] | 224 | "vnfd-connection-point-ref": { |
| 225 | "type": "array", |
| 226 | "minItems": 1, |
| 227 | "items": { |
| 228 | "type": "object", |
| 229 | "properties": { |
| 230 | "member-vnf-index-ref": name_schema, |
| 231 | "vnfd-connection-point-ref": name_schema, |
| 232 | "ip-address": ip_schema, |
| 233 | # "mac-address": mac_schema, |
| 234 | }, |
| 235 | "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"], |
| 236 | "minProperties": 3, |
| 237 | "additionalProperties": False |
| 238 | }, |
| 239 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 240 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 241 | "required": ["name"], |
| 242 | "additionalProperties": False |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 243 | } |
| 244 | }, |
| 245 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 246 | "required": ["nsName", "nsdId", "vimAccountId"], |
| 247 | "additionalProperties": False |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 248 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 249 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 250 | ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 251 | "title": "ns action input schema", |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 252 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 253 | "type": "object", |
| 254 | "properties": { |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 255 | "member_vnf_index": name_schema, |
| 256 | "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future |
| tierno | 7ce1db9 | 2018-07-25 12:50:52 +0200 | [diff] [blame] | 257 | "vdu_id": name_schema, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 258 | "primitive": name_schema, |
| 259 | "primitive_params": {"type": "object"}, |
| 260 | }, |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 261 | "required": ["primitive", "primitive_params"], # TODO add member_vnf_index |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 262 | "additionalProperties": False |
| 263 | } |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 264 | ns_scale = { # TODO for the moment it is only VDU-scaling |
| 265 | "title": "ns scale input schema", |
| 266 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 267 | "type": "object", |
| 268 | "properties": { |
| 269 | "scaleType": {"enum": ["SCALE_VNF"]}, |
| 270 | "scaleVnfData": { |
| 271 | "type": "object", |
| 272 | "properties": { |
| 273 | "vnfInstanceId": name_schema, |
| 274 | "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']}, |
| 275 | "scaleByStepData": { |
| 276 | "type": "object", |
| 277 | "properties": { |
| 278 | "scaling-group-descriptor": name_schema, |
| 279 | "member-vnf-index": name_schema, |
| 280 | "scaling-policy": name_schema, |
| 281 | }, |
| 282 | "required": ["scaling-group-descriptor", "member-vnf-index"], |
| 283 | "additionalProperties": False |
| 284 | }, |
| 285 | }, |
| 286 | "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId |
| 287 | "additionalProperties": False |
| 288 | }, |
| 289 | "scaleTime": time_schema, |
| 290 | }, |
| 291 | "required": ["scaleType", "scaleVnfData"], |
| 292 | "additionalProperties": False |
| 293 | } |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 294 | |
| 295 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 296 | schema_version = {"type": "string", "enum": ["1.0"]} |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 297 | vim_account_edit_schema = { |
| 298 | "title": "vim_account edit input schema", |
| 299 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 300 | "type": "object", |
| 301 | "properties": { |
| 302 | "name": name_schema, |
| 303 | "description": description_schema, |
| 304 | "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins |
| 305 | "vim": name_schema, |
| 306 | "datacenter": name_schema, |
| 307 | "vim_url": description_schema, |
| 308 | "vim_url_admin": description_schema, |
| 309 | "vim_tenant": name_schema, |
| 310 | "vim_tenant_name": name_schema, |
| 311 | "vim_username": nameshort_schema, |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 312 | "vim_password": passwd_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 313 | "config": {"type": "object"} |
| 314 | }, |
| 315 | "additionalProperties": False |
| 316 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 317 | schema_type = {"type": "string"} |
| 318 | |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 319 | vim_account_new_schema = { |
| 320 | "title": "vim_account creation input schema", |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 321 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 322 | "type": "object", |
| 323 | "properties": { |
| 324 | "schema_version": schema_version, |
| 325 | "schema_type": schema_type, |
| 326 | "name": name_schema, |
| 327 | "description": description_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 328 | "vim": name_schema, |
| 329 | "datacenter": name_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 330 | "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]}, |
| 331 | "vim_url": description_schema, |
| 332 | # "vim_url_admin": description_schema, |
| 333 | # "vim_tenant": name_schema, |
| 334 | "vim_tenant_name": name_schema, |
| 335 | "vim_user": nameshort_schema, |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 336 | "vim_password": passwd_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 337 | "config": {"type": "object"} |
| 338 | }, |
| 339 | "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"], |
| 340 | "additionalProperties": False |
| 341 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 342 | |
| 343 | |
| 344 | sdn_properties = { |
| 345 | "name": name_schema, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 346 | "description": description_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 347 | "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, |
| 348 | "ip": ip_schema, |
| 349 | "port": port_schema, |
| 350 | "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]}, |
| 351 | "version": {"type": "string", "minLength": 1, "maxLength": 12}, |
| 352 | "user": nameshort_schema, |
| 353 | "password": passwd_schema |
| 354 | } |
| 355 | sdn_new_schema = { |
| 356 | "title": "sdn controller information schema", |
| 357 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 358 | "type": "object", |
| 359 | "properties": sdn_properties, |
| 360 | "required": ["name", "port", 'ip', 'dpid', 'type'], |
| 361 | "additionalProperties": False |
| 362 | } |
| 363 | sdn_edit_schema = { |
| 364 | "title": "sdn controller update information schema", |
| 365 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 366 | "type": "object", |
| 367 | "properties": sdn_properties, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 368 | # "required": ["name", "port", 'ip', 'dpid', 'type'], |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 369 | "additionalProperties": False |
| 370 | } |
| 371 | sdn_port_mapping_schema = { |
| 372 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 373 | "title": "sdn port mapping information schema", |
| 374 | "type": "array", |
| 375 | "items": { |
| 376 | "type": "object", |
| 377 | "properties": { |
| 378 | "compute_node": nameshort_schema, |
| 379 | "ports": { |
| 380 | "type": "array", |
| 381 | "items": { |
| 382 | "type": "object", |
| 383 | "properties": { |
| 384 | "pci": pci_schema, |
| 385 | "switch_port": nameshort_schema, |
| 386 | "switch_mac": mac_schema |
| 387 | }, |
| 388 | "required": ["pci"] |
| 389 | } |
| 390 | } |
| 391 | }, |
| 392 | "required": ["compute_node", "ports"] |
| 393 | } |
| 394 | } |
| 395 | sdn_external_port_schema = { |
| 396 | "$schema": "http://json-schema.org/draft-04/schema#", |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 397 | "title": "External port information", |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 398 | "type": "object", |
| 399 | "properties": { |
| 400 | "port": {"type": "string", "minLength": 1, "maxLength": 60}, |
| 401 | "vlan": vlan_schema, |
| 402 | "mac": mac_schema |
| 403 | }, |
| 404 | "required": ["port"] |
| 405 | } |
| 406 | |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 407 | # USERS |
| 408 | user_project_schema = { |
| 409 | "type": "array", |
| 410 | "minItems": 1, |
| 411 | "items": nameshort_schema, |
| 412 | } |
| 413 | user_new_schema = { |
| 414 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 415 | "title": "New user schema", |
| 416 | "type": "object", |
| 417 | "properties": { |
| 418 | "username": nameshort_schema, |
| 419 | "password": passwd_schema, |
| 420 | "projects": user_project_schema, |
| 421 | }, |
| 422 | "required": ["username", "password", "projects"], |
| 423 | "additionalProperties": False |
| 424 | } |
| 425 | user_edit_schema = { |
| 426 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 427 | "title": "User edit schema for administrators", |
| 428 | "type": "object", |
| 429 | "properties": { |
| 430 | "password": passwd_schema, |
| 431 | "projects": { |
| 432 | "oneOff": [ |
| 433 | user_project_schema, |
| 434 | array_edition_schema |
| 435 | ] |
| 436 | }, |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | # PROJECTS |
| 441 | project_new_schema = { |
| 442 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 443 | "title": "New project schema for administrators", |
| 444 | "type": "object", |
| 445 | "properties": { |
| 446 | "name": nameshort_schema, |
| 447 | "admin": bool_schema, |
| 448 | }, |
| 449 | "required": ["name"], |
| 450 | "additionalProperties": False |
| 451 | } |
| 452 | project_edit_schema = { |
| 453 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 454 | "title": "Project edit schema for administrators", |
| 455 | "type": "object", |
| 456 | "properties": { |
| 457 | "admin": bool_schema, |
| 458 | }, |
| 459 | "additionalProperties": False, |
| 460 | "minProperties": 1 |
| 461 | } |
| 462 | |
| 463 | # GLOBAL SCHEMAS |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 464 | |
| 465 | nbi_new_input_schemas = { |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 466 | "users": user_new_schema, |
| 467 | "projects": project_new_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 468 | "vim_accounts": vim_account_new_schema, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 469 | "sdns": sdn_new_schema, |
| 470 | "ns_instantiate": ns_instantiate, |
| 471 | "ns_action": ns_action, |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 472 | "ns_scale": ns_scale |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | nbi_edit_input_schemas = { |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 476 | "users": user_edit_schema, |
| 477 | "projects": project_edit_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 478 | "vim_accounts": vim_account_edit_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 479 | "sdns": sdn_edit_schema |
| 480 | } |
| 481 | |
| 482 | |
| 483 | class ValidationError(Exception): |
| 484 | pass |
| 485 | |
| 486 | |
| 487 | def validate_input(indata, item, new=True): |
| 488 | """ |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 489 | Validates input data against json schema |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 490 | :param indata: user input data. Should be a dictionary |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 491 | :param item: can be users, projects, vims, sdns, ns_xxxxx |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 492 | :param new: True if the validation is for creating or False if it is for editing |
| 493 | :return: None if ok, raises ValidationError exception otherwise |
| 494 | """ |
| 495 | try: |
| 496 | if new: |
| 497 | schema_to_use = nbi_new_input_schemas.get(item) |
| 498 | else: |
| 499 | schema_to_use = nbi_edit_input_schemas.get(item) |
| 500 | if schema_to_use: |
| 501 | js_v(indata, schema_to_use) |
| 502 | return None |
| 503 | except js_e.ValidationError as e: |
| 504 | if e.path: |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 505 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 506 | else: |
| 507 | error_pos = "" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 508 | raise ValidationError("Format error {} '{}' ".format(error_pos, e.message)) |