update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_vnf_ns_service_primitive.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 = 'ToscaVnfNSServiceConfiguration'
29
30
31 class ToscaVnfNSServiceConfiguration(ManoResource):
32 '''Translate TOSCA node type tosca.policies.Scaling.'''
33
34 toscatype = 'tosca.policies.nfv.riftio.ns_service_primitives'
35
36 IGNORE_PROPS = []
37 VALUE_TYPE_CONVERSION_MAP = {
38 'integer': 'INTEGER',
39 'string':'STRING',
40 'float':'DECIMAL',
41 'INTEGER': 'INTEGER',
42 'FLOAT':'DECIMAL'
43
44 }
45
46 def __init__(self, log, policy, metadata=None, vnf_name = None):
47 self.log = log
48 self.name = policy.name
49 self.type_ = 'place-grp'
50 self.metadata = metadata
51 self.linked_to_vnf = False
52 self.policy = policy
53 self.service_primitive = None
54 self.properties = {}
55 self.scripts = []
56
57 def __str__(self):
58 return "%s(%s)" % (self.name, self.type)
59
60 def handle_properties(self, nodes, groups):
61 tosca_props = self.get_policy_props()
62 service_primitive = {}
63 if 'name' in tosca_props:
64 service_primitive['name'] = tosca_props['name']
65 if 'user_defined_script' in tosca_props:
66 service_primitive['user_defined_script'] = tosca_props['user_defined_script']
67 self.scripts.append('../scripts/{}'. \
68 format(tosca_props['user_defined_script']))
69
70
71 if 'parameter' in tosca_props:
72 service_primitive['parameter'] = []
73 for parameter in tosca_props['parameter']:
74 prop = {}
75 if 'name' in parameter:
76 prop['name'] = parameter['name']
77 if 'hidden' in parameter:
78 prop['hidden'] = parameter['hidden']
79 if 'mandatory' in parameter:
80 prop['mandatory'] = parameter['mandatory']
81 if 'data_type' in parameter:
82 prop['data_type'] = ToscaVnfNSServiceConfiguration.VALUE_TYPE_CONVERSION_MAP[parameter['data_type']]
83 if 'default_value' in parameter:
84 prop['default_value'] = str(parameter['default_value'])
85 service_primitive['parameter'].append(prop)
86
87 self.service_primitive = service_primitive
88
89
90
91
92 #self.properties = prop
93
94 def generate_yang_submodel_gi(self, vnfd):
95 pass
96
97 def generate_yang_model(self, nsd, vnfds, use_gi):
98 if self.service_primitive is not None:
99 nsd.service_primitive.add().from_dict(self.service_primitive)
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 if desc_id not in files:
114 return
115 for script in self.scripts:
116 files[desc_id].append({
117 'type': 'script',
118 'name': script,
119 },)