b9d4c775df01726ae08fa59270dea1125ca5d60c
[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 import six
16 from toscaparser.utils.gettextutils import _
17 from translator.hot.syntax.hot_template import HotTemplate
18 from translator.hot.translate_inputs import TranslateInputs
19 from translator.hot.translate_node_templates import TranslateNodeTemplates
20 from translator.hot.translate_outputs import TranslateOutputs
21
22 log = logging.getLogger('heat-translator')
23
24
25 class TOSCATranslator(object):
26     '''Invokes translation methods.'''
27
28     def __init__(self, tosca, parsed_params, deploy=None, csar_dir=None):
29         super(TOSCATranslator, self).__init__()
30         self.tosca = tosca
31         self.hot_template = HotTemplate()
32         self.parsed_params = parsed_params
33         self.deploy = deploy
34         self.csar_dir = csar_dir
35         self.node_translator = None
36         log.info(_('Initialized parmaters for translation.'))
37
38     def _translate_to_hot_yaml(self):
39         self._resolve_input()
40         self.hot_template.description = self.tosca.description
41         self.hot_template.parameters = self._translate_inputs()
42         self.node_translator = TranslateNodeTemplates(self.tosca,
43                                                       self.hot_template,
44                                                       csar_dir=self.csar_dir)
45         self.hot_template.resources = \
46             self.node_translator.translate()
47         self.hot_template.outputs = self._translate_outputs()
48         if self.node_translator.hot_template_version is None:
49             self.node_translator.hot_template_version = HotTemplate.LATEST
50
51     def translate(self):
52         """Translate to HOT YAML
53
54         This method produces a translated output for main template.
55         The nested template, if any referenced by main, will be created
56         as a separate file.
57         """
58         self._translate_to_hot_yaml()
59
60         # TODO(mvelten) go back to calling hot_template.output_to_yaml instead
61         # for stdout once embed_substack_templates is correctly implemented
62         # return self.hot_template.output_to_yaml(
63         #     self.node_translator.hot_template_version)
64         yaml_files = self.hot_template.output_to_yaml_files_dict(
65             "output.yaml",
66             self.node_translator.hot_template_version)
67         for name, content in six.iteritems(yaml_files):
68             if name != "output.yaml":
69                 with open(name, 'w+') as f:
70                     f.write(content)
71
72         return yaml_files["output.yaml"]
73
74     def translate_to_yaml_files_dict(self, base_filename):
75         """Translate to HOT YAML
76
77         This method produces a translated output containing main and
78         any nested templates referenced by main. This output can be
79         programmatically stored into different files by using key as
80         template name and value as template content.
81         """
82         self._translate_to_hot_yaml()
83         return self.hot_template.output_to_yaml_files_dict(
84             base_filename,
85             self.node_translator.hot_template_version)
86
87     def _translate_inputs(self):
88         translator = TranslateInputs(self.tosca.inputs, self.parsed_params,
89                                      self.deploy)
90         return translator.translate()
91
92     def _translate_outputs(self):
93         translator = TranslateOutputs(self.tosca.outputs, self.node_translator)
94         return translator.translate()
95
96     # check all properties for all node and ensure they are resolved
97     # to actual value
98     def _resolve_input(self):
99         for n in self.tosca.nodetemplates:
100             for node_prop in n.get_properties_objects():
101                 if isinstance(node_prop.value, dict):
102                     try:
103                         self.parsed_params[node_prop.value['get_input']]
104                     except Exception:
105                         msg = (_('Must specify all input values in \
106                                 TOSCA template, missing %s.') %
107                                node_prop.value['get_input'])
108                         log.error(msg)
109                         raise ValueError(msg)