2 # Copyright 2016 RIFT.io Inc
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 from toscaparser
.common
.exception
import ValidationError
26 # Name used to dynamically load appropriate map class.
27 TARGET_CLASS_NAME
= 'ToscaScalingGroup'
30 class ToscaScalingGroup(ManoResource
):
31 '''Translate TOSCA node type tosca.policies.Scaling.'''
33 toscatype
= 'tosca.groups.nfv.riftio.scaling'
37 def __init__(self
, log
, group
, metadata
=None):
38 # TODO(Philip):Not inheriting for ManoResource, as there is no
39 # instance from parser
41 self
.name
= group
.name
42 #self.details = details
44 self
.type_
= 'scale-grp'
45 self
.metadata
= metadata
49 return "%s(%s)" % (self
.name
, self
.type)
50 def get_tosca_group_props(self
):
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
}
56 tosca_props
[prop
.name
] = prop
.value
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'] = []
75 if 'vnfd_members' in tosca_props
:
76 for member
, count
in tosca_props
['vnfd_members'].items():
77 node
= _get_node(member
)
80 memb
['member-vnf-index-ref'] = node
.get_member_vnf_index()
82 self
.properties
['vnfd-member'].append(memb
)
84 err_msg
= _("{0}: Did not find the member node {1} in "
87 self
.log
.error(err_msg
)
88 raise ValidationError(message
=err_msg
)
90 def _validate_action(action
):
92 if group
.validate_primitive(action
):
96 self
.properties
['scaling-config-action'] = []
97 if 'config_actions' in tosca_props
:
98 for action
, value
in tosca_props
['config_actions'].items():
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
)
105 err_msg
= _("{0}: Did not find the action {1} in "
106 "config primitives"). \
108 self
.log
.error(err_msg
)
109 raise ValidationError(message
=err_msg
)
111 self
.log
.debug(_("{0} properties: {1}").format(self
, self
.properties
))
113 def get_yang_model_gi(self
, nsd
, vnfds
):
114 props
= convert_keys_to_python(self
.properties
)
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
)
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}").
129 for key
in ToscaScalingGroup
.IGNORE_PROPS
:
130 if key
in self
.properties
:
131 self
.properties
.pop(key
)
134 return self
.get_yang_model_gi(nsd
, vnfds
)
136 if 'scaling-group-descriptor' not in nsd
:
137 nsd
['scaling-group-descriptor'] = []
139 for key
, value
in self
.properties
.items():
141 nsd
['scaling-group-descriptor'].append(scale
)