25246af05bba49403e05093456b527ff1b7abcb3
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_scaling_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
22 from toscaparser.common.exception import ValidationError
23
24
25 # Name used to dynamically load appropriate map class.
26 TARGET_CLASS_NAME = 'ToscaScalingGroup'
27
28
29 class ToscaScalingGroup(ManoResource):
30 '''Translate TOSCA node type tosca.policies.Scaling.'''
31
32 toscatype = 'tosca.policies.riftio.ScalingGroup'
33
34 IGNORE_PROPS = []
35
36 def __init__(self, log, policy, 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 policy.items():
41 self.name = name
42 self.details = details
43 break
44 self.type_ = 'scale-grp'
45 self.metadata = metadata
46 self.properties = {}
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.details
53 self.log.debug(_("{0} with tosca properties: {1}").
54 format(self, tosca_props))
55 self.properties['name'] = tosca_props['name']
56 self.properties['max-instance-count'] = \
57 tosca_props['max_instance_count']
58 self.properties['min-instance-count'] = \
59 tosca_props['min_instance_count']
60 self.properties['vnfd-member'] = []
61
62 def _get_node(name):
63 for node in nodes:
64 if node.name == name:
65 return node
66
67 for member, count in tosca_props['vnfd_members'].items():
68 node = _get_node(member)
69 if node:
70 memb = {}
71 memb['member-vnf-index-ref'] = node.get_member_vnf_index()
72 memb['count'] = count
73 self.properties['vnfd-member'].append(memb)
74 else:
75 err_msg = _("{0}: Did not find the member node {1} in "
76 "resources list"). \
77 format(self, member)
78 self.log.error(err_msg)
79 raise ValidationError(message=err_msg)
80
81 def _validate_action(action):
82 for group in groups:
83 if group.validate_primitive(action):
84 return True
85 return False
86
87 self.properties['scaling-config-action'] = []
88 for action, value in tosca_props['config_actions'].items():
89 conf = {}
90 if _validate_action(value):
91 conf['trigger'] = action
92 conf['ns-config-primitive-name-ref'] = value
93 self.properties['scaling-config-action'].append(conf)
94 else:
95 err_msg = _("{0}: Did not find the action {1} in "
96 "config primitives"). \
97 format(self, action)
98 self.log.error(err_msg)
99 raise ValidationError(message=err_msg)
100
101 self.log.debug(_("{0} properties: {1}").format(self, self.properties))
102
103 def get_yang_model_gi(self, nsd, vnfds):
104 props = convert_keys_to_python(self.properties)
105 try:
106 nsd.scaling_group_descriptor.add().from_dict(props)
107 except Exception as e:
108 err_msg = _("{0} Exception nsd scaling group from dict {1}: {2}"). \
109 format(self, props, e)
110 self.log.error(err_msg)
111 raise e
112
113 def generate_yang_model(self, nsd, vnfds, use_gi=False):
114 """Generate yang model for the node"""
115 self.log.debug(_("Generate YANG model for {0}").
116 format(self))
117
118 for key in ToscaScalingGroup.IGNORE_PROPS:
119 if key in self.properties:
120 self.properties.pop(key)
121
122 if use_gi:
123 return self.get_yang_model_gi(nsd, vnfds)
124
125 if 'scaling-group-descriptor' not in nsd:
126 nsd['scaling-group-descriptor'] = []
127 scale = {}
128 for key, value in self.properties.items():
129 scale[key] = value
130 nsd['scaling-group-descriptor'].append(scale)