Skip to content

Commit

Permalink
Create VNF with parameter file using scaling policy
Browse files Browse the repository at this point in the history
If user provides parameter file while creating VNF using scaling
policy, earlier it was failing with error 'The Parameter was not
provided" This patch allows creation of VNF using scaling policy
with parameter file.

APIImpact
Return 200 instead of 400 error when VNF is created using scaling
policy with parameter file

Change-Id: If102519127a3ef63449ab59f849e8cfaaa3ae62b
Closes-Bug: #1799683
  • Loading branch information
shubham potale committed Feb 26, 2019
1 parent 66ce5cf commit 68e06e2
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 6 deletions.
95 changes: 92 additions & 3 deletions translator/hot/tosca/tests/test_tosca_autoscaling.py
Expand Up @@ -10,17 +10,25 @@
# License for the specific language governing permissions and limitations
# under the License.

import datetime
import yaml

from toscaparser.nodetemplate import NodeTemplate
from toscaparser.policy import Policy
from toscaparser.tests.base import TestCase
import toscaparser.utils.yamlparser

from translator.hot.syntax.hot_parameter import HotParameter
from translator.hot.tosca.tosca_compute import ToscaCompute
from translator.hot.tosca.tosca_policies_scaling import ToscaAutoscaling

HOT_TEMPLATE_VERSION = '2013-05-23'


class AutoscalingTest(TestCase):

def _tosca_scaling_test(self, tpl_snippet, expectedprops):
def _tosca_scaling_test(self, tpl_snippet, expectedprops,
hot_template_parameters=None):
nodetemplates = (toscaparser.utils.yamlparser.
simple_parse(tpl_snippet)['node_templates'])
policies = (toscaparser.utils.yamlparser.
Expand All @@ -37,9 +45,18 @@ def _tosca_scaling_test(self, tpl_snippet, expectedprops):
toscacompute.handle_properties()
policy = Policy(policy_name, tpl, targets,
properties, "node_templates")
toscascaling = ToscaAutoscaling(policy)
toscascaling = ToscaAutoscaling(
policy, hot_template_parameters=hot_template_parameters)
parameters = toscascaling.handle_properties([toscacompute])
self.assertEqual(parameters[0].properties, expectedprops)
if hot_template_parameters:
substack_template = toscascaling.extract_substack_templates(
"output.yaml", HOT_TEMPLATE_VERSION)
actual_nested_resource = yaml.load(
substack_template['SP1_res.yaml'])
self.assertEqual(expectedprops,
actual_nested_resource)
else:
self.assertEqual(parameters[0].properties, expectedprops)
except Exception:
raise

Expand Down Expand Up @@ -93,3 +110,75 @@ def test_compute_with_scaling(self):
self._tosca_scaling_test(
tpl_snippet,
expectedprops)

def test_scaling_nested_template_with_params(self):
tpl_snippet = '''
node_templates:
VDU1:
type: tosca.nodes.Compute
properties:
image: { get_input: image_name }
flavor: { get_input: flavor }
mgmt_driver: noop
availability_zone: nova
metadata: {metering.server_group: SG1}
policies:
- SP1:
type: tosca.policies.Scaling
targets: [VDU1]
properties:
increment: 1
cooldown: 120
min_instances: 1
max_instances: 3
default_instances: 1
'''

expected_nested_resource = {'heat_template_version':
datetime.date(2013, 5, 23),
'description':
'Tacker Scaling template',
'parameters':
{'flavor':
{'default': 'm1.tiny',
'type': 'string',
'description':
'Flavor Information'
},
'image_name':
{'default':
'cirros-0.3.5-x86_64-disk',
'type': 'string',
'description': 'Image Name'
}
},
'resources':
{'VDU1':
{'type':
'OS::Nova::Server',
'properties':
{'flavor': None,
'user_data_format':
'SOFTWARE_CONFIG'
}
}
}
}

flavor = HotParameter('flavor', 'string',
label=None,
description='Flavor Information',
default='m1.tiny',
hidden=None,
constraints=[])
image = HotParameter('image_name', 'string',
label=None,
description='Image Name',
default='cirros-0.3.5-x86_64-disk',
hidden=None,
constraints=[])
hot_template_parameters = [flavor, image]
self._tosca_scaling_test(tpl_snippet,
expected_nested_resource,
hot_template_parameters)
11 changes: 9 additions & 2 deletions translator/hot/tosca/tosca_policies_scaling.py
Expand Up @@ -34,12 +34,13 @@ class ToscaAutoscaling(HotResource):

toscatype = 'tosca.policies.Scaling'

def __init__(self, policy, csar_dir=None):
def __init__(self, policy, csar_dir=None, hot_template_parameters=None):
hot_type = "OS::Heat::ScalingPolicy"
super(ToscaAutoscaling, self).__init__(policy,
type=hot_type,
csar_dir=csar_dir)
self.policy = policy
self.hot_template_parameters = hot_template_parameters

def handle_expansion(self):
if self.policy.entity_tpl.get('triggers'):
Expand Down Expand Up @@ -76,6 +77,12 @@ def represent_ordereddict(self, dumper, data):
def _handle_nested_template(self, scale_res):
template_dict = yaml.safe_load(HEAT_TEMPLATE_BASE)
template_dict['description'] = 'Tacker Scaling template'
if self.hot_template_parameters:
all_params = OrderedDict()
for parameter in self.hot_template_parameters:
all_params.update(parameter.get_dict_output())
template_dict.update({'parameters': all_params})

template_dict["resources"] = {}
dict_res = OrderedDict()
for res in scale_res:
Expand All @@ -87,7 +94,7 @@ def _handle_nested_template(self, scale_res):
yaml.add_representer(OrderedDict, self.represent_ordereddict)
yaml.add_representer(dict, self.represent_ordereddict)
yaml_string = yaml.dump(template_dict, default_flow_style=False)
yaml_string = yaml_string.replace('\'', '') .replace('\n\n', '\n')
yaml_string = yaml_string.replace('\'', '').replace('\n\n', '\n')
self.nested_template = {
self.policy.name + '_res.yaml': yaml_string
}
Expand Down
8 changes: 7 additions & 1 deletion translator/hot/translate_node_templates.py
Expand Up @@ -283,7 +283,13 @@ def _translate_nodetemplates(self):
raise UnsupportedTypeError(type=_('%s') % policy_type.type)
elif policy_type.type == 'tosca.policies.Scaling.Cluster':
self.hot_template_version = '2016-04-08'
policy_node = TOSCA_TO_HOT_TYPE[policy_type.type](policy)
if policy.is_derived_from('tosca.policies.Scaling') and \
policy_type.type != 'tosca.policies.Scaling.Cluster':
policy_node = TOSCA_TO_HOT_TYPE[policy_type.type](
policy,
hot_template_parameters=self.hot_template.parameters)
else:
policy_node = TOSCA_TO_HOT_TYPE[policy_type.type](policy)
self.hot_resources.append(policy_node)

# Handle life cycle operations: this may expand each node
Expand Down
29 changes: 29 additions & 0 deletions translator/tests/data/hot_output/nfv/SP_res.yaml
@@ -0,0 +1,29 @@
heat_template_version: 2013-05-23
description: Tacker Scaling template

parameters:
flavor: {type: string, default: m1.tiny, description: Flavor Information}
image_name: {type: string, default: cirros-0.3.5-x86_64-disk, description: Image Name}

resources:
VDU1:
type: OS::Nova::Server
properties:
user_data_format: SOFTWARE_CONFIG
availability_zone: nova
flavor: {get_param: flavor}
user_data_format: SOFTWARE_CONFIG
image: {get_param: image_name}
config_drive: False
networks:
- port: { get_resource: CP1 }
metadata:
metering.server_group: SG1
CP1:
type: OS::Neutron::Port
properties:
anti_spoofing_protection: False
management: True
network: net_mgmt
VL1:
type: OS::Neutron::Net
@@ -0,0 +1,66 @@
heat_template_version: 2013-05-23

description: >
Demo example
parameters:
flavor:
default: m1.tiny
type: string
description: Flavor Information

image_name:
default: cirros-0.3.5-x86_64-disk
type: string
description: Image Name

resources:
SP_group:
type: OS::Heat::AutoScalingGroup
properties:
min_size: 1
desired_capacity: 1
cooldown: 120
resource:
type: SP_res.yaml
max_size: 3
SP_scale_out:
type: OS::Heat::ScalingPolicy
properties:
auto_scaling_group_id:
get_resource: SP_group
adjustment_type: change_in_capacity
scaling_adjustment: 1
cooldown: 120
SP_scale_in:
type: OS::Heat::ScalingPolicy
properties:
auto_scaling_group_id:
get_resource: SP_group
adjustment_type: change_in_capacity
scaling_adjustment: -1
cooldown: 120
vdu_hcpu_usage_scaling_out:
type: OS::Aodh::GnocchiAggregationByResourcesAlarm
properties:
metric: cpu_util
description: utilization greater_than 80%
evaluation_periods: 1
granularity: 60
aggregation_method: mean
threshold: 80
resource_type: instance
comparison_operator: gt
vdu_lcpu_usage_scaling_in:
type: OS::Aodh::GnocchiAggregationByResourcesAlarm
properties:
metric: cpu_util
description: utilization less_than 10%
evaluation_periods: 1
granularity: 60
aggregation_method: mean
threshold: 10
resource_type: instance
comparison_operator: lt

outputs: {}
4 changes: 4 additions & 0 deletions translator/tests/data/hot_output/reservation/SP_RSV_res.yaml
Expand Up @@ -2,6 +2,10 @@ heat_template_version: 2013-05-23

description: Tacker Scaling template

parameters:
flavor: {type: string, description: Flavor Information}
lease_id: {type: string, description: lease id}

resources:
VDU1:
type: OS::Nova::Server
Expand Down
@@ -0,0 +1,94 @@
tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0

description: Demo example

imports:
- tacker_defs.yaml
- tacker_nfv_defs.yaml

metadata:
template_name: sample-tosca-vnfd

topology_template:
inputs:
image_name:
type: string
description: Image Name

flavor:
type: string
description: Flavor Information

node_templates:
VDU1:
type: tosca.nodes.nfv.VDU.Tacker
properties:
image: { get_input: image_name }
flavor: { get_input: flavor }
mgmt_driver: noop
availability_zone: nova
metadata: {metering.server_group: SG1}

CP1:
type: tosca.nodes.nfv.CP.Tacker
properties:
management: true
anti_spoofing_protection: false
requirements:
- virtualLink:
node: VL1
- virtualBinding:
node: VDU1

VL1:
type: tosca.nodes.nfv.VL
properties:
network_name: net_mgmt
vendor: Tacker

policies:
- SP:
type: tosca.policies.tacker.Scaling
targets: [VDU1]
properties:
increment: 1
cooldown: 120
min_instances: 1
max_instances: 3
default_instances: 1

- vdu_cpu_usage_monitoring_policy:
type: tosca.policies.tacker.Alarming
triggers:
vdu_hcpu_usage_scaling_out:
event_type:
type: tosca.events.resource.utilization
implementation: ceilometer
metric: cpu_util
condition:
threshold: 80
constraint: utilization greater_than 80%
granularity: 60
evaluations: 1
aggregation_method: mean
resource_type: instance
comparison_operator: gt
metadata: SG1
action: [SP]

vdu_lcpu_usage_scaling_in:
event_type:
type: tosca.events.resource.utilization
implementation: ceilometer
metric: cpu_util
condition:
threshold: 10
constraint: utilization less_than 10%
granularity: 60
evaluations: 1
aggregation_method: mean
resource_type: instance
comparison_operator: lt
metadata: SG1
action: [SP]

12 changes: 12 additions & 0 deletions translator/tests/test_tosca_hot_translation.py
Expand Up @@ -593,6 +593,18 @@ def test_hot_translate_nfv_scaling(self):
params = {}
self._test_successful_translation(tosca_file, hot_files, params)

def test_hot_translate_nfv_scaling_with_params(self):
tosca_file = '../tests/data/nfv/test_tosca_nfv_autoscaling_with_' \
'params.yaml'
hot_files = [
'../tests/data/hot_output/nfv/hot_tosca_nfv_autoscaling_with_'
'param.yaml',
'../tests/data/hot_output/nfv/SP_res.yaml',
]
params = {'image_name': 'cirros-0.3.5-x86_64-disk',
'flavor': 'm1.tiny'}
self._test_successful_translation(tosca_file, hot_files, params)

def test_hot_translate_mon_scaling_policy(self):
tosca_file = '../tests/data/monitoring/tosca_monitoring_scaling.yaml'
hot_files = [
Expand Down

0 comments on commit 68e06e2

Please sign in to comment.