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 |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 19 | from pattern import en |
| 20 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 21 | class FieldNotFound(Exception): |
| 22 | def __init__(self, message): |
| 23 | super(FieldNotFound, self).__init__(message) |
| 24 | |
Sapan Bhatia | d456759 | 2017-07-24 17:26:26 -0400 | [diff] [blame] | 25 | def xproto_debug(**kwargs): |
| 26 | print kwargs |
| 27 | pdb.set_trace() |
| 28 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 29 | def xproto_unquote(s): |
| 30 | return unquote(s) |
| 31 | |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 32 | def unquote(s): |
| 33 | if (s.startswith('"') and s.endswith('"')): |
| 34 | return s[1:-1] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 35 | else: |
| 36 | return s |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 37 | |
| 38 | def xproto_singularize(field): |
| 39 | try: |
| 40 | # The user has set a singular, as an exception that cannot be handled automatically |
| 41 | singular = field['options']['singular'] |
| 42 | singular = unquote(singular) |
| 43 | except KeyError: |
| 44 | singular = en.singularize(field['name']) |
| 45 | |
| 46 | return singular |
| 47 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 48 | def xproto_singularize_pluralize(field): |
| 49 | try: |
| 50 | # The user has set a plural, as an exception that cannot be handled automatically |
| 51 | plural = field['options']['plural'] |
| 52 | plural = unquote(plural) |
| 53 | except KeyError: |
| 54 | plural = en.pluralize(en.singularize(field['name'])) |
| 55 | |
| 56 | return plural |
| 57 | |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 58 | def xproto_pluralize(field): |
| 59 | try: |
| 60 | # The user has set a plural, as an exception that cannot be handled automatically |
| 61 | plural = field['options']['plural'] |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 62 | plural = unquote(plural) |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 63 | except KeyError: |
| 64 | plural = en.pluralize(field['name']) |
| 65 | |
| 66 | return plural |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 67 | |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 68 | def xproto_base_def(model_name, base, suffix='', suffix_list=[]): |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 69 | if (model_name=='XOSBase'): |
| 70 | return '(models.Model, PlModelMixIn)' |
| 71 | elif (not base): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 72 | return '' |
| 73 | else: |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 74 | int_base = [i['name']+suffix for i in base if i['name'] in suffix_list] |
| 75 | ext_base = [i['name'] for i in base if i['name'] not in suffix_list] |
| 76 | return '(' + ','.join(int_base + ext_base) + ')' |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 77 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 78 | def xproto_first_non_empty(lst): |
| 79 | for l in lst: |
| 80 | if l: return l |
| 81 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 82 | def xproto_api_type(field): |
| 83 | try: |
| 84 | if (unquote(field['options']['content_type'])=='date'): |
Scott Baker | 450e5d0 | 2017-09-06 11:18:16 -0700 | [diff] [blame] | 85 | return 'double' |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 86 | except KeyError: |
| 87 | pass |
| 88 | |
| 89 | return field['type'] |
| 90 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 91 | |
| 92 | def xproto_base_name(n): |
| 93 | # Hack - Refactor NetworkParameter* to make this go away |
| 94 | if (n.startswith('NetworkParameter')): |
| 95 | return '_' |
| 96 | |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 97 | expr = r'^[A-Z]+[a-z]*' |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 98 | |
| 99 | try: |
| 100 | match = re.findall(expr, n)[0] |
| 101 | except: |
| 102 | return '_' |
| 103 | |
| 104 | return match |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 105 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 106 | def xproto_base_fields(m, table): |
| 107 | fields = [] |
| 108 | |
| 109 | for b in m['bases']: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 110 | option1 = b['fqn'] |
| 111 | try: |
| 112 | option2 = m['package'] + '.' + b['name'] |
| 113 | except TypeError: |
| 114 | option2 = option1 |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 115 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 116 | accessor = None |
| 117 | if option1 in table: accessor = option1 |
| 118 | elif option2 in table: accessor = option2 |
| 119 | |
| 120 | if accessor: |
| 121 | base_fields = xproto_base_fields(table[accessor], table) |
| 122 | |
| 123 | model_fields = table[accessor]['fields'] |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 124 | fields.extend(base_fields) |
| 125 | fields.extend(model_fields) |
| 126 | |
Matteo Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 127 | if 'no_sync' in m['options'] and m['options']['no_sync']: |
| 128 | fields = [f for f in fields if f['name'] != 'backend_status' and f['name'] != 'backend_code'] |
| 129 | |
| 130 | if 'no_policy' in m['options'] and m['options']['no_policy']: |
| 131 | fields = [f for f in fields if f['name'] != 'policy_status' and f['name'] != 'policy_code'] |
| 132 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 133 | return fields |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 134 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 135 | def xproto_base_rlinks(m, table): |
| 136 | links = [] |
| 137 | |
| 138 | for base in m['bases']: |
| 139 | b = base['name'] |
| 140 | if b in table: |
| 141 | base_rlinks = xproto_base_rlinks(table[b], table) |
| 142 | |
| 143 | model_rlinks = table[b]['rlinks'] |
| 144 | links.extend(base_rlinks) |
| 145 | links.extend(model_rlinks) |
| 146 | |
| 147 | return links |
| 148 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 149 | def xproto_base_links(m, table): |
| 150 | links = [] |
| 151 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 152 | for base in m['bases']: |
| 153 | b = base['name'] |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 154 | if b in table: |
| 155 | base_links = xproto_base_links(table[b], table) |
| 156 | |
| 157 | model_links = table[b]['links'] |
| 158 | links.extend(base_links) |
| 159 | links.extend(model_links) |
| 160 | return links |
| 161 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 162 | def xproto_string_type(xptags): |
| 163 | try: |
| 164 | max_length = eval(xptags['max_length']) |
| 165 | except: |
| 166 | max_length = 1024 |
| 167 | |
| 168 | if ('varchar' not in xptags): |
| 169 | return 'string' |
| 170 | else: |
| 171 | return 'text' |
| 172 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 173 | def xproto_tuplify(nested_list_or_set): |
| 174 | if not isinstance(nested_list_or_set, list) and not isinstance(nested_list_or_set, set): |
| 175 | return nested_list_or_set |
| 176 | else: |
| 177 | return tuple([xproto_tuplify(i) for i in nested_list_or_set]) |
| 178 | |
| 179 | def xproto_field_graph_components(fields, tag='unique_with'): |
| 180 | def find_components(graph): |
| 181 | pending = set(graph.keys()) |
| 182 | components = [] |
| 183 | |
| 184 | while pending: |
| 185 | front = { pending.pop() } |
| 186 | component = set() |
| 187 | |
| 188 | while front: |
| 189 | node = front.pop() |
| 190 | neighbours = graph[node] |
| 191 | neighbours-=component # These we have already visited |
| 192 | front |= neighbours |
| 193 | |
| 194 | pending-=neighbours |
| 195 | component |= neighbours |
| 196 | |
| 197 | components.append(component) |
| 198 | |
| 199 | return components |
| 200 | |
| 201 | field_graph = {} |
| 202 | field_names = {f['name'] for f in fields} |
| 203 | |
| 204 | for f in fields: |
| 205 | try: |
| 206 | tagged_str = unquote(f['options'][tag]) |
| 207 | tagged_fields = tagged_str.split(',') |
| 208 | |
| 209 | for uf in tagged_fields: |
| 210 | if uf not in field_names: |
| 211 | raise FieldNotFound('Field %s not found'%uf) |
| 212 | |
| 213 | field_graph.setdefault(f['name'],set()).add(uf) |
| 214 | field_graph.setdefault(uf,set()).add(f['name']) |
| 215 | except KeyError: |
| 216 | pass |
| 217 | |
| 218 | components = find_components(field_graph) |
| 219 | return components |
| 220 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 221 | def xproto_api_opts(field): |
| 222 | options = [] |
| 223 | if 'max_length' in field['options'] and field['type']=='string': |
| 224 | options.append('(val).maxLength = %s'%field['options']['max_length']) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 225 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 226 | try: |
| 227 | if field['options']['null'] == 'False': |
| 228 | options.append('(val).nonNull = true') |
| 229 | except KeyError: |
| 230 | pass |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 231 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 232 | if 'link' in field and 'model' in field['options']: |
| 233 | options.append('(foreignKey).modelName = "%s"'%field['options']['model']) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 234 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 235 | if options: |
| 236 | options_str = '[' + ', '.join(options) + ']' |
| 237 | else: |
| 238 | options_str = '' |
| 239 | |
| 240 | return options_str |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 241 | |
Matteo Scandolo | 3b7857b | 2017-06-30 16:22:33 -0700 | [diff] [blame] | 242 | def xproto_tosca_required(null, blank, default=None): |
| 243 | |
| 244 | if null == 'True' or blank == 'True' or default != 'False': |
| 245 | return "false" |
| 246 | return "true" |
| 247 | |
| 248 | def xproto_tosca_field_type(type): |
| 249 | """ |
| 250 | TOSCA requires fields of type 'bool' to be 'boolean' |
| 251 | """ |
| 252 | if type == "bool": |
| 253 | return "boolean" |
| 254 | else: |
| 255 | return type |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 256 | |
| 257 | def xproto_type_to_swagger_type(f): |
| 258 | try: |
| 259 | content_type = f['options']['content_type'] |
| 260 | content_type = eval(content_type) |
| 261 | except: |
| 262 | content_type = None |
| 263 | pass |
| 264 | |
| 265 | if 'choices' in f['options']: |
| 266 | return 'string' |
| 267 | elif content_type == 'date': |
| 268 | return 'string' |
| 269 | elif f['type'] == 'bool': |
| 270 | return 'boolean' |
| 271 | elif f['type'] == 'string': |
| 272 | return 'string' |
| 273 | elif f['type'] in ['int','uint32','int32'] or 'link' in f: |
| 274 | return 'integer' |
| 275 | elif f['type'] in ['double','float']: |
| 276 | return 'string' |
| 277 | |
| 278 | def xproto_field_to_swagger_enum(f): |
| 279 | if 'choices' in f['options']: |
| 280 | list = [] |
| 281 | |
| 282 | for c in eval(xproto_unquote(f['options']['choices'])): |
| 283 | list.append(c[0]) |
| 284 | |
| 285 | return list |
| 286 | else: |
| 287 | return False |