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
22 from toscaparser
.common
.exception
import ValidationError
25 # Name used to dynamically load appropriate map class.
26 TARGET_CLASS_NAME
= 'ToscaScalingGroup'
29 class ToscaScalingGroup(ManoResource
):
30 '''Translate TOSCA node type tosca.policies.Scaling.'''
32 toscatype
= 'tosca.policies.riftio.ScalingGroup'
36 def __init__(self
, log
, policy
, metadata
=None):
37 # TODO(Philip):Not inheriting for ManoResource, as there is no
38 # instance from parser
40 for name
, details
in policy
.items():
42 self
.details
= details
44 self
.type_
= 'scale-grp'
45 self
.metadata
= metadata
49 return "%s(%s)" % (self
.name
, self
.type)
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'] = []
67 for member
, count
in tosca_props
['vnfd_members'].items():
68 node
= _get_node(member
)
71 memb
['member-vnf-index-ref'] = node
.get_member_vnf_index()
73 self
.properties
['vnfd-member'].append(memb
)
75 err_msg
= _("{0}: Did not find the member node {1} in "
78 self
.log
.error(err_msg
)
79 raise ValidationError(message
=err_msg
)
81 def _validate_action(action
):
83 if group
.validate_primitive(action
):
87 self
.properties
['scaling-config-action'] = []
88 for action
, value
in tosca_props
['config_actions'].items():
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
)
95 err_msg
= _("{0}: Did not find the action {1} in "
96 "config primitives"). \
98 self
.log
.error(err_msg
)
99 raise ValidationError(message
=err_msg
)
101 self
.log
.debug(_("{0} properties: {1}").format(self
, self
.properties
))
103 def get_yang_model_gi(self
, nsd
, vnfds
):
104 props
= convert_keys_to_python(self
.properties
)
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
)
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}").
118 for key
in ToscaScalingGroup
.IGNORE_PROPS
:
119 if key
in self
.properties
:
120 self
.properties
.pop(key
)
123 return self
.get_yang_model_gi(nsd
, vnfds
)
125 if 'scaling-group-descriptor' not in nsd
:
126 nsd
['scaling-group-descriptor'] = []
128 for key
, value
in self
.properties
.items():
130 nsd
['scaling-group-descriptor'].append(scale
)