Service template with substitution mapping definition can deployed
[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 # 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
28
29
30 log = logging.getLogger('tosca')
31
32
33 class Substitution_mappings(object):
34     '''Substitution_mappings class declaration
35
36     Substitution_mappings exports the topology template as an
37     implementation of a Node type.
38     '''
39
40     SECTIONS = (NODE_TYPE, CAPABILITIES, REQUIREMENTS) = \
41                ('node_type', 'requirements', 'capabilities')
42
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 {}
51         self._validate()
52
53         self._capabilities = None
54         self._requirements = None
55
56     @classmethod
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)
60
61     @property
62     def node_type(self):
63         return self.sub_mapping_def.get(self.NODE_TYPE)
64
65     @property
66     def capabilities(self):
67         return self.sub_mapping_def.get(self.CAPABILITIES)
68
69     @property
70     def requirements(self):
71         return self.sub_mapping_def.get(self.REQUIREMENTS)
72
73     def _validate(self):
74         self._validate_keys()
75         self._validate_type()
76         self._validate_inputs()
77         self._validate_capabilities()
78         self._validate_requirements()
79         self._validate_outputs()
80
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',
87                                       field=key))
88
89     def _validate_type(self):
90         """validate the node_type of substitution mappings."""
91         node_type = self.sub_mapping_def.get(self.NODE_TYPE)
92         if not node_type:
93             ExceptionCollector.appendException(
94                 MissingRequiredFieldError(
95                     what=_('Substitution_mappings used in topology_template'),
96                     required=self.NODE_TYPE))
97
98         node_type_def = self.custom_defs.get(node_type)
99         if not node_type_def:
100             ExceptionCollector.appendException(
101                 InvalidNodeTypeError(what=node_type_def))
102
103     def _validate_inputs(self):
104         """validate the inputs of substitution mappings."""
105
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                             if self.sub_mapped_node_template else [])
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',
115                                       field=name))
116
117     def _validate_capabilities(self):
118         """validate the capabilities of substitution mappings."""
119
120         # The capabilites must be in node template wchich be mapped.
121         tpls_capabilities = self.sub_mapping_def.get(self.CAPABILITIES)
122         node_capabiliteys = self.sub_mapped_node_template.get_capabilities() \
123             if self.sub_mapped_node_template else None
124         for cap in node_capabiliteys.keys() if node_capabiliteys else []:
125             if (tpls_capabilities and
126                     cap not in list(tpls_capabilities.keys())):
127                 pass
128                 # ExceptionCollector.appendException(
129                 #    UnknownFieldError(what='Substitution_mappings',
130                 #                      field=cap))
131
132     def _validate_requirements(self):
133         """validate the requirements of substitution mappings."""
134
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             if self.sub_mapped_node_template else None
139         for req in node_requirements if node_requirements else []:
140             if (tpls_requirements and
141                     req not in list(tpls_requirements.keys())):
142                 pass
143                 # ExceptionCollector.appendException(
144                 #    UnknownFieldError(what='Substitution_mappings',
145                 #                      field=req))
146
147     def _validate_outputs(self):
148         """validate the outputs of substitution mappings."""
149         pass
150         # The outputs in service template which defines substutition mappings
151         # must be in atrributes of node template wchich be mapped.
152         # outputs_names = self.sub_mapped_node_template.get_properties().
153         #     keys() if self.sub_mapped_node_template else None
154         # for name in outputs_names:
155         #    if name not in [output.name for input in self.outputs]:
156         #        ExceptionCollector.appendException(
157         #            UnknownFieldError(what='Substitution_mappings',
158         #                              field=name))