TOSCA to YANG Translator Initial commit
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_placement_group.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 = 'ToscaPlacementGroup'
29
30
31 class ToscaPlacementGroup(ManoResource):
32 '''Translate TOSCA node type tosca.policies.Scaling.'''
33
34 toscatype = 'tosca.policies.nfv.riftio.placement'
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._vnf_name = vnf_name
46
47 def __str__(self):
48 return "%s(%s)" % (self.name, self.type)
49
50 def handle_properties(self, nodes, groups):
51 tosca_props = self.get_policy_props()
52 self.properties['name'] = tosca_props['name']
53 self.properties['strategy'] = tosca_props['strategy']
54 self.properties['requirement'] = tosca_props['requirement']
55 if self._vnf_name is None:
56 self.properties['member-vnfd'] = []
57 index_count = 1
58 for node in self.policy.get_targets_list():
59 vnf_node = self.get_node_with_name(node.name, nodes)
60 prop = {}
61 prop['member-vnf-index-ref'] = index_count
62 prop['vnfd-id-ref'] = vnf_node.id
63 self.properties['member-vnfd'].append(prop)
64 index_count = index_count + 1
65 else:
66 self.properties['member-vdus'] = []
67 for node in self.policy.get_targets_list():
68 vdu_node = self.get_node_with_name(node.name, nodes)
69 prop = {}
70 prop['member-vdu-ref'] = vdu_node.name
71 self.properties['member-vdus'].append(prop)
72
73 def get_yang_model_gi(self, nsd, vnfds):
74 props = convert_keys_to_python(self.properties)
75 try:
76 if self._vnf_name is None:
77 nsd.placement_groups.add().from_dict(props)
78 except Exception as e:
79 err_msg = _("{0} Exception nsd placement-groups 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 if use_gi:
86 return self.get_yang_model_gi(nsd, vnfds)
87 if 'placement-groups' not in nsd:
88 nsd['placement-groups'] = []
89
90 for key, value in self.properties.items():
91 prim[key] = value
92 nsd['placement-groups'].append(prim)
93
94 def generate_yang_submodel_gi(self, vnfd):
95 if vnfd is None:
96 return None
97 try:
98 props = convert_keys_to_python(self.properties)
99 vnfd.placement_groups.add().from_dict(props)
100 except Exception as e:
101 err_msg = _("{0} Exception policy from dict {1}: {2}"). \
102 format(self, props, e)
103 self.log.error(err_msg)
104 raise e
105
106 def get_policy_props(self):
107 tosca_props = {}
108
109 for prop in self.policy.get_properties_objects():
110 if isinstance(prop.value, GetInput):
111 tosca_props[prop.name] = {'get_param': prop.value.input_name}
112 else:
113 tosca_props[prop.name] = prop.value
114 return tosca_props