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
57 def get_node_type(cls, sub_mapping_def):
58 if isinstance(sub_mapping_def, dict):
59 return sub_mapping_def.get(cls.NODE_TYPE)
63 return self.sub_mapping_def.get(self.NODE_TYPE)
66 def capabilities(self):
67 return self.sub_mapping_def.get(self.CAPABILITIES)
70 def requirements(self):
71 return self.sub_mapping_def.get(self.REQUIREMENTS)
76 self._validate_inputs()
77 self._validate_capabilities()
78 self._validate_requirements()
79 self._validate_outputs()
81 def _validate_keys(self):
82 """validate the keys of substitution mappings."""
83 for key in self.sub_mapping_def.keys():
84 if key not in self.SECTIONS:
85 ExceptionCollector.appendException(
86 UnknownFieldError(what='Substitution_mappings',
89 def _validate_type(self):
90 """validate the node_type of substitution mappings."""
91 node_type = self.sub_mapping_def.get(self.NODE_TYPE)
93 ExceptionCollector.appendException(
94 MissingRequiredFieldError(
95 what=_('Substitution_mappings used in topology_template'),
96 required=self.NODE_TYPE))
98 node_type_def = self.custom_defs.get(node_type)
100 ExceptionCollector.appendException(
101 InvalidNodeTypeError(what=node_type_def))
103 def _validate_inputs(self):
104 """validate the inputs of substitution mappings."""
106 # The inputs in service template which defines substutition mappings
107 # must be in properties of node template wchich be mapped.
108 inputs_names = list(self.sub_mapped_node_template
109 .get_properties().keys())
110 for name in inputs_names:
111 if name not in [input.name for input in self.inputs]:
112 ExceptionCollector.appendException(
113 UnknownFieldError(what='Substitution_mappings',
116 def _validate_capabilities(self):
117 """validate the capabilities of substitution mappings."""
119 # The capabilites must be in node template wchich be mapped.
120 tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
121 node_capabiliteys = self.sub_mapped_node_template.get_capabilities()
122 for cap in node_capabiliteys.keys() if node_capabiliteys else []:
123 if (tpls_capabilities and
124 cap not in list(tpls_capabilities.keys())):
126 # ExceptionCollector.appendException(
127 # UnknownFieldError(what='Substitution_mappings',
130 def _validate_requirements(self):
131 """validate the requirements of substitution mappings."""
133 # The requirements must be in node template wchich be mapped.
134 tpls_requirements = self.sub_mapping_def.get(self.REQUIREMENTS)
135 node_requirements = self.sub_mapped_node_template.requirements
136 for req in node_requirements if node_requirements else []:
137 if (tpls_requirements and
138 req not in list(tpls_requirements.keys())):
140 # ExceptionCollector.appendException(
141 # UnknownFieldError(what='Substitution_mappings',
144 def _validate_outputs(self):
145 """validate the outputs of substitution mappings."""
147 # The outputs in service template which defines substutition mappings
148 # must be in atrributes of node template wchich be mapped.
149 # outputs_names = self.sub_mapped_node_template.get_properties().keys()
150 # for name in outputs_names:
151 # if name not in [output.name for input in self.outputs]:
152 # ExceptionCollector.appendException(
153 # UnknownFieldError(what='Substitution_mappings',