blob: 50bcf0e6b0912bd630c177d7f2e03c5a996277dd [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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
Matteo Scandolo292cc2a2017-07-31 19:02:12 -070017from base import xproto_string_type, unquote
18
19def xproto_type_to_ui_type(f):
20 try:
21 content_type = f['options']['content_type']
22 content_type = eval(content_type)
23 except:
24 content_type = None
25 pass
26
27 if 'choices' in f['options']:
28 return 'select';
29 elif content_type == 'date':
30 return 'date'
31 elif f['type'] == 'bool':
32 return 'boolean'
33 elif f['type'] == 'string':
34 return xproto_string_type(f['options'])
35 elif f['type'] in ['int','uint32','int32'] or 'link' in f:
36 return 'number'
37 elif f['type'] in ['double','float']:
38 return 'string'
39
40def xproto_options_choices_to_dict(choices):
41 list = []
42
43 for c in eval(choices):
44 list.append({'id': c[0], 'label': c[1]})
45 if len(list) > 0:
46 return list
47 else:
48 return None
49
50def xproto_validators(f):
51 # To be cleaned up when we formalize validation in xproto
52 validators = []
53
54 # bound-based validators
55 bound_validators = [('max_length','maxlength'), ('min', 'min'), ('max', 'max')]
56
57 for v0, v1 in bound_validators:
58 try:
59 validators.append({'name':v1, 'int_value':int(f['options'][v0])})
60 except KeyError:
61 pass
62
63 # validators based on content_type
64 content_type_validators = ['ip', 'url', 'email']
65
66 for v in content_type_validators:
67 #if f['name']=='ip': pdb.set_trace()
68 try:
69 val = unquote(f['options']['content_type'])==v
70 if not val:
71 raise KeyError
72
73 validators.append({'name':v, 'bool_value': True})
74 except KeyError:
75 pass
76
77 # required validator
78 try:
79 required = f['options']['blank']=='False' and f['options']['null']=='False'
80 if required:
81 validators.append({'name':'required', 'bool_value':required})
82 except KeyError:
83 pass
84
85 return validators
86
87def is_number(s):
88 try:
89 float(s)
90 return True
91 except ValueError:
92 return False
93
94def xproto_default_to_gui(default):
95 val = "null"
96 if is_number(default):
97 val = str(default)
98 elif eval(default) == True:
99 val = 'true'
100 elif eval(default) == False:
101 val = 'false'
102 elif eval(default) == None:
103 val = 'null'
104 else:
105 val = str(default)
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700106 return val
107
108
109def xproto_links_to_modeldef_relations(llst):
110 outlist = []
111 seen = []
112 for l in llst:
113 try:
114 t = l['link_type']
115 except KeyError, e:
116 raise e
117
118 if l['peer']['fqn'] not in seen and t != 'manytomany':
119 on_field = 'null'
120 if l['link_type'] == 'manytoone':
121 on_field = l['src_port']
122 elif l['link_type'] == 'onetomany':
123 on_field = l['dst_port']
124 outlist.append('- {model: %s, type: %s, on_field: %s}\n' % (l['peer']['name'], l['link_type'], on_field))
125 seen.append(l['peer'])
126
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800127 return outlist