blob: 9e3b4a8c13d092b311340bbc3e5b7a90893e2352 [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
Matteo Scandolo68ab5432017-12-06 15:38:13 -080015from xosgenx.jinja2_extensions import xproto_field_graph_components
16
Matteo Scandolo727e19c2017-12-05 12:53:16 -080017def xproto_tosca_required(null, blank, default=None):
18
19 if null == 'True' or blank == 'True' or default != 'False':
20 return "false"
21 return "true"
22
23def xproto_tosca_field_type(type):
24 """
25 TOSCA requires fields of type 'bool' to be 'boolean'
26 TOSCA requires fields of type 'int32' to be 'integer'
27 """
28
29 if type == "bool":
30 return "boolean"
31 elif type == "int32":
32 return "integer"
33 else:
34 return type
35
Matteo Scandoloc3c0f0a2017-10-18 09:53:30 +020036def xproto_fields_to_tosca_keys(fields):
Matteo Scandolo68ab5432017-12-06 15:38:13 -080037 keys = []
38
39 # look for one_of keys
40 _one_of = xproto_field_graph_components(fields, 'tosca_key_one_of')
41 one_of = [list(i) for i in _one_of]
42
43 # look for explicit keys
44 for f in fields:
45 if 'tosca_key' in f['options'] and f['options']['tosca_key'] and 'link' not in f:
46 keys.append(f['name'])
47 if 'tosca_key' in f['options'] and f['options']['tosca_key'] and ('link' in f and f['link']):
48 keys.append("%s_id" % f['name'])
49 # if not keys are specified and there is a name field, use that as key.
50 if len(keys) == 0 and 'name' in map(lambda f: f['name'], fields):
51 keys.append('name')
52
53 for of in one_of:
54 # check if the field is a link, and in case add _id
55 for index, f in enumerate(of):
56 try:
57 field = [x for x in fields if x['name'] == f and ('link' in x and x['link'])][0]
58 of[index] = "%s_id" % f
59 except IndexError, e:
60 # the field is not a link
61 pass
62
63 keys.append(of)
64
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080065 return keys