update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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'] = int(tosca_props['seq'])
57 self.properties['user-defined-script'] = \
58 tosca_props['user_defined_script']
59 self.scripts.append('../scripts/{}'. \
60 format(tosca_props['user_defined_script']))
61
62 if 'parameter' in tosca_props:
63 self.properties['parameter'] = []
64 for parameter in tosca_props['parameter']:
65 self.properties['parameter'].append({
66 'name': parameter['name'],
67 'value': str(parameter['value']),
68 })
69 self.log.debug(_("{0} properties: {1}").format(self, self.properties))
70
71 def get_policy_props(self):
72 tosca_props = {}
73 for prop in self.policy.get_properties_objects():
74 if isinstance(prop.value, GetInput):
75 tosca_props[prop.name] = {'get_param': prop.value.input_name}
76 else:
77 tosca_props[prop.name] = prop.value
78 return tosca_props
79 def get_yang_model_gi(self, nsd, vnfds):
80 props = convert_keys_to_python(self.properties)
81 try:
82 nsd.initial_config_primitive.add().from_dict(props)
83 except Exception as e:
84 err_msg = _("{0} Exception nsd initial config from dict {1}: {2}"). \
85 format(self, props, e)
86 self.log.error(err_msg)
87 raise e
88
89 def generate_yang_model(self, nsd, vnfds, use_gi=False):
90 """Generate yang model for the node"""
91 self.log.debug(_("Generate YANG model for {0}").
92 format(self))
93
94 for key in ToscaInitialConfig.IGNORE_PROPS:
95 if key in self.properties:
96 self.properties.pop(key)
97
98 if use_gi:
99 return self.get_yang_model_gi(nsd, vnfds)
100
101 if 'initial-config-primitive' not in nsd:
102 nsd['initial-config-primitive'] = []
103 prim = {}
104 for key, value in self.properties.items():
105 prim[key] = value
106 nsd['initial-config-primitive'].append(prim)
107
108 def get_supporting_files(self, files, desc_id=None):
109 if not len(self.scripts):
110 return
111
112 if desc_id not in files:
113 files[desc_id] = []
114
115 for script in self.scripts:
116 files[desc_id].append({
117 'type': 'script',
118 'name': script,
119 },)