* YANG to TOSCA translator
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / 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 # Copyright 2016 RIFT.io Inc
15
16 from rift.mano.tosca_translator.common.utils import _
17 from rift.mano.tosca_translator.rwmano.syntax.mano_template import ManoTemplate
18 from rift.mano.tosca_translator.rwmano.translate_inputs import TranslateInputs
19 from rift.mano.tosca_translator.rwmano.translate_node_templates \
20 import TranslateNodeTemplates
21 from rift.mano.tosca_translator.rwmano.translate_outputs \
22 import TranslateOutputs
23
24
25 class TOSCATranslator(object):
26 '''Invokes translation methods.'''
27
28 def __init__(self, log, tosca, parsed_params, deploy=None, use_gi=False):
29 super(TOSCATranslator, self).__init__()
30 self.log = log
31 self.tosca = tosca
32 self.mano_template = ManoTemplate(log)
33 self.parsed_params = parsed_params
34 self.deploy = deploy
35 self.use_gi = use_gi
36 self.node_translator = None
37 log.info(_('Initialized parmaters for translation.'))
38
39 def translate(self):
40 self._resolve_input()
41 self.mano_template.description = self.tosca.description
42 self.mano_template.parameters = self._translate_inputs()
43 self.node_translator = TranslateNodeTemplates(self.log,
44 self.tosca,
45 self.mano_template)
46 self.mano_template.resources = self.node_translator.translate()
47 # TODO(Philip): Currently doing groups and policies seperately
48 # due to limitations with parser
49 self.mano_template.groups = self.node_translator.translate_groups()
50 self.mano_template.policies = self.node_translator.translate_policies()
51 self.mano_template.metadata = self.node_translator.metadata
52 # Currently we do not use outputs, so not processing them
53 # self.mano_template.outputs = self._translate_outputs()
54 return self.mano_template.output_to_yang(use_gi=self.use_gi)
55
56 def _translate_inputs(self):
57 translator = TranslateInputs(self.log,
58 self.tosca.inputs,
59 self.parsed_params,
60 self.deploy)
61 return translator.translate()
62
63 def _translate_outputs(self):
64 translator = TranslateOutputs(self.log,
65 self.tosca.outputs,
66 self.node_translator)
67 return translator.translate()
68
69 # check all properties for all node and ensure they are resolved
70 # to actual value
71 def _resolve_input(self):
72 for n in self.tosca.nodetemplates:
73 for node_prop in n.get_properties_objects():
74 if isinstance(node_prop.value, dict):
75 if 'get_input' in node_prop.value:
76 try:
77 self.parsed_params[node_prop.value['get_input']]
78 except Exception:
79 msg = (_('Must specify all input values in '
80 'TOSCA template, missing %s.') %
81 node_prop.value['get_input'])
82 self.log.error(msg)
83 raise ValueError(msg)