Bug fixe version info print
[parser.git] / tosca2heat / tosca-parser / toscaparser / substitution_mappings.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 import logging
14
15 from toscaparser.common.exception import ExceptionCollector
16 from toscaparser.common.exception import InvalidNodeTypeError
17 from toscaparser.common.exception import MissingDefaultValueError
18 from toscaparser.common.exception import MissingRequiredFieldError
19 from toscaparser.common.exception import MissingRequiredInputError
20 from toscaparser.common.exception import MissingRequiredOutputError
21 from toscaparser.common.exception import UnknownFieldError
22 from toscaparser.common.exception import UnknownOutputError
23 from toscaparser.elements.nodetype import NodeType
24 from toscaparser.utils.gettextutils import _
25
26 log = logging.getLogger('tosca')
27
28
29 class SubstitutionMappings(object):
30     '''SubstitutionMappings class declaration
31
32     SubstitutionMappings exports the topology template as an
33     implementation of a Node type.
34     '''
35
36     SECTIONS = (NODE_TYPE, REQUIREMENTS, CAPABILITIES) = \
37                ('node_type', 'requirements', 'capabilities')
38
39     OPTIONAL_OUTPUTS = ['tosca_id', 'tosca_name', 'state']
40
41     def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs,
42                  sub_mapped_node_template, custom_defs):
43         self.nodetemplates = nodetemplates
44         self.sub_mapping_def = sub_mapping_def
45         self.inputs = inputs or []
46         self.outputs = outputs or []
47         self.sub_mapped_node_template = sub_mapped_node_template
48         self.custom_defs = custom_defs or {}
49         self._validate()
50
51         self._capabilities = None
52         self._requirements = None
53
54     @property
55     def type(self):
56         if self.sub_mapping_def:
57             return self.sub_mapping_def.get(self.NODE_TYPE)
58
59     @classmethod
60     def get_node_type(cls, sub_mapping_def):
61         if isinstance(sub_mapping_def, dict):
62             return sub_mapping_def.get(cls.NODE_TYPE)
63
64     @property
65     def node_type(self):
66         return self.sub_mapping_def.get(self.NODE_TYPE)
67
68     @property
69     def capabilities(self):
70         return self.sub_mapping_def.get(self.CAPABILITIES)
71
72     @property
73     def requirements(self):
74         return self.sub_mapping_def.get(self.REQUIREMENTS)
75
76     @property
77     def node_definition(self):
78         return NodeType(self.node_type, self.custom_defs)
79
80     def _validate(self):
81         # Basic validation
82         self._validate_keys()
83         self._validate_type()
84
85         # SubstitutionMapping class syntax validation
86         self._validate_inputs()
87         self._validate_capabilities()
88         self._validate_requirements()
89         self._validate_outputs()
90
91     def _validate_keys(self):
92         """validate the keys of substitution mappings."""
93         for key in self.sub_mapping_def.keys():
94             if key not in self.SECTIONS:
95                 ExceptionCollector.appendException(
96                     UnknownFieldError(what=_('SubstitutionMappings'),
97                                       field=key))
98
99     def _validate_type(self):
100         """validate the node_type of substitution mappings."""
101         node_type = self.sub_mapping_def.get(self.NODE_TYPE)
102         if not node_type:
103             ExceptionCollector.appendException(
104                 MissingRequiredFieldError(
105                     what=_('SubstitutionMappings used in topology_template'),
106                     required=self.NODE_TYPE))
107
108         node_type_def = self.custom_defs.get(node_type)
109         if not node_type_def:
110             ExceptionCollector.appendException(
111                 InvalidNodeTypeError(what=node_type))
112
113     def _validate_inputs(self):
114         """validate the inputs of substitution mappings.
115
116         The inputs defined by the topology template have to match the
117         properties of the node type or the substituted node template. If
118         there are more inputs than the substituted node has properties,
119         default values must be defined for those inputs.
120         """
121
122         all_inputs = set([input.name for input in self.inputs])
123         required_properties = set([p.name for p in
124                                    self.node_definition.
125                                    get_properties_def_objects()
126                                    if p.required and p.default is None])
127         # Must provide inputs for required properties of node type.
128         for property in required_properties:
129             # Check property which is 'required' and has no 'default' value
130             if property not in all_inputs:
131                 ExceptionCollector.appendException(
132                     MissingRequiredInputError(
133                         what=_('SubstitutionMappings with node_type ')
134                         + self.node_type,
135                         input_name=property))
136
137         # If the optional properties of node type need to be customized by
138         # substituted node, it also is necessary to define inputs for them,
139         # otherwise they are not mandatory to be defined.
140         customized_parameters = set(self.sub_mapped_node_template
141                                     .get_properties().keys()
142                                     if self.sub_mapped_node_template else [])
143         all_properties = set(self.node_definition.get_properties_def())
144         for parameter in customized_parameters - all_inputs:
145             if parameter in all_properties:
146                 ExceptionCollector.appendException(
147                     MissingRequiredInputError(
148                         what=_('SubstitutionMappings with node_type ')
149                         + self.node_type,
150                         input_name=parameter))
151
152         # Additional inputs are not in the properties of node type must
153         # provide default values. Currently the scenario may not happen
154         # because of parameters validation in nodetemplate, here is a
155         # guarantee.
156         for input in self.inputs:
157             if input.name in all_inputs - all_properties \
158                and input.default is None:
159                 ExceptionCollector.appendException(
160                     MissingDefaultValueError(
161                         what=_('SubstitutionMappings with node_type ')
162                         + self.node_type,
163                         input_name=input.name))
164
165     def _validate_capabilities(self):
166         """validate the capabilities of substitution mappings."""
167
168         # The capabilites must be in node template wchich be mapped.
169         tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
170         node_capabiliteys = self.sub_mapped_node_template.get_capabilities() \
171             if self.sub_mapped_node_template else None
172         for cap in node_capabiliteys.keys() if node_capabiliteys else []:
173             if (tpls_capabilities and
174                     cap not in list(tpls_capabilities.keys())):
175                 pass
176                 # ExceptionCollector.appendException(
177                 #    UnknownFieldError(what='SubstitutionMappings',
178                 #                      field=cap))
179
180     def _validate_requirements(self):
181         """validate the requirements of substitution mappings."""
182
183         # The requirements must be in node template wchich be mapped.
184         tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS)
185         node_requirements = self.sub_mapped_node_template.requirements \
186             if self.sub_mapped_node_template else None
187         for req in node_requirements if node_requirements else []:
188             if (tpls_requirements and
189                     req not in list(tpls_requirements.keys())):
190                 pass
191                 # ExceptionCollector.appendException(
192                 #    UnknownFieldError(what='SubstitutionMappings',
193                 #                      field=req))
194
195     def _validate_outputs(self):
196         """validate the outputs of substitution mappings.
197
198         The outputs defined by the topology template have to match the
199         attributes of the node type or the substituted node template,
200         and the observable attributes of the substituted node template
201         have to be defined as attributes of the node type or outputs in
202         the topology template.
203         """
204
205         # The outputs defined by the topology template have to match the
206         # attributes of the node type according to the specification, but
207         # it's reasonable that there are more inputs than the node type
208         # has properties, the specification will be amended?
209         for output in self.outputs:
210             if output.name not in self.node_definition.get_attributes_def():
211                 ExceptionCollector.appendException(
212                     UnknownOutputError(
213                         where=_('SubstitutionMappings with node_type ')
214                         + self.node_type,
215                         output_name=output.name))
216
217         # The observable attributes of the substituted node template
218         # have to be defined as attributes of the node type or outputs in
219         # the topology template, the attributes in tosca.node.root are
220         # optional.
221         for attribute in self.node_definition.get_attributes_def():
222             if attribute not in [output.name for output in self.outputs] \
223                and attribute not in self.OPTIONAL_OUTPUTS:
224                 ExceptionCollector.appendException(
225                     MissingRequiredOutputError(
226                         what=_('SubstitutionMappings with node_type ')
227                         + self.node_type,
228                         output_name=attribute))