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