a637a8ca58069789182f4527ff024adac8cfc2ec
[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                                      nested_resources=False):
76         """Translate to HOT YAML
77
78         This method produces a translated output containing main and
79         any nested templates referenced by main. This output can be
80         programmatically stored into different files by using key as
81         template name and value as template content.
82         """
83         self._translate_to_hot_yaml()
84         return self.hot_template.output_to_yaml_files_dict(
85             base_filename,
86             self.node_translator.hot_template_version, nested_resources)
87
88     def _translate_inputs(self):
89         translator = TranslateInputs(self.tosca.inputs, self.parsed_params,
90                                      self.deploy)
91         return translator.translate()
92
93     def _translate_outputs(self):
94         translator = TranslateOutputs(self.tosca.outputs, self.node_translator)
95         return translator.translate()
96
97     # check all properties for all node and ensure they are resolved
98     # to actual value
99     def _resolve_input(self):
100         for n in self.tosca.nodetemplates:
101             for node_prop in n.get_properties_objects():
102                 if isinstance(node_prop.value, dict):
103                     try:
104                         self.parsed_params[node_prop.value['get_input']]
105                     except Exception:
106                         msg = (_('Must specify all input values in \
107                                 TOSCA template, missing %s.') %
108                                node_prop.value['get_input'])
109                         log.error(msg)
110                         raise ValueError(msg)