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 UnknownFieldError
21 from toscaparser.elements.nodetype import NodeType
22 from toscaparser.utils.gettextutils import _
24 log = logging.getLogger('tosca')
27 class SubstitutionMappings(object):
28 '''SubstitutionMappings class declaration
30 SubstitutionMappings exports the topology template as an
31 implementation of a Node type.
34 SECTIONS = (NODE_TYPE, REQUIREMENTS, CAPABILITIES) = \
35 ('node_type', 'requirements', 'capabilities')
37 def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs,
38 sub_mapped_node_template, custom_defs):
39 self.nodetemplates = nodetemplates
40 self.sub_mapping_def = sub_mapping_def
41 self.inputs = inputs or []
42 self.outputs = outputs or []
43 self.sub_mapped_node_template = sub_mapped_node_template
44 self.custom_defs = custom_defs or {}
47 self._capabilities = None
48 self._requirements = None
52 if self.sub_mapping_def:
53 return self.sub_mapping_def.get(self.NODE_TYPE)
56 def get_node_type(cls, sub_mapping_def):
57 if isinstance(sub_mapping_def, dict):
58 return sub_mapping_def.get(cls.NODE_TYPE)
62 return self.sub_mapping_def.get(self.NODE_TYPE)
65 def capabilities(self):
66 return self.sub_mapping_def.get(self.CAPABILITIES)
69 def requirements(self):
70 return self.sub_mapping_def.get(self.REQUIREMENTS)
73 def node_definition(self):
74 return NodeType(self.node_type, self.custom_defs)
81 # SubstitutionMapping class syntax validation
82 self._validate_inputs()
83 self._validate_capabilities()
84 self._validate_requirements()
85 self._validate_outputs()
87 def _validate_keys(self):
88 """validate the keys of substitution mappings."""
89 for key in self.sub_mapping_def.keys():
90 if key not in self.SECTIONS:
91 ExceptionCollector.appendException(
92 UnknownFieldError(what=_('SubstitutionMappings'),
95 def _validate_type(self):
96 """validate the node_type of substitution mappings."""
97 node_type = self.sub_mapping_def.get(self.NODE_TYPE)
99 ExceptionCollector.appendException(
100 MissingRequiredFieldError(
101 what=_('SubstitutionMappings used in topology_template'),
102 required=self.NODE_TYPE))
104 node_type_def = self.custom_defs.get(node_type)
105 if not node_type_def:
106 ExceptionCollector.appendException(
107 InvalidNodeTypeError(what=node_type))
109 def _validate_inputs(self):
110 """validate the inputs of substitution mappings.
112 The inputs defined by the topology template have to match the
113 properties of the node type or the substituted node. If there are
114 more inputs than the substituted node has properties, default values
115 must be defined for those inputs.
118 all_inputs = set([input.name for input in self.inputs])
119 required_properties = set([p.name for p in
120 self.node_definition.
121 get_properties_def_objects()
122 if p.required and p.default is None])
123 # Must provide inputs for required properties of node type.
124 for property in required_properties:
125 # Check property which is 'required' and has no 'default' value
126 if property not in all_inputs:
127 ExceptionCollector.appendException(
128 MissingRequiredInputError(
129 what=_('SubstitutionMappings with node_type ')
131 input_name=property))
133 # If the optional properties of node type need to be customized by
134 # substituted node, it also is necessary to define inputs for them,
135 # otherwise they are not mandatory to be defined.
136 customized_parameters = set(self.sub_mapped_node_template
137 .get_properties().keys()
138 if self.sub_mapped_node_template else [])
139 all_properties = set([p.name for p in
140 self.node_definition.
141 get_properties_def_objects()])
142 for parameter in customized_parameters - all_inputs:
143 if parameter in all_properties:
144 ExceptionCollector.appendException(
145 MissingRequiredInputError(
146 what=_('SubstitutionMappings with node_type ')
148 input_name=parameter))
150 # Additional inputs are not in the properties of node type must
151 # provide default values. Currently the scenario may not happen
152 # because of parameters validation in nodetemplate, here is a
154 for input in self.inputs:
155 if input.name in all_inputs - all_properties \
156 and input.default is None:
157 ExceptionCollector.appendException(
158 MissingDefaultValueError(
159 what=_('SubstitutionMappings with node_type ')
161 input_name=input.name))
163 def _validate_capabilities(self):
164 """validate the capabilities of substitution mappings."""
166 # The capabilites must be in node template wchich be mapped.
167 tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
168 node_capabiliteys = self.sub_mapped_node_template.get_capabilities() \
169 if self.sub_mapped_node_template else None
170 for cap in node_capabiliteys.keys() if node_capabiliteys else []:
171 if (tpls_capabilities and
172 cap not in list(tpls_capabilities.keys())):
174 # ExceptionCollector.appendException(
175 # UnknownFieldError(what='SubstitutionMappings',
178 def _validate_requirements(self):
179 """validate the requirements of substitution mappings."""
181 # The requirements must be in node template wchich be mapped.
182 tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS)
183 node_requirements = self.sub_mapped_node_template.requirements \
184 if self.sub_mapped_node_template else None
185 for req in node_requirements if node_requirements else []:
186 if (tpls_requirements and
187 req not in list(tpls_requirements.keys())):
189 # ExceptionCollector.appendException(
190 # UnknownFieldError(what='SubstitutionMappings',
193 def _validate_outputs(self):
194 """validate the outputs of substitution mappings."""
196 # The outputs in service template which defines substutition mappings
197 # must be in atrributes of node template wchich be mapped.
198 # outputs_names = self.sub_mapped_node_template.get_properties().
199 # keys() if self.sub_mapped_node_template else None
200 # for name in outputs_names:
201 # if name not in [output.name for input in self.outputs]:
202 # ExceptionCollector.appendException(
203 # UnknownFieldError(what='SubstitutionMappings',