Merge "Update the nfv tosca definietion against the latest spec"
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca_translator.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 import logging
15 from toscaparser.utils.gettextutils import _
16 from translator.hot.syntax.hot_template import HotTemplate
17 from translator.hot.translate_inputs import TranslateInputs
18 from translator.hot.translate_node_templates import TranslateNodeTemplates
19 from translator.hot.translate_outputs import TranslateOutputs
20
21 log = logging.getLogger('heat-translator')
22
23
24 class TOSCATranslator(object):
25     '''Invokes translation methods.'''
26
27     def __init__(self, tosca, parsed_params, deploy=None):
28         super(TOSCATranslator, self).__init__()
29         self.tosca = tosca
30         self.hot_template = HotTemplate()
31         self.parsed_params = parsed_params
32         self.deploy = deploy
33         self.node_translator = None
34         log.info(_('Initialized parmaters for translation.'))
35
36     def translate(self):
37         self._resolve_input()
38         self.hot_template.description = self.tosca.description
39         self.hot_template.parameters = self._translate_inputs()
40         self.node_translator = TranslateNodeTemplates(self.tosca,
41                                                       self.hot_template)
42         self.hot_template.resources = self.node_translator.translate()
43         self.hot_template.outputs = self._translate_outputs()
44         return self.hot_template.output_to_yaml()
45
46     def _translate_inputs(self):
47         translator = TranslateInputs(self.tosca.inputs, self.parsed_params,
48                                      self.deploy)
49         return translator.translate()
50
51     def _translate_outputs(self):
52         translator = TranslateOutputs(self.tosca.outputs, self.node_translator)
53         return translator.translate()
54
55     # check all properties for all node and ensure they are resolved
56     # to actual value
57     def _resolve_input(self):
58         for n in self.tosca.nodetemplates:
59             for node_prop in n.get_properties_objects():
60                 if isinstance(node_prop.value, dict):
61                     try:
62                         self.parsed_params[node_prop.value['get_input']]
63                     except Exception:
64                         msg = (_('Must specify all input values in \
65                                 TOSCA template, missing %s.') %
66                                node_prop.value['get_input'])
67                         log.error(msg)
68                         raise ValueError(msg)