update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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 for init_config in tosca_props['initial_config']:
75 if 'parameter' in init_config:
76 parameters = init_config.pop('parameter')
77 init_config['parameter'] = []
78 for parameter in parameters:
79 for key, value in parameter.items():
80 init_config['parameter'].append({'name': key, 'value': str(value)})
81
82 if 'user_defined_script' in init_config:
83 self.scripts.append('../scripts/{}'. \
84 format(init_config['user_defined_script']))
85 prop['initial-config-primitive'].append(init_config)
86
87 self.properties = prop
88
89 def generate_yang_submodel_gi(self, vnfd):
90 if vnfd is None:
91 return None
92 try:
93 props = convert_keys_to_python(self.properties)
94 vnfd.vnf_configuration.from_dict(props)
95 except Exception as e:
96 err_msg = _("{0} Exception vdu from dict {1}: {2}"). \
97 format(self, props, e)
98 self.log.error(err_msg)
99 raise e
100
101 def get_policy_props(self):
102 tosca_props = {}
103
104 for prop in self.policy.get_properties_objects():
105 if isinstance(prop.value, GetInput):
106 tosca_props[prop.name] = {'get_param': prop.value.input_name}
107 else:
108 tosca_props[prop.name] = prop.value
109 return tosca_props
110 def get_supporting_files(self, files, desc_id=None):
111 if not len(self.scripts):
112 return
113
114 if self._vnf_id not in files:
115 files[self._vnf_id] = []
116
117 for script in self.scripts:
118 files[self._vnf_id].append({
119 'type': 'script',
120 'name': script,
121 },)