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