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 nested_resources=False):
76 """Translate to HOT YAML
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.
83 self._translate_to_hot_yaml()
84 return self.hot_template.output_to_yaml_files_dict(
86 self.node_translator.hot_template_version, nested_resources)
88 def _translate_inputs(self):
89 translator = TranslateInputs(self.tosca.inputs, self.parsed_params,
91 return translator.translate()
93 def _translate_outputs(self):
94 translator = TranslateOutputs(self.tosca.outputs, self.node_translator)
95 return translator.translate()
97 # check all properties for all node and ensure they are resolved
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):
104 self.parsed_params[node_prop.value['get_input']]
106 msg = (_('Must specify all input values in \
107 TOSCA template, missing %s.') %
108 node_prop.value['get_input'])
110 raise ValueError(msg)