blob: 4424824cdf0ea8a40c702df1c4fe3c30f29182d5 [file] [log] [blame]
Matteo Scandoloc3c0f0a2017-10-18 09:53:30 +02001# 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 Williams9a42f872019-02-15 17:56:04 -070015from __future__ import absolute_import
Matteo Scandolo68ab5432017-12-06 15:38:13 -080016from xosgenx.jinja2_extensions import xproto_field_graph_components
17
Zack Williams045b63d2019-01-22 16:30:57 -070018
Matteo Scandolo727e19c2017-12-05 12:53:16 -080019def xproto_tosca_required(null, blank, default=None):
20
Zack Williams045b63d2019-01-22 16:30:57 -070021 if null == "True" or blank == "True" or default != "False":
Matteo Scandolo727e19c2017-12-05 12:53:16 -080022 return "false"
23 return "true"
24
Zack Williams045b63d2019-01-22 16:30:57 -070025
Matteo Scandolo727e19c2017-12-05 12:53:16 -080026def xproto_tosca_field_type(type):
27 """
28 TOSCA requires fields of type 'bool' to be 'boolean'
29 TOSCA requires fields of type 'int32' to be 'integer'
30 """
31
32 if type == "bool":
33 return "boolean"
34 elif type == "int32":
35 return "integer"
36 else:
37 return type
38
Zack Williams045b63d2019-01-22 16:30:57 -070039
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070040def xproto_fields_to_tosca_keys(fields, m):
Matteo Scandolo68ab5432017-12-06 15:38:13 -080041 keys = []
42
43 # look for one_of keys
Zack Williams045b63d2019-01-22 16:30:57 -070044 _one_of = xproto_field_graph_components(fields, m, "tosca_key_one_of")
Matteo Scandolo68ab5432017-12-06 15:38:13 -080045 one_of = [list(i) for i in _one_of]
46
47 # look for explicit keys
48 for f in fields:
Zack Williams045b63d2019-01-22 16:30:57 -070049 if (
50 "tosca_key" in f["options"]
51 and f["options"]["tosca_key"]
52 and "link" not in f
53 ):
54 keys.append(f["name"])
55 if (
56 "tosca_key" in f["options"]
57 and f["options"]["tosca_key"]
58 and ("link" in f and f["link"])
59 ):
60 keys.append("%s_id" % f["name"])
Matteo Scandolo68ab5432017-12-06 15:38:13 -080061 # if not keys are specified and there is a name field, use that as key.
Zack Williams9a42f872019-02-15 17:56:04 -070062 if len(keys) == 0 and "name" in [f["name"] for f in fields]:
Zack Williams045b63d2019-01-22 16:30:57 -070063 keys.append("name")
Matteo Scandolo68ab5432017-12-06 15:38:13 -080064
Zack Williams9a42f872019-02-15 17:56:04 -070065 for of in sorted(one_of):
Matteo Scandolo68ab5432017-12-06 15:38:13 -080066 # check if the field is a link, and in case add _id
67 for index, f in enumerate(of):
68 try:
Zack Williams9a42f872019-02-15 17:56:04 -070069 # FIXME: the 'field' var appears to be dead code, but exists to cause the IndexError?
70 field = [ # noqa: F841
Zack Williams045b63d2019-01-22 16:30:57 -070071 x for x in fields if x["name"] == f and ("link" in x and x["link"])
72 ][0]
Matteo Scandolo68ab5432017-12-06 15:38:13 -080073 of[index] = "%s_id" % f
Zack Williams9a42f872019-02-15 17:56:04 -070074 except IndexError:
Matteo Scandolo68ab5432017-12-06 15:38:13 -080075 # the field is not a link
76 pass
77
78 keys.append(of)
79
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080080 return keys