Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [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 | |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 15 | from xosgenx.jinja2_extensions import xproto_field_graph_components |
| 16 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 17 | |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 18 | def xproto_tosca_required(null, blank, default=None): |
| 19 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 20 | if null == "True" or blank == "True" or default != "False": |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 21 | return "false" |
| 22 | return "true" |
| 23 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 24 | |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 25 | def xproto_tosca_field_type(type): |
| 26 | """ |
| 27 | TOSCA requires fields of type 'bool' to be 'boolean' |
| 28 | TOSCA requires fields of type 'int32' to be 'integer' |
| 29 | """ |
| 30 | |
| 31 | if type == "bool": |
| 32 | return "boolean" |
| 33 | elif type == "int32": |
| 34 | return "integer" |
| 35 | else: |
| 36 | return type |
| 37 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 38 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 39 | def xproto_fields_to_tosca_keys(fields, m): |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 40 | keys = [] |
| 41 | |
| 42 | # look for one_of keys |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 43 | _one_of = xproto_field_graph_components(fields, m, "tosca_key_one_of") |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 44 | one_of = [list(i) for i in _one_of] |
| 45 | |
| 46 | # look for explicit keys |
| 47 | for f in fields: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 48 | if ( |
| 49 | "tosca_key" in f["options"] |
| 50 | and f["options"]["tosca_key"] |
| 51 | and "link" not in f |
| 52 | ): |
| 53 | keys.append(f["name"]) |
| 54 | if ( |
| 55 | "tosca_key" in f["options"] |
| 56 | and f["options"]["tosca_key"] |
| 57 | and ("link" in f and f["link"]) |
| 58 | ): |
| 59 | keys.append("%s_id" % f["name"]) |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 60 | # if not keys are specified and there is a name field, use that as key. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 61 | if len(keys) == 0 and "name" in map(lambda f: f["name"], fields): |
| 62 | keys.append("name") |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 63 | |
| 64 | for of in one_of: |
| 65 | # check if the field is a link, and in case add _id |
| 66 | for index, f in enumerate(of): |
| 67 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 68 | field = [ |
| 69 | x for x in fields if x["name"] == f and ("link" in x and x["link"]) |
| 70 | ][0] |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 71 | of[index] = "%s_id" % f |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 72 | except IndexError as e: |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 73 | # the field is not a link |
| 74 | pass |
| 75 | |
| 76 | keys.append(of) |
| 77 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 78 | return keys |