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
19 # from toscaparser.common.exception import ValidationError
20 # from toscaparser.utils.gettextutils import _
21 # from toscaparser.utils import validateutils
22 # from toscaparser.nodetemplate import NodeTemplate
23 # from toscaparser.elements.nodetype import NodeType
24 # from toscaparser.parameters import Input
25 # from toscaparser.parameters import Output
26 # from toscaparser.groups import Group
27 # from toscaparser.policy import Policy
30 log = logging.getLogger('tosca')
33 class Substitution_mappings(object):
34 '''Substitution_mappings class declaration
36 Substitution_mappings exports the topology template as an
37 implementation of a Node type.
40 SECTIONS = (NODE_TYPE, CAPABILITIES, REQUIREMENTS) = \
41 ('node_type', 'requirements', 'capabilities')
43 def __init__(self, sub_mapping_def, nodetemplates, inputs, outputs,
44 sub_mapped_node_template, custom_defs):
45 self.nodetemplates = nodetemplates
46 self.sub_mapping_def = sub_mapping_def
47 self.inputs = inputs or []
48 self.outputs = outputs or []
49 self.sub_mapped_node_template = sub_mapped_node_template
50 self.custom_defs = custom_defs or {}
53 self._capabilities = None
54 self._requirements = None
56 self.sub_mapped_node_template.substitution_mapped = True
59 def get_node_type(cls, sub_mapping_def):
60 if isinstance(sub_mapping_def, dict):
61 return sub_mapping_def.get(cls.NODE_TYPE)
65 return self.sub_mapping_def.get(self.NODE_TYPE)
68 def capabilities(self):
69 return self.sub_mapping_def.get(self.CAPABILITIES)
72 def requirements(self):
73 return self.sub_mapping_def.get(self.REQUIREMENTS)
78 self._validate_inputs()
79 self._validate_capabilities()
80 self._validate_requirements()
81 self._validate_outputs()
83 def _validate_keys(self):
84 """validate the keys of substitution mappings."""
85 for key in self.sub_mapping_def.keys():
86 if key not in self.SECTIONS:
87 ExceptionCollector.appendException(
88 UnknownFieldError(what='Substitution_mappings',
91 def _validate_type(self):
92 """validate the node_type of substitution mappings."""
93 node_type = self.sub_mapping_def.get(self.NODE_TYPE)
95 ExceptionCollector.appendException(
96 MissingRequiredFieldError(
97 what=_('Substitution_mappings used in topology_template'),
98 required=self.NODE_TYPE))
100 node_type_def = self.custom_defs.get(node_type)
101 if not node_type_def:
102 ExceptionCollector.appendException(
103 InvalidNodeTypeError(what=node_type_def))
105 def _validate_inputs(self):
106 """validate the inputs of substitution mappings."""
108 # The inputs in service template which defines substutition mappings
109 # must be in properties of node template wchich be mapped.
110 inputs_names = list(self.sub_mapped_node_template
111 .get_properties().keys())
112 for name in inputs_names:
113 if name not in [input.name for input in self.inputs]:
114 ExceptionCollector.appendException(
115 UnknownFieldError(what='Substitution_mappings',
118 def _validate_capabilities(self):
119 """validate the capabilities of substitution mappings."""
121 # The capabilites must be in node template wchich be mapped.
122 tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
123 node_capabiliteys = self.sub_mapped_node_template.get_capabilities()
124 for cap in node_capabiliteys.keys() if node_capabiliteys else []:
125 if (tpls_capabilities and
126 cap not in list(tpls_capabilities.keys())):
128 # ExceptionCollector.appendException(
129 # UnknownFieldError(what='Substitution_mappings',
132 def _validate_requirements(self):
133 """validate the requirements of substitution mappings."""
135 # The requirements must be in node template wchich be mapped.
136 tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS)
137 node_requirements = self.sub_mapped_node_template.requirements
138 for req in node_requirements if node_requirements else []:
139 if (tpls_requirements and
140 req not in list(tpls_requirements.keys())):
142 # ExceptionCollector.appendException(
143 # UnknownFieldError(what='Substitution_mappings',
146 def _validate_outputs(self):
147 """validate the outputs of substitution mappings."""
149 # The outputs in service template which defines substutition mappings
150 # must be in atrributes of node template wchich be mapped.
151 # outputs_names = self.sub_mapped_node_template.get_properties().keys()
152 # for name in outputs_names:
153 # if name not in [output.name for input in self.outputs]:
154 # ExceptionCollector.appendException(
155 # UnknownFieldError(what='Substitution_mappings',