| 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 = "^[ -~]+$" |
| 15 | passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60} |
| 16 | nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()'\"]+$"} |
| 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} |
| 51 | |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 52 | ns_instantiate_vdu = { |
| 53 | "title": "ns action instantiate input schema for vdu", |
| 54 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 55 | "type": "object", |
| 56 | "properties": { |
| 57 | "id": name_schema, |
| 58 | "volume": { |
| 59 | "type": "array", |
| 60 | "minItems": 1, |
| 61 | "items": { |
| 62 | "type": "object", |
| 63 | "properties": { |
| 64 | "name": name_schema, |
| 65 | "vim-volume-id": name_schema, |
| 66 | }, |
| 67 | "required": ["name", "vim-volume-id"], |
| 68 | "additionalProperties": False |
| 69 | } |
| 70 | }, |
| 71 | "interface": { |
| 72 | "type": "array", |
| 73 | "minItems": 1, |
| 74 | "items": { |
| 75 | "type": "object", |
| 76 | "properties": { |
| 77 | "name": name_schema, |
| 78 | "ip-address": ip_schema, |
| 79 | "mac-address": mac_schema, |
| 80 | "floating-ip-required": bool_schema, |
| 81 | }, |
| 82 | "required": ["name"], |
| 83 | "additionalProperties": False |
| 84 | } |
| 85 | } |
| 86 | }, |
| 87 | "required": ["id"], |
| 88 | "additionalProperties": False |
| 89 | } |
| 90 | |
| 91 | ip_profile_dns_schema = { |
| 92 | "type": "array", |
| 93 | "minItems": 1, |
| 94 | "items": { |
| 95 | "type": "object", |
| 96 | "properties": { |
| 97 | "address": ip_schema, |
| 98 | }, |
| 99 | "required": ["address"], |
| 100 | "additionalProperties": False |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | ip_profile_dhcp_schema = { |
| 105 | "type": "object", |
| 106 | "properties": { |
| 107 | "enabled": {"type": "boolean"}, |
| 108 | "count": integer1_schema, |
| 109 | "start-address": ip_schema |
| 110 | }, |
| 111 | "additionalProperties": False, |
| 112 | } |
| 113 | |
| 114 | ip_profile_schema = { |
| 115 | "title": "ip profile validation schame", |
| 116 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 117 | "type": "object", |
| 118 | "properties": { |
| 119 | "ip-version": {"enum": ["ipv4", "ipv6"]}, |
| 120 | "subnet-address": ip_prefix_schema, |
| 121 | "gateway-address": ip_schema, |
| 122 | "dns-server": ip_profile_dns_schema, |
| 123 | "dhcp-params": ip_profile_dhcp_schema, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ip_profile_update_schema = { |
| 128 | "title": "ip profile validation schame", |
| 129 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 130 | "type": "object", |
| 131 | "properties": { |
| 132 | "ip-version": {"enum": ["ipv4", "ipv6"]}, |
| 133 | "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]}, |
| 134 | "gateway-address": {"oneOf": [null_schema, ip_schema]}, |
| 135 | "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]}, |
| 136 | |
| 137 | "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]}, |
| 138 | }, |
| 139 | "additionalProperties": False |
| 140 | } |
| 141 | |
| 142 | ns_instantiate_internal_vld = { |
| 143 | "title": "ns action instantiate input schema for vdu", |
| 144 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 145 | "type": "object", |
| 146 | "properties": { |
| 147 | "name": name_schema, |
| 148 | "vim-network-name": name_schema, |
| 149 | "ip-profile": ip_profile_update_schema, |
| 150 | "internal-connection-point": { |
| 151 | "type": "array", |
| 152 | "minItems": 1, |
| 153 | "items": { |
| 154 | "type": "object", |
| 155 | "properties": { |
| 156 | "id-ref": name_schema, |
| 157 | "ip-address": ip_schema, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame^] | 158 | # "mac-address": mac_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 159 | }, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame^] | 160 | "required": ["id-ref"], |
| 161 | "minProperties": 2, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 162 | "additionalProperties": False |
| 163 | }, |
| 164 | } |
| 165 | }, |
| 166 | "required": ["name"], |
| 167 | "minProperties": 2, |
| 168 | "additionalProperties": False |
| 169 | } |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 170 | |
| 171 | ns_instantiate = { |
| 172 | "title": "ns action instantiate input schema", |
| 173 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 174 | "type": "object", |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 175 | "properties": { |
| 176 | "nsName": name_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 177 | "nsDescription": {"oneOf": [description_schema, {"type": "null"}]}, |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 178 | "nsdId": id_schema, |
| 179 | "vimAccountId": id_schema, |
| 180 | "ssh_keys": {"type": "string"}, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 181 | "nsr_id": id_schema, |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 182 | "vnf": { |
| 183 | "type": "array", |
| 184 | "minItems": 1, |
| 185 | "items": { |
| 186 | "type": "object", |
| 187 | "properties": { |
| 188 | "member-vnf-index": name_schema, |
| 189 | "vimAccountId": id_schema, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 190 | "vdu": { |
| 191 | "type": "array", |
| 192 | "minItems": 1, |
| 193 | "items": ns_instantiate_vdu, |
| 194 | }, |
| 195 | "internal-vld": { |
| 196 | "type": "array", |
| 197 | "minItems": 1, |
| 198 | "items": ns_instantiate_internal_vld |
| 199 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 200 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 201 | "required": ["member-vnf-index"], |
| 202 | "minProperties": 2, |
| 203 | "additionalProperties": False |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 204 | } |
| 205 | }, |
| 206 | "vld": { |
| 207 | "type": "array", |
| 208 | "minItems": 1, |
| 209 | "items": { |
| 210 | "type": "object", |
| 211 | "properties": { |
| 212 | "name": string_schema, |
| 213 | "vim-network-name": {"OneOf": [string_schema, object_schema]}, |
| 214 | "ip-profile": object_schema, |
| tierno | b7c6f2a | 2018-09-03 14:32:10 +0200 | [diff] [blame^] | 215 | "vnfd-connection-point-ref": { |
| 216 | "type": "array", |
| 217 | "minItems": 1, |
| 218 | "items": { |
| 219 | "type": "object", |
| 220 | "properties": { |
| 221 | "member-vnf-index-ref": name_schema, |
| 222 | "vnfd-connection-point-ref": name_schema, |
| 223 | "ip-address": ip_schema, |
| 224 | # "mac-address": mac_schema, |
| 225 | }, |
| 226 | "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"], |
| 227 | "minProperties": 3, |
| 228 | "additionalProperties": False |
| 229 | }, |
| 230 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 231 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 232 | "required": ["name"], |
| 233 | "additionalProperties": False |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 234 | } |
| 235 | }, |
| 236 | }, |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 237 | "required": ["nsName", "nsdId", "vimAccountId"], |
| 238 | "additionalProperties": False |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 239 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 240 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 241 | 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] | 242 | "title": "ns action input schema", |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 243 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 244 | "type": "object", |
| 245 | "properties": { |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 246 | "member_vnf_index": name_schema, |
| 247 | "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future |
| tierno | 7ce1db9 | 2018-07-25 12:50:52 +0200 | [diff] [blame] | 248 | "vdu_id": name_schema, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 249 | "primitive": name_schema, |
| 250 | "primitive_params": {"type": "object"}, |
| 251 | }, |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 252 | "required": ["primitive", "primitive_params"], # TODO add member_vnf_index |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 253 | "additionalProperties": False |
| 254 | } |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 255 | ns_scale = { # TODO for the moment it is only VDU-scaling |
| 256 | "title": "ns scale input schema", |
| 257 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 258 | "type": "object", |
| 259 | "properties": { |
| 260 | "scaleType": {"enum": ["SCALE_VNF"]}, |
| 261 | "scaleVnfData": { |
| 262 | "type": "object", |
| 263 | "properties": { |
| 264 | "vnfInstanceId": name_schema, |
| 265 | "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']}, |
| 266 | "scaleByStepData": { |
| 267 | "type": "object", |
| 268 | "properties": { |
| 269 | "scaling-group-descriptor": name_schema, |
| 270 | "member-vnf-index": name_schema, |
| 271 | "scaling-policy": name_schema, |
| 272 | }, |
| 273 | "required": ["scaling-group-descriptor", "member-vnf-index"], |
| 274 | "additionalProperties": False |
| 275 | }, |
| 276 | }, |
| 277 | "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId |
| 278 | "additionalProperties": False |
| 279 | }, |
| 280 | "scaleTime": time_schema, |
| 281 | }, |
| 282 | "required": ["scaleType", "scaleVnfData"], |
| 283 | "additionalProperties": False |
| 284 | } |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 285 | |
| 286 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 287 | schema_version = {"type": "string", "enum": ["1.0"]} |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 288 | vim_account_edit_schema = { |
| 289 | "title": "vim_account edit input schema", |
| 290 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 291 | "type": "object", |
| 292 | "properties": { |
| 293 | "name": name_schema, |
| 294 | "description": description_schema, |
| 295 | "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins |
| 296 | "vim": name_schema, |
| 297 | "datacenter": name_schema, |
| 298 | "vim_url": description_schema, |
| 299 | "vim_url_admin": description_schema, |
| 300 | "vim_tenant": name_schema, |
| 301 | "vim_tenant_name": name_schema, |
| 302 | "vim_username": nameshort_schema, |
| 303 | "vim_password": nameshort_schema, |
| 304 | "config": {"type": "object"} |
| 305 | }, |
| 306 | "additionalProperties": False |
| 307 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 308 | schema_type = {"type": "string"} |
| 309 | |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 310 | vim_account_new_schema = { |
| 311 | "title": "vim_account creation input schema", |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 312 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 313 | "type": "object", |
| 314 | "properties": { |
| 315 | "schema_version": schema_version, |
| 316 | "schema_type": schema_type, |
| 317 | "name": name_schema, |
| 318 | "description": description_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 319 | "vim": name_schema, |
| 320 | "datacenter": name_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 321 | "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]}, |
| 322 | "vim_url": description_schema, |
| 323 | # "vim_url_admin": description_schema, |
| 324 | # "vim_tenant": name_schema, |
| 325 | "vim_tenant_name": name_schema, |
| 326 | "vim_user": nameshort_schema, |
| 327 | "vim_password": nameshort_schema, |
| 328 | "config": {"type": "object"} |
| 329 | }, |
| 330 | "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"], |
| 331 | "additionalProperties": False |
| 332 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 333 | |
| 334 | |
| 335 | sdn_properties = { |
| 336 | "name": name_schema, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 337 | "description": description_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 338 | "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, |
| 339 | "ip": ip_schema, |
| 340 | "port": port_schema, |
| 341 | "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]}, |
| 342 | "version": {"type": "string", "minLength": 1, "maxLength": 12}, |
| 343 | "user": nameshort_schema, |
| 344 | "password": passwd_schema |
| 345 | } |
| 346 | sdn_new_schema = { |
| 347 | "title": "sdn controller information schema", |
| 348 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 349 | "type": "object", |
| 350 | "properties": sdn_properties, |
| 351 | "required": ["name", "port", 'ip', 'dpid', 'type'], |
| 352 | "additionalProperties": False |
| 353 | } |
| 354 | sdn_edit_schema = { |
| 355 | "title": "sdn controller update information schema", |
| 356 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 357 | "type": "object", |
| 358 | "properties": sdn_properties, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 359 | # "required": ["name", "port", 'ip', 'dpid', 'type'], |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 360 | "additionalProperties": False |
| 361 | } |
| 362 | sdn_port_mapping_schema = { |
| 363 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 364 | "title": "sdn port mapping information schema", |
| 365 | "type": "array", |
| 366 | "items": { |
| 367 | "type": "object", |
| 368 | "properties": { |
| 369 | "compute_node": nameshort_schema, |
| 370 | "ports": { |
| 371 | "type": "array", |
| 372 | "items": { |
| 373 | "type": "object", |
| 374 | "properties": { |
| 375 | "pci": pci_schema, |
| 376 | "switch_port": nameshort_schema, |
| 377 | "switch_mac": mac_schema |
| 378 | }, |
| 379 | "required": ["pci"] |
| 380 | } |
| 381 | } |
| 382 | }, |
| 383 | "required": ["compute_node", "ports"] |
| 384 | } |
| 385 | } |
| 386 | sdn_external_port_schema = { |
| 387 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 388 | "title": "External port ingformation", |
| 389 | "type": "object", |
| 390 | "properties": { |
| 391 | "port": {"type": "string", "minLength": 1, "maxLength": 60}, |
| 392 | "vlan": vlan_schema, |
| 393 | "mac": mac_schema |
| 394 | }, |
| 395 | "required": ["port"] |
| 396 | } |
| 397 | |
| 398 | |
| 399 | nbi_new_input_schemas = { |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 400 | "vim_accounts": vim_account_new_schema, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 401 | "sdns": sdn_new_schema, |
| 402 | "ns_instantiate": ns_instantiate, |
| 403 | "ns_action": ns_action, |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 404 | "ns_scale": ns_scale |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | nbi_edit_input_schemas = { |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 408 | "vim_accounts": vim_account_edit_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 409 | "sdns": sdn_edit_schema |
| 410 | } |
| 411 | |
| 412 | |
| 413 | class ValidationError(Exception): |
| 414 | pass |
| 415 | |
| 416 | |
| 417 | def validate_input(indata, item, new=True): |
| 418 | """ |
| 419 | Validates input data agains json schema |
| 420 | :param indata: user input data. Should be a dictionary |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 421 | :param item: can be users, projects, vims, sdns, ns_xxxxx |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 422 | :param new: True if the validation is for creating or False if it is for editing |
| 423 | :return: None if ok, raises ValidationError exception otherwise |
| 424 | """ |
| 425 | try: |
| 426 | if new: |
| 427 | schema_to_use = nbi_new_input_schemas.get(item) |
| 428 | else: |
| 429 | schema_to_use = nbi_edit_input_schemas.get(item) |
| 430 | if schema_to_use: |
| 431 | js_v(indata, schema_to_use) |
| 432 | return None |
| 433 | except js_e.ValidationError as e: |
| 434 | if e.path: |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 435 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 436 | else: |
| 437 | error_pos = "" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 438 | raise ValidationError("Format error {} '{}' ".format(error_pos, e.message)) |