* YANG to TOSCA translator
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_vnf_configuration.py
1 #
2 # Copyright 2016 RIFT.io Inc
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17
18 from rift.mano.tosca_translator.common.utils import _
19 from rift.mano.tosca_translator.common.utils import convert_keys_to_python
20 from rift.mano.tosca_translator.rwmano.syntax.mano_resource import ManoResource
21 from toscaparser.functions import GetInput
22 from rift.mano.tosca_translator.common.utils import convert_keys_to_python
23
24 from toscaparser.common.exception import ValidationError
25
26
27 # Name used to dynamically load appropriate map class.
28 TARGET_CLASS_NAME = 'ToscaVnfConfiguration'
29
30
31 class ToscaVnfConfiguration(ManoResource):
32 '''Translate TOSCA node type tosca.policies.Scaling.'''
33
34 toscatype = 'tosca.policies.nfv.riftio.vnf_configuration'
35
36 IGNORE_PROPS = []
37
38 def __init__(self, log, policy, metadata=None, vnf_name = None):
39 self.log = log
40 self.name = policy.name
41 self.type_ = 'place-grp'
42 self.metadata = metadata
43 self.policy = policy
44 self.properties = {}
45 self.linked_to_vnf = True
46 self._vnf_name = vnf_name
47 self._vnf_id = None
48 self.scripts = []
49
50 def __str__(self):
51 return "%s(%s)" % (self.name, self.type)
52
53 def handle_properties(self, nodes, groups):
54 tosca_props = self.get_policy_props()
55 if self._vnf_name:
56 vnf_node = self.get_node_with_name(self._vnf_name, nodes)
57 self._vnf_id = vnf_node.id
58 self.properties["vnf-configuration"] = {}
59 prop = {}
60 prop["config-attributes"] = {}
61 prop["script"] = {}
62 if 'config' in tosca_props:
63 if 'config_delay' in tosca_props['config']:
64 prop["config-attributes"]['config-delay'] = tosca_props['config']['config_delay']
65 if 'config_priority' in tosca_props['config']:
66 prop["config-attributes"]['config-priority'] = tosca_props['config']['config_priority']
67 if 'config_template' in tosca_props['config']:
68 prop["config-template"] = tosca_props['config']['config_template']
69 if 'config_details' in tosca_props['config']:
70 if 'script_type' in tosca_props['config']['config_details']:
71 prop["script"]["script-type"] = tosca_props['config']['config_details']['script_type']
72 if 'initial_config' in tosca_props:
73 prop['initial-config-primitive'] = []
74 #print("Weleek " + str(tosca_props['initial_config']))
75 for init_config in tosca_props['initial_config']:
76 if 'parameter' in init_config:
77 parameters = init_config.pop('parameter')
78 init_config['parameter'] = []
79 for key, value in parameters.items():
80 init_config['parameter'].append({'name': key, 'value': str(value)})
81 if 'user_defined_script' in init_config:
82 self.scripts.append('../scripts/{}'. \
83 format(init_config['user_defined_script']))
84 prop['initial-config-primitive'].append(init_config)
85
86 self.properties = prop
87
88 def generate_yang_submodel_gi(self, vnfd):
89 if vnfd is None:
90 return None
91 try:
92 props = convert_keys_to_python(self.properties)
93 vnfd.vnf_configuration.from_dict(props)
94 except Exception as e:
95 err_msg = _("{0} Exception vdu from dict {1}: {2}"). \
96 format(self, props, e)
97 self.log.error(err_msg)
98 raise e
99
100 def get_policy_props(self):
101 tosca_props = {}
102
103 for prop in self.policy.get_properties_objects():
104 if isinstance(prop.value, GetInput):
105 tosca_props[prop.name] = {'get_param': prop.value.input_name}
106 else:
107 tosca_props[prop.name] = prop.value
108 return tosca_props
109 def get_supporting_files(self, files, desc_id=None):
110 if not len(self.scripts):
111 return
112
113 if self._vnf_id not in files:
114 files[desc_id] = []
115
116 for script in self.scripts:
117 files[self._vnf_id].append({
118 'type': 'script',
119 'name': script,
120 },)