0a54bcc013076b6f675b0b28823afa09a4bda0fe
[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
48 def __str__(self):
49 return "%s(%s)" % (self.name, self.type)
50
51 def handle_properties(self, nodes, groups):
52 tosca_props = self.get_policy_props()
53 self.properties["vnf-configuration"] = {}
54 prop = {}
55 prop["config-attributes"] = {}
56 prop["script"] = {}
57 if 'config' in tosca_props:
58 if 'config_delay' in tosca_props['config']:
59 prop["config-attributes"]['config-delay'] = tosca_props['config']['config_delay']
60 if 'config_priority' in tosca_props['config']:
61 prop["config-attributes"]['config-priority'] = tosca_props['config']['config_priority']
62 if 'config_template' in tosca_props['config']:
63 prop["config-template"] = tosca_props['config']['config_template']
64 if 'config_details' in tosca_props['config']:
65 if 'script_type' in tosca_props['config']['config_details']:
66 prop["script"]["script-type"] = tosca_props['config']['config_details']['script_type']
67 self.properties = prop
68
69 def generate_yang_submodel_gi(self, vnfd):
70 if vnfd is None:
71 return None
72 try:
73 props = convert_keys_to_python(self.properties)
74 vnfd.vnf_configuration.from_dict(props)
75 except Exception as e:
76 err_msg = _("{0} Exception vdu from dict {1}: {2}"). \
77 format(self, props, e)
78 self.log.error(err_msg)
79 raise e
80
81 def get_policy_props(self):
82 tosca_props = {}
83
84 for prop in self.policy.get_properties_objects():
85 if isinstance(prop.value, GetInput):
86 tosca_props[prop.name] = {'get_param': prop.value.input_name}
87 else:
88 tosca_props[prop.name] = prop.value
89 return tosca_props