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