Merge "Add output in vRNC for substitution mappings"
[parser.git] / tosca2heat / tosca-parser / toscaparser / substitution_mappings.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
13 import logging
14
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 _
23
24 log = logging.getLogger('tosca')
25
26
27 class SubstitutionMappings(object):
28     '''SubstitutionMappings class declaration
29
30     SubstitutionMappings exports the topology template as an
31     implementation of a Node type.
32     '''
33
34     SECTIONS = (NODE_TYPE, REQUIREMENTS, CAPABILITIES) = \
35                ('node_type', 'requirements', 'capabilities')
36
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 {}
45         self._validate()
46
47         self._capabilities = None
48         self._requirements = None
49
50     @property
51     def type(self):
52         if self.sub_mapping_def:
53             return self.sub_mapping_def.get(self.NODE_TYPE)
54
55     @classmethod
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)
59
60     @property
61     def node_type(self):
62         return self.sub_mapping_def.get(self.NODE_TYPE)
63
64     @property
65     def capabilities(self):
66         return self.sub_mapping_def.get(self.CAPABILITIES)
67
68     @property
69     def requirements(self):
70         return self.sub_mapping_def.get(self.REQUIREMENTS)
71
72     @property
73     def node_definition(self):
74         return NodeType(self.node_type, self.custom_defs)
75
76     def _validate(self):
77         # Basic validation
78         self._validate_keys()
79         self._validate_type()
80
81         # SubstitutionMapping class syntax validation
82         self._validate_inputs()
83         self._validate_capabilities()
84         self._validate_requirements()
85         self._validate_outputs()
86
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'),
93                                       field=key))
94
95     def _validate_type(self):
96         """validate the node_type of substitution mappings."""
97         node_type = self.sub_mapping_def.get(self.NODE_TYPE)
98         if not node_type:
99             ExceptionCollector.appendException(
100                 MissingRequiredFieldError(
101                     what=_('SubstitutionMappings used in topology_template'),
102                     required=self.NODE_TYPE))
103
104         node_type_def = self.custom_defs.get(node_type)
105         if not node_type_def:
106             ExceptionCollector.appendException(
107                 InvalidNodeTypeError(what=node_type))
108
109     def _validate_inputs(self):
110         """validate the inputs of substitution mappings.
111
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.
116         """
117
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 ')
130                         + self.node_type,
131                         input_name=property))
132
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 ')
147                         + self.node_type,
148                         input_name=parameter))
149
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
153         # guarantee.
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 ')
160                         + self.node_type,
161                         input_name=input.name))
162
163     def _validate_capabilities(self):
164         """validate the capabilities of substitution mappings."""
165
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())):
173                 pass
174                 # ExceptionCollector.appendException(
175                 #    UnknownFieldError(what='SubstitutionMappings',
176                 #                      field=cap))
177
178     def _validate_requirements(self):
179         """validate the requirements of substitution mappings."""
180
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())):
188                 pass
189                 # ExceptionCollector.appendException(
190                 #    UnknownFieldError(what='SubstitutionMappings',
191                 #                      field=req))
192
193     def _validate_outputs(self):
194         """validate the outputs of substitution mappings."""
195         pass
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',
204         #                              field=name))