Fix StatefulEntityType when entitytype is not define
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / property_definition.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import InvalidSchemaError
15 from toscaparser.common.exception import TOSCAException
16 from toscaparser.utils.gettextutils import _
17
18
19 class PropertyDef(object):
20     '''TOSCA built-in Property type.'''
21
22     VALID_PROPERTY_KEYNAMES = (PROPERTY_KEYNAME_DEFAULT,
23                                PROPERTY_KEYNAME_REQUIRED,
24                                PROPERTY_KEYNAME_STATUS) = \
25         ('default', 'required', 'status')
26
27     PROPERTY_REQUIRED_DEFAULT = True
28
29     VALID_REQUIRED_VALUES = ['true', 'false']
30     VALID_STATUS_VALUES = (PROPERTY_STATUS_SUPPORTED,
31                            PROPERTY_STATUS_EXPERIMENTAL) = \
32         ('supported', 'experimental')
33
34     PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED
35
36     def __init__(self, name, value=None, schema=None):
37         self.name = name
38         self.value = value
39         self.schema = schema
40         self._status = self.PROPERTY_STATUS_DEFAULT
41         self._required = self.PROPERTY_REQUIRED_DEFAULT
42
43         # Validate required 'type' property exists
44         try:
45             self.schema['type']
46         except KeyError:
47             msg = (_('Schema definition of "%(pname)s" must have a "type" '
48                      'attribute.') % dict(pname=self.name))
49             ExceptionCollector.appendException(
50                 InvalidSchemaError(message=msg))
51
52         if self.schema:
53             self._load_required_attr_from_schema()
54             self._load_status_attr_from_schema()
55
56     @property
57     def default(self):
58         if self.schema:
59             for prop_key, prop_value in self.schema.items():
60                 if prop_key == self.PROPERTY_KEYNAME_DEFAULT:
61                     return prop_value
62         return None
63
64     @property
65     def required(self):
66         return self._required
67
68     def _load_required_attr_from_schema(self):
69         # IF 'required' keyname exists verify it's a boolean,
70         # if so override default
71         if self.PROPERTY_KEYNAME_REQUIRED in self.schema:
72             value = self.schema[self.PROPERTY_KEYNAME_REQUIRED]
73             if isinstance(value, bool):
74                 self._required = value
75             else:
76                 valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
77                 attr = self.PROPERTY_KEYNAME_REQUIRED
78                 TOSCAException.generate_inv_schema_property_error(self,
79                                                                   attr,
80                                                                   value,
81                                                                   valid_values)
82
83     @property
84     def status(self):
85         return self._status
86
87     def _load_status_attr_from_schema(self):
88         # IF 'status' keyname exists verify it's a valid value,
89         # if so override default
90         if self.PROPERTY_KEYNAME_STATUS in self.schema:
91             value = self.schema[self.PROPERTY_KEYNAME_STATUS]
92             if value in self.VALID_STATUS_VALUES:
93                 self._status = value
94             else:
95                 valid_values = ', '.join(self.VALID_STATUS_VALUES)
96                 attr = self.PROPERTY_KEYNAME_STATUS
97                 TOSCAException.generate_inv_schema_property_error(self,
98                                                                   attr,
99                                                                   value,
100                                                                   valid_values)