9b7cd0397bd115d59c67e61a611e771149a59c84
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_initial_config.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
23 from toscaparser.common.exception import ValidationError
24
25
26 # Name used to dynamically load appropriate map class.
27 TARGET_CLASS_NAME = 'ToscaInitialConfig'
28
29
30 class ToscaInitialConfig(ManoResource):
31 '''Translate TOSCA node type tosca.policies.InitialConfigPrimitive.'''
32
33 toscatype = 'tosca.policies.nfv.riftio.initial_config_primitive'
34
35 IGNORE_PROPS = []
36
37 def __init__(self, log, policy, metadata=None):
38 # TODO(Philip):Not inheriting for ManoResource, as there is no
39 # instance from parser
40 self.log = log
41 self.name = policy.name
42 self.policy = policy
43 self.type_ = 'initial-cfg'
44 self.metadata = metadata
45 self.properties = {}
46 self.scripts = []
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.log.debug(_("{0} with tosca properties: {1}").
54 format(self, tosca_props))
55 self.properties['name'] = tosca_props['name']
56 self.properties['seq'] = \
57 tosca_props['seq']
58 self.properties['user-defined-script'] = \
59 tosca_props['user_defined_script']
60 self.scripts.append('../scripts/{}'. \
61 format(tosca_props['user_defined_script']))
62
63 if 'parameter' in tosca_props:
64 self.properties['parameter'] = []
65 for name, value in tosca_props['parameter'].items():
66 self.properties['parameter'].append({
67 'name': name,
68 'value': value,
69 })
70
71 self.log.debug(_("{0} properties: {1}").format(self, self.properties))
72
73 def get_policy_props(self):
74 tosca_props = {}
75 for prop in self.policy.get_properties_objects():
76 if isinstance(prop.value, GetInput):
77 tosca_props[prop.name] = {'get_param': prop.value.input_name}
78 else:
79 tosca_props[prop.name] = prop.value
80 return tosca_props
81 def get_yang_model_gi(self, nsd, vnfds):
82 props = convert_keys_to_python(self.properties)
83 try:
84 nsd.initial_config_primitive.add().from_dict(props)
85 except Exception as e:
86 err_msg = _("{0} Exception nsd initial config from dict {1}: {2}"). \
87 format(self, props, e)
88 self.log.error(err_msg)
89 raise e
90
91 def generate_yang_model(self, nsd, vnfds, use_gi=False):
92 """Generate yang model for the node"""
93 self.log.debug(_("Generate YANG model for {0}").
94 format(self))
95
96 for key in ToscaInitialConfig.IGNORE_PROPS:
97 if key in self.properties:
98 self.properties.pop(key)
99
100 if use_gi:
101 return self.get_yang_model_gi(nsd, vnfds)
102
103 if 'initial-config-primitive' not in nsd:
104 nsd['initial-config-primitive'] = []
105 prim = {}
106 for key, value in self.properties.items():
107 prim[key] = value
108 nsd['initial-config-primitive'].append(prim)
109
110 def get_supporting_files(self, files, desc_id=None):
111 if not len(self.scripts):
112 return
113
114 if desc_id not in files:
115 files[desc_id] = []
116
117 for script in self.scripts:
118 files[desc_id].append({
119 'type': 'script',
120 'name': script,
121 },)