Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 17 | import pdb |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 18 | import re |
Scott Baker | 391f5d8 | 2018-10-02 16:34:41 -0700 | [diff] [blame] | 19 | from inflect import engine as inflect_engine_class |
| 20 | |
| 21 | inflect_engine = inflect_engine_class() |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 22 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 23 | class FieldNotFound(Exception): |
| 24 | def __init__(self, message): |
| 25 | super(FieldNotFound, self).__init__(message) |
| 26 | |
Sapan Bhatia | d456759 | 2017-07-24 17:26:26 -0400 | [diff] [blame] | 27 | def xproto_debug(**kwargs): |
| 28 | print kwargs |
| 29 | pdb.set_trace() |
| 30 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 31 | def xproto_unquote(s): |
| 32 | return unquote(s) |
| 33 | |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 34 | def unquote(s): |
| 35 | if (s.startswith('"') and s.endswith('"')): |
| 36 | return s[1:-1] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 37 | else: |
| 38 | return s |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 39 | |
| 40 | def xproto_singularize(field): |
| 41 | try: |
| 42 | # The user has set a singular, as an exception that cannot be handled automatically |
| 43 | singular = field['options']['singular'] |
| 44 | singular = unquote(singular) |
| 45 | except KeyError: |
Scott Baker | 391f5d8 | 2018-10-02 16:34:41 -0700 | [diff] [blame] | 46 | singular = inflect_engine.singular_noun(field['name']) |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 47 | |
| 48 | return singular |
| 49 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 50 | def xproto_singularize_pluralize(field): |
| 51 | try: |
| 52 | # The user has set a plural, as an exception that cannot be handled automatically |
| 53 | plural = field['options']['plural'] |
| 54 | plural = unquote(plural) |
| 55 | except KeyError: |
Scott Baker | 391f5d8 | 2018-10-02 16:34:41 -0700 | [diff] [blame] | 56 | plural = inflect_engine.plural_noun(inflect_engine.singular_noun(field['name'])) |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 57 | |
| 58 | return plural |
| 59 | |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 60 | def xproto_pluralize(field): |
| 61 | try: |
| 62 | # The user has set a plural, as an exception that cannot be handled automatically |
| 63 | plural = field['options']['plural'] |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 64 | plural = unquote(plural) |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 65 | except KeyError: |
Scott Baker | 391f5d8 | 2018-10-02 16:34:41 -0700 | [diff] [blame] | 66 | plural = inflect_engine.plural_noun(field['name']) |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 67 | |
| 68 | return plural |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 69 | |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 70 | def xproto_base_def(model_name, base, suffix='', suffix_list=[]): |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 71 | if (model_name=='XOSBase'): |
| 72 | return '(models.Model, PlModelMixIn)' |
| 73 | elif (not base): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 74 | return '' |
| 75 | else: |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 76 | int_base = [i['name']+suffix for i in base if i['name'] in suffix_list] |
| 77 | ext_base = [i['name'] for i in base if i['name'] not in suffix_list] |
| 78 | return '(' + ','.join(int_base + ext_base) + ')' |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 79 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 80 | def xproto_first_non_empty(lst): |
| 81 | for l in lst: |
| 82 | if l: return l |
| 83 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 84 | def xproto_api_type(field): |
| 85 | try: |
| 86 | if (unquote(field['options']['content_type'])=='date'): |
Scott Baker | 450e5d0 | 2017-09-06 11:18:16 -0700 | [diff] [blame] | 87 | return 'double' |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 88 | except KeyError: |
| 89 | pass |
| 90 | |
| 91 | return field['type'] |
| 92 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 93 | |
| 94 | def xproto_base_name(n): |
| 95 | # Hack - Refactor NetworkParameter* to make this go away |
| 96 | if (n.startswith('NetworkParameter')): |
| 97 | return '_' |
| 98 | |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 99 | expr = r'^[A-Z]+[a-z]*' |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 100 | |
| 101 | try: |
| 102 | match = re.findall(expr, n)[0] |
| 103 | except: |
| 104 | return '_' |
| 105 | |
| 106 | return match |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 107 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 108 | def xproto_base_fields(m, table): |
| 109 | fields = [] |
| 110 | |
| 111 | for b in m['bases']: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 112 | option1 = b['fqn'] |
| 113 | try: |
| 114 | option2 = m['package'] + '.' + b['name'] |
| 115 | except TypeError: |
| 116 | option2 = option1 |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 117 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 118 | accessor = None |
| 119 | if option1 in table: accessor = option1 |
| 120 | elif option2 in table: accessor = option2 |
| 121 | |
| 122 | if accessor: |
| 123 | base_fields = xproto_base_fields(table[accessor], table) |
| 124 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 125 | model_fields = [x.copy() for x in table[accessor]['fields']] |
| 126 | for field in model_fields: |
| 127 | field["accessor"] = accessor |
| 128 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 129 | fields.extend(base_fields) |
| 130 | fields.extend(model_fields) |
| 131 | |
Matteo Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 132 | if 'no_sync' in m['options'] and m['options']['no_sync']: |
| 133 | fields = [f for f in fields if f['name'] != 'backend_status' and f['name'] != 'backend_code'] |
| 134 | |
| 135 | if 'no_policy' in m['options'] and m['options']['no_policy']: |
| 136 | fields = [f for f in fields if f['name'] != 'policy_status' and f['name'] != 'policy_code'] |
| 137 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 138 | return fields |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 139 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 140 | def xproto_fields(m, table): |
| 141 | """ Generate the full list of models for the xproto message `m` including fields from the classes it inherits. |
| 142 | |
| 143 | Inserts the special field "id" at the very beginning. |
| 144 | |
| 145 | Each time we descend a new level of inheritance, increment the offset field numbers by 100. The base |
| 146 | class's fields will be numbered from 1-99, the first descendant will be number 100-199, the second |
| 147 | descdendant numbered from 200-299, and so on. This assumes any particular model as at most 100 |
| 148 | fields. |
| 149 | """ |
| 150 | |
| 151 | model_fields = [x.copy() for x in m["fields"]] |
| 152 | for field in model_fields: |
| 153 | field["accessor"] = m["fqn"] |
| 154 | |
| 155 | fields = xproto_base_fields(m, table) + model_fields |
| 156 | |
| 157 | # The "id" field is a special field. Every model has one. Put it up front and pretend it's part of the |
| 158 | |
| 159 | id_field = {'type': 'int32', 'name': 'id', 'options': {}, "id": "1", "accessor": fields[0]["accessor"]} |
| 160 | |
| 161 | fields = [id_field] + fields |
| 162 | |
| 163 | # Walk through the list of fields. They will be in depth-first search order from the base model forward. Each time |
| 164 | # the model changes, offset the protobuf field numbers by 100. |
| 165 | offset = 0 |
| 166 | last_accessor = fields[0]["accessor"] |
| 167 | for field in fields: |
| 168 | if (field["accessor"] != last_accessor): |
| 169 | last_accessor = field["accessor"] |
| 170 | offset += 100 |
| 171 | field_id = int(field["id"]) |
| 172 | if (field_id < 1) or (field_id >= 100): |
| 173 | raise Exception("Only field numbers from 1 to 99 are permitted, field %s in model %s" % (field["name"], field["accessor"])) |
| 174 | field["id"] = int(field["id"]) + offset |
| 175 | |
| 176 | # Check for duplicates |
| 177 | fields_by_number = {} |
| 178 | for field in fields: |
| 179 | id = field["id"] |
| 180 | dup = fields_by_number.get(id) |
| 181 | if dup: |
| 182 | raise Exception("Field %s has duplicate number %d with field %s in model %s" % (field["name"], id, dup["name"], field["accessor"])) |
| 183 | fields_by_number[id] = field |
| 184 | |
| 185 | return fields |
| 186 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 187 | def xproto_base_rlinks(m, table): |
| 188 | links = [] |
| 189 | |
| 190 | for base in m['bases']: |
| 191 | b = base['name'] |
| 192 | if b in table: |
| 193 | base_rlinks = xproto_base_rlinks(table[b], table) |
| 194 | |
| 195 | model_rlinks = table[b]['rlinks'] |
| 196 | links.extend(base_rlinks) |
| 197 | links.extend(model_rlinks) |
| 198 | |
| 199 | return links |
| 200 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 201 | def xproto_rlinks(m, table): |
| 202 | """ Return the reverse links for the xproto message `m`. |
| 203 | |
| 204 | If the link includes a reverse_id, then it will be used for the protobuf field id. If there is no |
| 205 | reverse_id, then one will automatically be allocated started at id 1900. It is incouraged that all links |
| 206 | include reverse_ids, so that field identifiers are deterministic across all protobuf messages. |
| 207 | """ |
| 208 | |
| 209 | index = 1900 |
| 210 | links = xproto_base_rlinks(m, table) + m["rlinks"] |
| 211 | |
| 212 | links = [x for x in links if ("+" not in x["src_port"]) and ("+" not in x["dst_port"])] |
| 213 | |
| 214 | for link in links: |
| 215 | if link["reverse_id"]: |
| 216 | link["id"] = int(link["reverse_id"]) |
| 217 | else: |
| 218 | link["id"] = index |
| 219 | index += 1 |
| 220 | |
| 221 | # check for duplicates |
| 222 | links_by_number={} |
| 223 | for link in links: |
| 224 | id = link["id"] |
| 225 | dup=links_by_number.get(id) |
| 226 | if dup: |
| 227 | raise Exception("Field %s has duplicate number %d with field %s in model %s" % (link["src_port"], id, link["src_port"], m["name"])) |
| 228 | links_by_number[id] = link |
| 229 | |
| 230 | return links |
| 231 | |
| 232 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 233 | def xproto_base_links(m, table): |
| 234 | links = [] |
| 235 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 236 | for base in m['bases']: |
| 237 | b = base['name'] |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 238 | if b in table: |
| 239 | base_links = xproto_base_links(table[b], table) |
| 240 | |
| 241 | model_links = table[b]['links'] |
| 242 | links.extend(base_links) |
| 243 | links.extend(model_links) |
| 244 | return links |
| 245 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 246 | def xproto_string_type(xptags): |
| 247 | try: |
| 248 | max_length = eval(xptags['max_length']) |
| 249 | except: |
| 250 | max_length = 1024 |
| 251 | |
| 252 | if ('varchar' not in xptags): |
| 253 | return 'string' |
| 254 | else: |
| 255 | return 'text' |
| 256 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 257 | def xproto_tuplify(nested_list_or_set): |
| 258 | if not isinstance(nested_list_or_set, list) and not isinstance(nested_list_or_set, set): |
| 259 | return nested_list_or_set |
| 260 | else: |
| 261 | return tuple([xproto_tuplify(i) for i in nested_list_or_set]) |
| 262 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 263 | def xproto_field_graph_components(fields, model, tag='unique_with'): |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 264 | def find_components(graph): |
| 265 | pending = set(graph.keys()) |
| 266 | components = [] |
| 267 | |
| 268 | while pending: |
| 269 | front = { pending.pop() } |
| 270 | component = set() |
| 271 | |
| 272 | while front: |
| 273 | node = front.pop() |
| 274 | neighbours = graph[node] |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 275 | neighbours -= component # These we have already visited |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 276 | front |= neighbours |
| 277 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 278 | pending -= neighbours |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 279 | component |= neighbours |
| 280 | |
| 281 | components.append(component) |
| 282 | |
| 283 | return components |
| 284 | |
| 285 | field_graph = {} |
| 286 | field_names = {f['name'] for f in fields} |
| 287 | |
| 288 | for f in fields: |
| 289 | try: |
| 290 | tagged_str = unquote(f['options'][tag]) |
| 291 | tagged_fields = tagged_str.split(',') |
| 292 | |
| 293 | for uf in tagged_fields: |
| 294 | if uf not in field_names: |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 295 | raise FieldNotFound('Field "%s" not found in model "%s", referenced from field "%s" by option "%s"' % (uf, model['name'], f['name'], tag)) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 296 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 297 | field_graph.setdefault(f['name'], set()).add(uf) |
| 298 | field_graph.setdefault(uf, set()).add(f['name']) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 299 | except KeyError: |
| 300 | pass |
| 301 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 302 | return find_components(field_graph) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 303 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 304 | def xproto_api_opts(field): |
| 305 | options = [] |
| 306 | if 'max_length' in field['options'] and field['type']=='string': |
| 307 | options.append('(val).maxLength = %s'%field['options']['max_length']) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 308 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 309 | try: |
| 310 | if field['options']['null'] == 'False': |
| 311 | options.append('(val).nonNull = true') |
| 312 | except KeyError: |
| 313 | pass |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 314 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 315 | if 'link' in field and 'model' in field['options']: |
| 316 | options.append('(foreignKey).modelName = "%s"'%field['options']['model']) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 317 | if ("options" in field) and ("port" in field["options"]): |
| 318 | options.append('(foreignKey).reverseFieldName = "%s"' % field['options']['port']) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 319 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 320 | if options: |
| 321 | options_str = '[' + ', '.join(options) + ']' |
| 322 | else: |
| 323 | options_str = '' |
| 324 | |
| 325 | return options_str |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 326 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 327 | def xproto_type_to_swagger_type(f): |
| 328 | try: |
| 329 | content_type = f['options']['content_type'] |
| 330 | content_type = eval(content_type) |
| 331 | except: |
| 332 | content_type = None |
| 333 | pass |
| 334 | |
| 335 | if 'choices' in f['options']: |
| 336 | return 'string' |
| 337 | elif content_type == 'date': |
| 338 | return 'string' |
| 339 | elif f['type'] == 'bool': |
| 340 | return 'boolean' |
| 341 | elif f['type'] == 'string': |
| 342 | return 'string' |
| 343 | elif f['type'] in ['int','uint32','int32'] or 'link' in f: |
| 344 | return 'integer' |
| 345 | elif f['type'] in ['double','float']: |
| 346 | return 'string' |
| 347 | |
| 348 | def xproto_field_to_swagger_enum(f): |
| 349 | if 'choices' in f['options']: |
| 350 | list = [] |
| 351 | |
| 352 | for c in eval(xproto_unquote(f['options']['choices'])): |
| 353 | list.append(c[0]) |
| 354 | |
| 355 | return list |
| 356 | else: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 357 | return False |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 358 | |
| 359 | def xproto_is_true(x): |
| 360 | # TODO: Audit xproto and make specification of trueness more uniform |
| 361 | if (x==True) or (x=="True") or (x=='"True"'): |
| 362 | return True |
| 363 | return False |