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 for declaration that
36 exports the topology template as an implementation of a Node type.
39 SECTIONS = (NODE_TYPE, CAPABILITIES, REQUIREMENTS) = \
40 ('node_type', 'requirements', 'capabilities')
42 def __init__(self, submap_def, nodetemplates, inputs, outputs,
43 submaped_node_template, custom_defs):
44 self.nodetemplates = nodetemplates
45 self.submap_def = submap_def
46 self.inputs = inputs or []
47 self.outputs = outputs or []
48 self.submaped_node_template = submaped_node_template
49 self.custom_defs = custom_defs or {}
52 self._capabilities = None
53 self._requirements = None
55 self.submaped_node_template.substitution_mapped = True
58 def get_node_type(cls, submap_tpl):
59 if isinstance(submap_tpl, dict):
60 return submap_tpl.get(cls.NODE_TYPE)
64 return self.submap_def.get(self.NODE_TYPE)
67 def capabilities(self):
68 return self.submap_def.get(self.CAPABILITIES)
71 def requirements(self):
72 return self.submap_def.get(self.REQUIREMENTS)
77 self._validate_inputs()
78 self._validate_capabilities()
79 self._validate_requirements()
80 self._validate_outputs()
82 def _validate_keys(self):
83 """validate the keys of substitution mappings."""
84 for key in self.submap_def.keys():
85 if key not in self.SECTIONS:
86 ExceptionCollector.appendException(
87 UnknownFieldError(what='Substitution_mappings',
90 def _validate_type(self):
91 """validate the node_type of substitution mappings."""
92 node_type = self.submap_def.get(self.NODE_TYPE)
94 ExceptionCollector.appendException(
95 MissingRequiredFieldError(
96 what=_('Substitution_mappings used in topology_template'),
97 required=self.NODE_TYPE))
99 node_type_def = self.custom_defs.get(node_type)
100 if not node_type_def:
101 ExceptionCollector.appendException(
102 InvalidNodeTypeError(what=node_type_def))
104 def _validate_inputs(self):
105 """validate the inputs of substitution mappings."""
107 # The inputs in service template which defines substutition mappings
108 # must be in properties of node template wchich be mapped.
109 inputs_names = list(self.submaped_node_template
110 .get_properties().keys())
111 for name in inputs_names:
112 if name not in [input.name for input in self.inputs]:
113 ExceptionCollector.appendException(
114 UnknownFieldError(what='Substitution_mappings',
117 def _validate_capabilities(self):
118 """validate the capabilities of substitution mappings."""
120 # The capabilites must be in node template wchich be mapped.
121 tpls_capabilities = self.submap_def.get(self.CAPABILITIES)
122 node_capabiliteys = self.submaped_node_template.get_capabilities()
123 for cap in node_capabiliteys.keys() if node_capabiliteys else []:
124 if (tpls_capabilities and
125 cap not in list(tpls_capabilities.keys())):
127 # ExceptionCollector.appendException(
128 # UnknownFieldError(what='Substitution_mappings',
131 def _validate_requirements(self):
132 """validate the requirements of substitution mappings."""
134 # The requirements must be in node template wchich be mapped.
135 tpls_requirements = self.submap_def.get(self.REQUIREMENTS)
136 node_requirements = self.submaped_node_template.requirements
137 for req in node_requirements if node_requirements else []:
138 if (tpls_requirements and
139 req not in list(tpls_requirements.keys())):
141 # ExceptionCollector.appendException(
142 # UnknownFieldError(what='Substitution_mappings',
145 def _validate_outputs(self):
146 """validate the outputs of substitution mappings."""
148 # The outputs in service template which defines substutition mappings
149 # must be in atrributes of node template wchich be mapped.
150 # outputs_names = self.submaped_node_template.get_properties().keys()
151 # for name in outputs_names:
152 # if name not in [output.name for input in self.outputs]:
153 # ExceptionCollector.appendException(
154 # UnknownFieldError(what='Substitution_mappings',