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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 log = logging.getLogger('heat-translator')
25 class TOSCATranslator(object):
26 '''Invokes translation methods.'''
28 def __init__(self, tosca, parsed_params, deploy=None, csar_dir=None):
29 super(TOSCATranslator, self).__init__()
31 self.hot_template = HotTemplate()
32 self.parsed_params = parsed_params
34 self.csar_dir = csar_dir
35 self.node_translator = None
36 log.info(_('Initialized parmaters for translation.'))
38 def _translate_to_hot_yaml(self):
40 self.hot_template.description = self.tosca.description
41 self.hot_template.parameters = self._translate_inputs()
42 self.node_translator = TranslateNodeTemplates(self.tosca,
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
52 """Translate to HOT YAML
54 This method produces a translated output for main template.
55 The nested template, if any referenced by main, will be created
58 self._translate_to_hot_yaml()
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(
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:
72 return yaml_files["output.yaml"]
74 def translate_to_yaml_files_dict(self, base_filename):
75 """Translate to HOT YAML
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.
82 self._translate_to_hot_yaml()
83 return self.hot_template.output_to_yaml_files_dict(
85 self.node_translator.hot_template_version)
87 def _translate_inputs(self):
88 translator = TranslateInputs(self.tosca.inputs, self.parsed_params,
90 return translator.translate()
92 def _translate_outputs(self):
93 translator = TranslateOutputs(self.tosca.outputs, self.node_translator)
94 return translator.translate()
96 # check all properties for all node and ensure they are resolved
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):
103 self.parsed_params[node_prop.value['get_input']]
105 msg = (_('Must specify all input values in \
106 TOSCA template, missing %s.') %
107 node_prop.value['get_input'])
109 raise ValueError(msg)