| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | from jsonschema import validate as js_v, exceptions as js_e |
| 4 | |
| 5 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 6 | __version__ = "0.1" |
| 7 | version_date = "Mar 2018" |
| 8 | |
| 9 | """ |
| 10 | Validator of input data using JSON schemas for those items that not contains an OSM yang information model |
| 11 | """ |
| 12 | |
| 13 | # Basis schemas |
| 14 | patern_name = "^[ -~]+$" |
| 15 | passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60} |
| 16 | nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()'\"]+$"} |
| 17 | name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"} |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 18 | string_schema = {"type": "string", "minLength": 1, "maxLength": 255} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 19 | xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"} |
| 20 | description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"} |
| 21 | id_schema_fake = {"type": "string", "minLength": 2, |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 22 | "maxLength": 36} |
| 23 | # "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] | 24 | 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] | 25 | time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"} |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 26 | pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"} |
| 27 | http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"} |
| 28 | bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"} |
| 29 | memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"} |
| 30 | integer0_schema = {"type": "integer", "minimum": 0} |
| 31 | integer1_schema = {"type": "integer", "minimum": 1} |
| 32 | path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"} |
| 33 | vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095} |
| 34 | vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095} |
| 35 | mac_schema = {"type": "string", |
| 36 | "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0 |
| 37 | # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"} |
| 38 | ip_schema = {"type": "string", |
| 39 | "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]?)$"} |
| 40 | ip_prefix_schema = {"type": "string", |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 41 | "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}" |
| 42 | "(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] | 43 | port_schema = {"type": "integer", "minimum": 1, "maximum": 65534} |
| 44 | object_schema = {"type": "object"} |
| 45 | schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2} |
| 46 | # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]} |
| 47 | log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]} |
| 48 | checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"} |
| 49 | size_schema = {"type": "integer", "minimum": 1, "maximum": 100} |
| 50 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 51 | |
| 52 | ns_instantiate = { |
| 53 | "title": "ns action instantiate input schema", |
| 54 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 55 | "type": "object", |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 56 | "properties": { |
| 57 | "nsName": name_schema, |
| 58 | "nsDescription": description_schema, |
| 59 | "nsdId": id_schema, |
| 60 | "vimAccountId": id_schema, |
| 61 | "ssh_keys": {"type": "string"}, |
| 62 | "vnf": { |
| 63 | "type": "array", |
| 64 | "minItems": 1, |
| 65 | "items": { |
| 66 | "type": "object", |
| 67 | "properties": { |
| 68 | "member-vnf-index": name_schema, |
| 69 | "vimAccountId": id_schema, |
| 70 | }, |
| 71 | "required": ["member-vnf-index"] |
| 72 | } |
| 73 | }, |
| 74 | "vld": { |
| 75 | "type": "array", |
| 76 | "minItems": 1, |
| 77 | "items": { |
| 78 | "type": "object", |
| 79 | "properties": { |
| 80 | "name": string_schema, |
| 81 | "vim-network-name": {"OneOf": [string_schema, object_schema]}, |
| 82 | "ip-profile": object_schema, |
| 83 | }, |
| 84 | "required": ["name"] |
| 85 | } |
| 86 | }, |
| 87 | }, |
| 88 | "required": ["nsName", "nsdId", "vimAccountId"] |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 89 | } |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 90 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 91 | 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] | 92 | "title": "ns action input schema", |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 93 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 94 | "type": "object", |
| 95 | "properties": { |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 96 | "member_vnf_index": name_schema, |
| 97 | "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 98 | "primitive": name_schema, |
| 99 | "primitive_params": {"type": "object"}, |
| 100 | }, |
| tierno | f5298be | 2018-05-16 14:43:57 +0200 | [diff] [blame] | 101 | "required": ["primitive", "primitive_params"], # TODO add member_vnf_index |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 102 | "additionalProperties": False |
| 103 | } |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 104 | ns_scale = { # TODO for the moment it is only VDU-scaling |
| 105 | "title": "ns scale input schema", |
| 106 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 107 | "type": "object", |
| 108 | "properties": { |
| 109 | "scaleType": {"enum": ["SCALE_VNF"]}, |
| 110 | "scaleVnfData": { |
| 111 | "type": "object", |
| 112 | "properties": { |
| 113 | "vnfInstanceId": name_schema, |
| 114 | "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']}, |
| 115 | "scaleByStepData": { |
| 116 | "type": "object", |
| 117 | "properties": { |
| 118 | "scaling-group-descriptor": name_schema, |
| 119 | "member-vnf-index": name_schema, |
| 120 | "scaling-policy": name_schema, |
| 121 | }, |
| 122 | "required": ["scaling-group-descriptor", "member-vnf-index"], |
| 123 | "additionalProperties": False |
| 124 | }, |
| 125 | }, |
| 126 | "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId |
| 127 | "additionalProperties": False |
| 128 | }, |
| 129 | "scaleTime": time_schema, |
| 130 | }, |
| 131 | "required": ["scaleType", "scaleVnfData"], |
| 132 | "additionalProperties": False |
| 133 | } |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 134 | |
| 135 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 136 | schema_version = {"type": "string", "enum": ["1.0"]} |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 137 | vim_account_edit_schema = { |
| 138 | "title": "vim_account edit input schema", |
| 139 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 140 | "type": "object", |
| 141 | "properties": { |
| 142 | "name": name_schema, |
| 143 | "description": description_schema, |
| 144 | "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins |
| 145 | "vim": name_schema, |
| 146 | "datacenter": name_schema, |
| 147 | "vim_url": description_schema, |
| 148 | "vim_url_admin": description_schema, |
| 149 | "vim_tenant": name_schema, |
| 150 | "vim_tenant_name": name_schema, |
| 151 | "vim_username": nameshort_schema, |
| 152 | "vim_password": nameshort_schema, |
| 153 | "config": {"type": "object"} |
| 154 | }, |
| 155 | "additionalProperties": False |
| 156 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 157 | schema_type = {"type": "string"} |
| 158 | |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 159 | vim_account_new_schema = { |
| 160 | "title": "vim_account creation input schema", |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 161 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 162 | "type": "object", |
| 163 | "properties": { |
| 164 | "schema_version": schema_version, |
| 165 | "schema_type": schema_type, |
| 166 | "name": name_schema, |
| 167 | "description": description_schema, |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 168 | "vim": name_schema, |
| 169 | "datacenter": name_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 170 | "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]}, |
| 171 | "vim_url": description_schema, |
| 172 | # "vim_url_admin": description_schema, |
| 173 | # "vim_tenant": name_schema, |
| 174 | "vim_tenant_name": name_schema, |
| 175 | "vim_user": nameshort_schema, |
| 176 | "vim_password": nameshort_schema, |
| 177 | "config": {"type": "object"} |
| 178 | }, |
| 179 | "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"], |
| 180 | "additionalProperties": False |
| 181 | } |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 182 | |
| 183 | |
| 184 | sdn_properties = { |
| 185 | "name": name_schema, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 186 | "description": description_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 187 | "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, |
| 188 | "ip": ip_schema, |
| 189 | "port": port_schema, |
| 190 | "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]}, |
| 191 | "version": {"type": "string", "minLength": 1, "maxLength": 12}, |
| 192 | "user": nameshort_schema, |
| 193 | "password": passwd_schema |
| 194 | } |
| 195 | sdn_new_schema = { |
| 196 | "title": "sdn controller information schema", |
| 197 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 198 | "type": "object", |
| 199 | "properties": sdn_properties, |
| 200 | "required": ["name", "port", 'ip', 'dpid', 'type'], |
| 201 | "additionalProperties": False |
| 202 | } |
| 203 | sdn_edit_schema = { |
| 204 | "title": "sdn controller update information schema", |
| 205 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 206 | "type": "object", |
| 207 | "properties": sdn_properties, |
| tierno | cfb07c6 | 2018-05-10 18:30:51 +0200 | [diff] [blame] | 208 | # "required": ["name", "port", 'ip', 'dpid', 'type'], |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 209 | "additionalProperties": False |
| 210 | } |
| 211 | sdn_port_mapping_schema = { |
| 212 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 213 | "title": "sdn port mapping information schema", |
| 214 | "type": "array", |
| 215 | "items": { |
| 216 | "type": "object", |
| 217 | "properties": { |
| 218 | "compute_node": nameshort_schema, |
| 219 | "ports": { |
| 220 | "type": "array", |
| 221 | "items": { |
| 222 | "type": "object", |
| 223 | "properties": { |
| 224 | "pci": pci_schema, |
| 225 | "switch_port": nameshort_schema, |
| 226 | "switch_mac": mac_schema |
| 227 | }, |
| 228 | "required": ["pci"] |
| 229 | } |
| 230 | } |
| 231 | }, |
| 232 | "required": ["compute_node", "ports"] |
| 233 | } |
| 234 | } |
| 235 | sdn_external_port_schema = { |
| 236 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 237 | "title": "External port ingformation", |
| 238 | "type": "object", |
| 239 | "properties": { |
| 240 | "port": {"type": "string", "minLength": 1, "maxLength": 60}, |
| 241 | "vlan": vlan_schema, |
| 242 | "mac": mac_schema |
| 243 | }, |
| 244 | "required": ["port"] |
| 245 | } |
| 246 | |
| 247 | |
| 248 | nbi_new_input_schemas = { |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 249 | "vim_accounts": vim_account_new_schema, |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 250 | "sdns": sdn_new_schema, |
| 251 | "ns_instantiate": ns_instantiate, |
| 252 | "ns_action": ns_action, |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 253 | "ns_scale": ns_scale |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | nbi_edit_input_schemas = { |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 257 | "vim_accounts": vim_account_edit_schema, |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 258 | "sdns": sdn_edit_schema |
| 259 | } |
| 260 | |
| 261 | |
| 262 | class ValidationError(Exception): |
| 263 | pass |
| 264 | |
| 265 | |
| 266 | def validate_input(indata, item, new=True): |
| 267 | """ |
| 268 | Validates input data agains json schema |
| 269 | :param indata: user input data. Should be a dictionary |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 270 | :param item: can be users, projects, vims, sdns, ns_xxxxx |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 271 | :param new: True if the validation is for creating or False if it is for editing |
| 272 | :return: None if ok, raises ValidationError exception otherwise |
| 273 | """ |
| 274 | try: |
| 275 | if new: |
| 276 | schema_to_use = nbi_new_input_schemas.get(item) |
| 277 | else: |
| 278 | schema_to_use = nbi_edit_input_schemas.get(item) |
| 279 | if schema_to_use: |
| 280 | js_v(indata, schema_to_use) |
| 281 | return None |
| 282 | except js_e.ValidationError as e: |
| 283 | if e.path: |
| tierno | 0da5225 | 2018-06-27 15:47:22 +0200 | [diff] [blame] | 284 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 285 | else: |
| 286 | error_pos = "" |
| 287 | raise ValidationError("Format error {} '{}' ".format(error_pos, e)) |