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