diff --git a/translator/hot/tosca/tosca_policies.py b/translator/hot/tosca/tosca_policies.py index 12b40d50..19f121a1 100755 --- a/translator/hot/tosca/tosca_policies.py +++ b/translator/hot/tosca/tosca_policies.py @@ -18,7 +18,7 @@ TARGET_CLASS_NAME = 'ToscaPolicies' class ToscaPolicies(HotResource): - '''Translate TOSCA policy type tosca.poicies.Placement.''' + '''Translate TOSCA policy type tosca.policies.Placement.''' toscatype = 'tosca.policies.Placement' @@ -29,8 +29,13 @@ class ToscaPolicies(HotResource): self.policy = policy def handle_properties(self, resources): + group_policy = "%(soft-prefix)s%(policy)s" % { + "soft-prefix": "soft-" if not self.get_tosca_props().get( + "strict", True) else "", + "policy": self.get_tosca_props().get("policy", "affinity"), + } self.properties["name"] = self.name - self.properties["policies"] = ["affinity"] + self.properties["policies"] = [group_policy] for resource in resources: if resource.name in self.policy.targets: resource.properties["scheduler_hints"] = { diff --git a/translator/hot/translate_node_templates.py b/translator/hot/translate_node_templates.py index 27a19c62..ac9837f6 100644 --- a/translator/hot/translate_node_templates.py +++ b/translator/hot/translate_node_templates.py @@ -273,6 +273,9 @@ class TranslateNodeTemplates(object): if policy.is_derived_from('tosca.policies.Monitoring'): TOSCA_TO_HOT_TYPE[policy_type.type] = \ TOSCA_TO_HOT_TYPE['tosca.policies.Monitoring'] + if policy.is_derived_from('tosca.policies.Placement'): + TOSCA_TO_HOT_TYPE[policy_type.type] = \ + TOSCA_TO_HOT_TYPE['tosca.policies.Placement'] if not policy.is_derived_from('tosca.policies.Monitoring') and \ not policy.is_derived_from('tosca.policies.Scaling') and \ policy_type.type not in TOSCA_TO_HOT_TYPE: diff --git a/translator/tests/data/hot_output/nfv/hot_policy_anti_affinity.yaml b/translator/tests/data/hot_output/nfv/hot_policy_anti_affinity.yaml new file mode 100644 index 00000000..6659e4b0 --- /dev/null +++ b/translator/tests/data/hot_output/nfv/hot_policy_anti_affinity.yaml @@ -0,0 +1,26 @@ +heat_template_version: 2013-05-23 + +description: > + Template for deploying the nodes based on anti-affinity + placement policy. + +parameters: {} + +resources: + my_server: + type: OS::Nova::Server + properties: + flavor: m1.medium + image: rhel-6.5-test-image + scheduler_hints: + group: + get_resource: my_compute_placement_policy + user_data_format: SOFTWARE_CONFIG + my_compute_placement_policy: + type: OS::Nova::ServerGroup + properties: + name: my_compute_placement_policy + policies: + - anti-affinity + +outputs: {} diff --git a/translator/tests/data/hot_output/nfv/hot_policy_soft_affinity.yaml b/translator/tests/data/hot_output/nfv/hot_policy_soft_affinity.yaml new file mode 100644 index 00000000..b50db12b --- /dev/null +++ b/translator/tests/data/hot_output/nfv/hot_policy_soft_affinity.yaml @@ -0,0 +1,26 @@ +heat_template_version: 2013-05-23 + +description: > + Template for deploying the nodes based on soft-affinity + placement policy. + +parameters: {} + +resources: + my_server: + type: OS::Nova::Server + properties: + flavor: m1.medium + image: rhel-6.5-test-image + scheduler_hints: + group: + get_resource: my_compute_placement_policy + user_data_format: SOFTWARE_CONFIG + my_compute_placement_policy: + type: OS::Nova::ServerGroup + properties: + name: my_compute_placement_policy + policies: + - soft-affinity + +outputs: {} diff --git a/translator/tests/data/nfv/tacker_defs.yaml b/translator/tests/data/nfv/tacker_defs.yaml index 96b0d458..f5fac057 100644 --- a/translator/tests/data/nfv/tacker_defs.yaml +++ b/translator/tests/data/nfv/tacker_defs.yaml @@ -76,7 +76,21 @@ data_types: policy_types: tosca.policies.tacker.Placement: - derived_from: tosca.policies.Root + derived_from: tosca.policies.Placement + description: Defines placement policy for VNF's. + properties: + policy: + type: string + required: false + default: affinity + description: Placement policy for target VDU's. + strict: + type: boolean + required: false + default: true + description: > + If the policy is not strict, it is allowed to continue even if the + nova-scheduler fails to assign hosts under the policy. tosca.policies.tacker.Failure: derived_from: tosca.policies.Root diff --git a/translator/tests/data/nfv/tosca_placement_policy_affinity.yaml b/translator/tests/data/nfv/tosca_placement_policy_affinity.yaml new file mode 100644 index 00000000..e67803ee --- /dev/null +++ b/translator/tests/data/nfv/tosca_placement_policy_affinity.yaml @@ -0,0 +1,35 @@ +tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0 + +description: > + Template for deploying the nodes based on given policies. + +imports: + - tacker_defs.yaml + - tacker_nfv_defs.yaml +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + policies: + - my_compute_placement_policy: + type: tosca.policies.tacker.Placement + properties: + policy: affinity + strict: true + description: Apply my placement policy to my application's servers + targets: [ my_server ] diff --git a/translator/tests/data/nfv/tosca_placement_policy_anti_affinity.yaml b/translator/tests/data/nfv/tosca_placement_policy_anti_affinity.yaml new file mode 100644 index 00000000..39c07516 --- /dev/null +++ b/translator/tests/data/nfv/tosca_placement_policy_anti_affinity.yaml @@ -0,0 +1,36 @@ +tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0 + +description: > + Template for deploying the nodes based on anti-affinity placement policy. + +imports: + - tacker_defs.yaml + - tacker_nfv_defs.yaml + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + policies: + - my_compute_placement_policy: + type: tosca.policies.tacker.Placement + properties: + policy: anti-affinity + strict: true + description: Apply my placement policy to my application’s servers + targets: [ my_server ] \ No newline at end of file diff --git a/translator/tests/data/nfv/tosca_placement_policy_soft_affinity.yaml b/translator/tests/data/nfv/tosca_placement_policy_soft_affinity.yaml new file mode 100644 index 00000000..69026dbc --- /dev/null +++ b/translator/tests/data/nfv/tosca_placement_policy_soft_affinity.yaml @@ -0,0 +1,35 @@ +tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0 + +description: > + Template for deploying the nodes based on soft-affinity placement policy. + +imports: + - tacker_defs.yaml + - tacker_nfv_defs.yaml +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + policies: + - my_compute_placement_policy: + type: tosca.policies.tacker.Placement + properties: + policy: affinity + strict: false + description: Apply my placement policy to my application's servers + targets: [ my_server ] diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index f67c86c6..1357a10d 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -486,12 +486,35 @@ class ToscaHotTranslationTest(TestCase): params = {} self._test_successful_translation(tosca_file, hot_file, params) - def test_hot_translate_policy(self): + def test_hot_translate_placement_policy_default_affinity(self): tosca_file = '../tests/data/policies/tosca_policies.yaml' hot_file = '../tests/data/hot_output/policies/hot_policies.yaml' params = {} self._test_successful_translation(tosca_file, hot_file, params) + def test_hot_translate_placement_policy_affinity(self): + tosca_file = ('../tests/data/nfv/' + 'tosca_placement_policy_affinity.yaml') + hot_file = '../tests/data/hot_output/policies/hot_policies.yaml' + params = {} + self._test_successful_translation(tosca_file, hot_file, params) + + def test_hot_translate_placement_policy_anti_affinity(self): + tosca_file = ('../tests/data/nfv/' + 'tosca_placement_policy_anti_affinity.yaml') + hot_file = ('../tests/data/hot_output/nfv/' + 'hot_policy_anti_affinity.yaml') + params = {} + self._test_successful_translation(tosca_file, hot_file, params) + + def test_hot_translate_placement_policy_soft_affinity(self): + tosca_file = ('../tests/data/nfv/' + 'tosca_placement_policy_soft_affinity.yaml') + hot_file = ('../tests/data/hot_output/nfv/' + 'hot_policy_soft_affinity.yaml') + params = {} + self._test_successful_translation(tosca_file, hot_file, params) + def test_hot_script_types(self): tosca_file = '../tests/data/interfaces/test_tosca_script_types.yaml' hot_file = '../tests/data/hot_output/hot_script_types.yaml'