update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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 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 = 'ToscaScalingGroup'
28
29
30 class ToscaScalingGroup(ManoResource):
31 '''Translate TOSCA node type tosca.policies.Scaling.'''
32
33 toscatype = 'tosca.groups.nfv.riftio.scaling'
34
35 IGNORE_PROPS = []
36
37 def __init__(self, log, group, metadata=None):
38 # TODO(Philip):Not inheriting for ManoResource, as there is no
39 # instance from parser
40 self.log = log
41 self.name = group.name
42 #self.details = details
43 self.group = group
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 def get_tosca_group_props(self):
51 tosca_props = {}
52 for prop in self.group.get_properties_objects():
53 if isinstance(prop.value, GetInput):
54 tosca_props[prop.name] = {'get_param': prop.value.input_name}
55 else:
56 tosca_props[prop.name] = prop.value
57 return tosca_props
58
59 def handle_properties(self, nodes, groups):
60 tosca_props = self.get_tosca_group_props()
61 self.log.debug(_("{0} with tosca properties: {1}").
62 format(self, tosca_props))
63 if 'name ' in tosca_props:
64 self.properties['name'] = tosca_props['name']
65 if 'max_instance_count' in tosca_props:
66 self.properties['max-instance-count'] = tosca_props['max_instance_count']
67 if 'min_instance_count' in tosca_props:
68 self.properties['min-instance-count'] = tosca_props['min_instance_count']
69 self.properties['vnfd-member'] = []
70
71 def _get_node(name):
72 for node in nodes:
73 if node.name == name:
74 return node
75 if 'vnfd_members' in tosca_props:
76 for member, count in tosca_props['vnfd_members'].items():
77 node = _get_node(member)
78 if node:
79 memb = {}
80 memb['member-vnf-index-ref'] = node.get_member_vnf_index()
81 memb['count'] = count
82 self.properties['vnfd-member'].append(memb)
83 else:
84 err_msg = _("{0}: Did not find the member node {1} in "
85 "resources list"). \
86 format(self, member)
87 self.log.error(err_msg)
88 raise ValidationError(message=err_msg)
89
90 def _validate_action(action):
91 for group in groups:
92 if group.validate_primitive(action):
93 return True
94 return False
95
96 self.properties['scaling-config-action'] = []
97 if 'config_actions' in tosca_props:
98 for action, value in tosca_props['config_actions'].items():
99 conf = {}
100 if _validate_action(value):
101 conf['trigger'] = action
102 conf['ns-service-primitive-name-ref'] = value
103 self.properties['scaling-config-action'].append(conf)
104 else:
105 err_msg = _("{0}: Did not find the action {1} in "
106 "config primitives"). \
107 format(self, action)
108 self.log.error(err_msg)
109 raise ValidationError(message=err_msg)
110
111 self.log.debug(_("{0} properties: {1}").format(self, self.properties))
112
113 def get_yang_model_gi(self, nsd, vnfds):
114 props = convert_keys_to_python(self.properties)
115 try:
116 if len(self.properties['vnfd-member']) > 0:
117 nsd.scaling_group_descriptor.add().from_dict(props)
118 except Exception as e:
119 err_msg = _("{0} Exception nsd scaling group from dict {1}: {2}"). \
120 format(self, props, e)
121 self.log.error(err_msg)
122 raise e
123
124 def generate_yang_model(self, nsd, vnfds, use_gi=False):
125 """Generate yang model for the node"""
126 self.log.debug(_("Generate YANG model for {0}").
127 format(self))
128
129 for key in ToscaScalingGroup.IGNORE_PROPS:
130 if key in self.properties:
131 self.properties.pop(key)
132
133 if use_gi:
134 return self.get_yang_model_gi(nsd, vnfds)
135
136 if 'scaling-group-descriptor' not in nsd:
137 nsd['scaling-group-descriptor'] = []
138 scale = {}
139 for key, value in self.properties.items():
140 scale[key] = value
141 nsd['scaling-group-descriptor'].append(scale)