Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
| 16 | from .base import xproto_string_type, unquote |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 17 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 19 | def xproto_type_to_ui_type(f): |
| 20 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | content_type = f["options"]["content_type"] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 22 | content_type = eval(content_type) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | except BaseException: |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 24 | content_type = None |
| 25 | pass |
| 26 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 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 | |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 40 | |
| 41 | def xproto_options_choices_to_dict(choices): |
| 42 | list = [] |
| 43 | |
| 44 | for c in eval(choices): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 45 | list.append({"id": c[0], "label": c[1]}) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 46 | if len(list) > 0: |
| 47 | return list |
| 48 | else: |
| 49 | return None |
| 50 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 52 | def xproto_dict_to_sorted_string(d): |
| 53 | """ |
| 54 | sorts the dict by key and returns a string representation, which makes |
| 55 | for better consistency when testing |
| 56 | """ |
| 57 | ft = [] # formatted tuples |
| 58 | for k, v in sorted(d.items(), key=lambda t: t[0]): # sorted by key |
| 59 | if v is not None: |
| 60 | ft.append("'%s': '%s'" % (k, v)) |
| 61 | else: |
| 62 | ft.append("'%s': None" % k) |
| 63 | return "{%s}" % ", ".join(ft) |
| 64 | |
| 65 | |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 66 | def xproto_validators(f): |
| 67 | # To be cleaned up when we formalize validation in xproto |
| 68 | validators = [] |
| 69 | |
| 70 | # bound-based validators |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 71 | bound_validators = [("max_length", "maxlength"), ("min", "min"), ("max", "max")] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 72 | |
| 73 | for v0, v1 in bound_validators: |
| 74 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 75 | validators.append({"name": v1, "int_value": int(f["options"][v0])}) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 76 | except KeyError: |
| 77 | pass |
| 78 | |
| 79 | # validators based on content_type |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 80 | content_type_validators = ["ip", "url", "email"] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 81 | |
| 82 | for v in content_type_validators: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 83 | # if f['name']=='ip': pdb.set_trace() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 84 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 85 | val = unquote(f["options"]["content_type"]) == v |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 86 | if not val: |
| 87 | raise KeyError |
| 88 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | validators.append({"name": v, "bool_value": True}) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 90 | except KeyError: |
| 91 | pass |
| 92 | |
| 93 | # required validator |
| 94 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 95 | required = f["options"]["blank"] == "False" and f["options"]["null"] == "False" |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 96 | if required: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | validators.append({"name": "required", "bool_value": required}) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 98 | except KeyError: |
| 99 | pass |
| 100 | |
| 101 | return validators |
| 102 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 104 | def is_number(s): |
| 105 | try: |
| 106 | float(s) |
| 107 | return True |
| 108 | except ValueError: |
| 109 | return False |
| 110 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 111 | |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 112 | def xproto_default_to_gui(default): |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 113 | # TODO: Using `eval` here is potentially dangerous as it may allow code injection |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 114 | val = "null" |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 115 | try: |
| 116 | if is_number(default): |
| 117 | val = str(default) |
| 118 | elif eval(default) is True: |
| 119 | val = "true" |
| 120 | elif eval(default) is False: |
| 121 | val = "false" |
| 122 | elif eval(default) is None: |
| 123 | val = "null" |
| 124 | else: |
| 125 | val = str(default) |
| 126 | except NameError: |
| 127 | # val was a function call, and we can't pass those to the GUI |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 128 | val = "null" |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 129 | |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 130 | return val |
| 131 | |
| 132 | |
| 133 | def xproto_links_to_modeldef_relations(llst): |
| 134 | outlist = [] |
| 135 | seen = [] |
| 136 | for l in llst: |
| 137 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 138 | t = l["link_type"] |
| 139 | except KeyError as e: |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 140 | raise e |
| 141 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 142 | if l["peer"]["fqn"] not in seen and t != "manytomany": |
| 143 | on_field = "null" |
| 144 | if l["link_type"] == "manytoone": |
| 145 | on_field = l["src_port"] |
| 146 | elif l["link_type"] == "onetomany": |
| 147 | on_field = l["dst_port"] |
| 148 | outlist.append( |
Zack Williams | be5ee1c | 2019-03-18 15:33:07 -0700 | [diff] [blame] | 149 | "{model: %s, type: %s, on_field: %s}" |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 150 | % (l["peer"]["name"], l["link_type"], on_field) |
| 151 | ) |
| 152 | seen.append(l["peer"]) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 153 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 154 | return outlist |