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