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