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 MissingRequiredFieldError
18 from toscaparser.common.exception import UnknownFieldError
20 log = logging.getLogger('tosca')
23 class SubstitutionMappings(object):
24 '''SubstitutionMappings class declaration
26 Substitution_mappings exports the topology template as an
27 implementation of a Node type.
30 SECTIONS = (NODE_TYPE, CAPABILITIES, REQUIREMENTS) = \
31 ('node_type', 'requirements', 'capabilities')
33 def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs,
34 sub_mapped_node_template, custom_defs):
35 self.nodetemplates = nodetemplates
36 self.sub_mapping_def = sub_mapping_def
37 self.inputs = inputs or []
38 self.outputs = outputs or []
39 self.sub_mapped_node_template = sub_mapped_node_template
40 self.custom_defs = custom_defs or {}
43 self._capabilities = None
44 self._requirements = None
47 def get_node_type(cls, sub_mapping_def):
48 if isinstance(sub_mapping_def, dict):
49 return sub_mapping_def.get(cls.NODE_TYPE)
53 return self.sub_mapping_def.get(self.NODE_TYPE)
56 def capabilities(self):
57 return self.sub_mapping_def.get(self.CAPABILITIES)
60 def requirements(self):
61 return self.sub_mapping_def.get(self.REQUIREMENTS)
66 self._validate_inputs()
67 self._validate_capabilities()
68 self._validate_requirements()
69 self._validate_outputs()
71 def _validate_keys(self):
72 """validate the keys of substitution mappings."""
73 for key in self.sub_mapping_def.keys():
74 if key not in self.SECTIONS:
75 ExceptionCollector.appendException(
76 UnknownFieldError(what='Substitution_mappings',
79 def _validate_type(self):
80 """validate the node_type of substitution mappings."""
81 node_type = self.sub_mapping_def.get(self.NODE_TYPE)
83 ExceptionCollector.appendException(
84 MissingRequiredFieldError(
85 what=_('Substitution_mappings used in topology_template'),
86 required=self.NODE_TYPE))
88 node_type_def = self.custom_defs.get(node_type)
90 ExceptionCollector.appendException(
91 InvalidNodeTypeError(what=node_type_def))
93 def _validate_inputs(self):
94 """validate the inputs of substitution mappings."""
96 # The inputs in service template which defines substutition mappings
97 # must be in properties of node template wchich be mapped.
98 inputs_names = list(self.sub_mapped_node_template
99 .get_properties().keys()
100 if self.sub_mapped_node_template else [])
101 for name in inputs_names:
102 if name not in [input.name for input in self.inputs]:
103 ExceptionCollector.appendException(
104 UnknownFieldError(what='SubstitutionMappings',
107 def _validate_capabilities(self):
108 """validate the capabilities of substitution mappings."""
110 # The capabilites must be in node template wchich be mapped.
111 tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
112 node_capabiliteys = self.sub_mapped_node_template.get_capabilities() \
113 if self.sub_mapped_node_template else None
114 for cap in node_capabiliteys.keys() if node_capabiliteys else []:
115 if (tpls_capabilities and
116 cap not in list(tpls_capabilities.keys())):
118 # ExceptionCollector.appendException(
119 # UnknownFieldError(what='Substitution_mappings',
122 def _validate_requirements(self):
123 """validate the requirements of substitution mappings."""
125 # The requirements must be in node template wchich be mapped.
126 tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS)
127 node_requirements = self.sub_mapped_node_template.requirements \
128 if self.sub_mapped_node_template else None
129 for req in node_requirements if node_requirements else []:
130 if (tpls_requirements and
131 req not in list(tpls_requirements.keys())):
133 # ExceptionCollector.appendException(
134 # UnknownFieldError(what='Substitution_mappings',
137 def _validate_outputs(self):
138 """validate the outputs of substitution mappings."""
140 # The outputs in service template which defines substutition mappings
141 # must be in atrributes of node template wchich be mapped.
142 # outputs_names = self.sub_mapped_node_template.get_properties().
143 # keys() if self.sub_mapped_node_template else None
144 # for name in outputs_names:
145 # if name not in [output.name for input in self.outputs]:
146 # ExceptionCollector.appendException(
147 # UnknownFieldError(what='Substitution_mappings',