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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
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 _
26 log = logging.getLogger('tosca')
29 class SubstitutionMappings(object):
30 '''SubstitutionMappings class declaration
32 SubstitutionMappings exports the topology template as an
33 implementation of a Node type.
36 SECTIONS = (NODE_TYPE, REQUIREMENTS, CAPABILITIES) = \
37 ('node_type', 'requirements', 'capabilities')
39 OPTIONAL_OUTPUTS = ['tosca_id', 'tosca_name', 'state']
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 {}
51 self._capabilities = None
52 self._requirements = None
56 if self.sub_mapping_def:
57 return self.sub_mapping_def.get(self.NODE_TYPE)
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)
66 return self.sub_mapping_def.get(self.NODE_TYPE)
69 def capabilities(self):
70 return self.sub_mapping_def.get(self.CAPABILITIES)
73 def requirements(self):
74 return self.sub_mapping_def.get(self.REQUIREMENTS)
77 def node_definition(self):
78 return NodeType(self.node_type, self.custom_defs)
85 # SubstitutionMapping class syntax validation
86 self._validate_inputs()
87 self._validate_capabilities()
88 self._validate_requirements()
89 self._validate_outputs()
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'),
99 def _validate_type(self):
100 """validate the node_type of substitution mappings."""
101 node_type = self.sub_mapping_def.get(self.NODE_TYPE)
103 ExceptionCollector.appendException(
104 MissingRequiredFieldError(
105 what=_('SubstitutionMappings used in topology_template'),
106 required=self.NODE_TYPE))
108 node_type_def = self.custom_defs.get(node_type)
109 if not node_type_def:
110 ExceptionCollector.appendException(
111 InvalidNodeTypeError(what=node_type))
113 def _validate_inputs(self):
114 """validate the inputs of substitution mappings.
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.
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 ')
135 input_name=property))
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 ')
150 input_name=parameter))
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
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 ')
163 input_name=input.name))
165 def _validate_capabilities(self):
166 """validate the capabilities of substitution mappings."""
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())):
176 # ExceptionCollector.appendException(
177 # UnknownFieldError(what='SubstitutionMappings',
180 def _validate_requirements(self):
181 """validate the requirements of substitution mappings."""
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())):
191 # ExceptionCollector.appendException(
192 # UnknownFieldError(what='SubstitutionMappings',
195 def _validate_outputs(self):
196 """validate the outputs of substitution mappings.
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.
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(
213 where=_('SubstitutionMappings with node_type ')
215 output_name=output.name))
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
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 ')
228 output_name=attribute))