Change the name of Substitution_mappings to SubstitutionMappings
[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 MissingRequiredFieldError
18 from toscaparser.common.exception import UnknownFieldError
19
20 log = logging.getLogger('tosca')
21
22
23 class SubstitutionMappings(object):
24     '''SubstitutionMappings class declaration
25
26     Substitution_mappings exports the topology template as an
27     implementation of a Node type.
28     '''
29
30     SECTIONS = (NODE_TYPE, CAPABILITIES, REQUIREMENTS) = \
31                ('node_type', 'requirements', 'capabilities')
32
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 {}
41         self._validate()
42
43         self._capabilities = None
44         self._requirements = None
45
46     @classmethod
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)
50
51     @property
52     def node_type(self):
53         return self.sub_mapping_def.get(self.NODE_TYPE)
54
55     @property
56     def capabilities(self):
57         return self.sub_mapping_def.get(self.CAPABILITIES)
58
59     @property
60     def requirements(self):
61         return self.sub_mapping_def.get(self.REQUIREMENTS)
62
63     def _validate(self):
64         self._validate_keys()
65         self._validate_type()
66         self._validate_inputs()
67         self._validate_capabilities()
68         self._validate_requirements()
69         self._validate_outputs()
70
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',
77                                       field=key))
78
79     def _validate_type(self):
80         """validate the node_type of substitution mappings."""
81         node_type = self.sub_mapping_def.get(self.NODE_TYPE)
82         if not node_type:
83             ExceptionCollector.appendException(
84                 MissingRequiredFieldError(
85                     what=_('Substitution_mappings used in topology_template'),
86                     required=self.NODE_TYPE))
87
88         node_type_def = self.custom_defs.get(node_type)
89         if not node_type_def:
90             ExceptionCollector.appendException(
91                 InvalidNodeTypeError(what=node_type_def))
92
93     def _validate_inputs(self):
94         """validate the inputs of substitution mappings."""
95
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',
105                                       field=name))
106
107     def _validate_capabilities(self):
108         """validate the capabilities of substitution mappings."""
109
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())):
117                 pass
118                 # ExceptionCollector.appendException(
119                 #    UnknownFieldError(what='Substitution_mappings',
120                 #                      field=cap))
121
122     def _validate_requirements(self):
123         """validate the requirements of substitution mappings."""
124
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())):
132                 pass
133                 # ExceptionCollector.appendException(
134                 #    UnknownFieldError(what='Substitution_mappings',
135                 #                      field=req))
136
137     def _validate_outputs(self):
138         """validate the outputs of substitution mappings."""
139         pass
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',
148         #                              field=name))