From e09c25a70bdae340e913543cbdfb512bf148b8ff Mon Sep 17 00:00:00 2001 From: Sripriya Date: Thu, 22 Sep 2016 17:04:31 -0700 Subject: [PATCH] Fix respawn action for alarm monitor Modify the validation for VnfPolicyNotFound by breaking the logic in to two steps: 1. Verify if action is already part of DEFAULT_ACTIONS 2. If 1. fails, try to get the policy for action name defined in template. If 2 fails, only then raise an exception. also fix the vim_auth args sent to the monitoring policy execute_action method. Closes-Bug: #365435 Change-Id: I7923189575c44efcd4121b0158c270f801b7cf2b --- tacker/tests/unit/db/utils.py | 1 + .../data/test_tosca_vnfd_alarm_respawn.yaml | 57 +++++++++++++++++++ tacker/tests/unit/vm/test_plugin.py | 43 ++++++++++++++ tacker/vnfm/plugin.py | 19 ++++--- 4 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 tacker/tests/unit/vm/infra_drivers/openstack/data/test_tosca_vnfd_alarm_respawn.yaml diff --git a/tacker/tests/unit/db/utils.py b/tacker/tests/unit/db/utils.py index 74c335706..956f6fa22 100644 --- a/tacker/tests/unit/db/utils.py +++ b/tacker/tests/unit/db/utils.py @@ -38,6 +38,7 @@ vnffgd_tosca_template = yaml.load(_get_template('tosca_vnffgd_template.yaml')) vnffgd_invalid_tosca_template = yaml.load(_get_template( 'tosca_invalid_vnffgd_template.yaml')) vnfd_scale_tosca_template = _get_template('tosca_scale.yaml') +vnfd_alarm_tosca_template = _get_template('test_tosca_vnfd_alarm_respawn.yaml') def get_dummy_vnfd_obj(): diff --git a/tacker/tests/unit/vm/infra_drivers/openstack/data/test_tosca_vnfd_alarm_respawn.yaml b/tacker/tests/unit/vm/infra_drivers/openstack/data/test_tosca_vnfd_alarm_respawn.yaml new file mode 100644 index 000000000..4635700e4 --- /dev/null +++ b/tacker/tests/unit/vm/infra_drivers/openstack/data/test_tosca_vnfd_alarm_respawn.yaml @@ -0,0 +1,57 @@ +tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0 +description: Demo example + +metadata: + template_name: sample-tosca-vnfd + +topology_template: + node_templates: + VDU1: + type: tosca.nodes.nfv.VDU.Tacker + capabilities: + nfv_compute: + properties: + disk_size: 1 GB + mem_size: 512 MB + num_cpus: 2 + properties: + image: cirros-0.3.4-x86_64-uec + mgmt_driver: noop + availability_zone: nova + + 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: + - vdu1_cpu_usage_monitoring_policy: + type: tosca.policies.tacker.Alarming + triggers: + resize_compute: + event_type: + type: tosca.events.resource.utilization + implementation: ceilometer + metrics: cpu_util + condition: + threshold: 50 + constraint: utilization greater_than 50% + period: 600 + evaluations: 1 + method: avg + comparison_operator: gt + action: + resize_compute: + action_name: respawn diff --git a/tacker/tests/unit/vm/test_plugin.py b/tacker/tests/unit/vm/test_plugin.py index ec33552bf..eecbff2e2 100644 --- a/tacker/tests/unit/vm/test_plugin.py +++ b/tacker/tests/unit/vm/test_plugin.py @@ -16,6 +16,7 @@ import uuid import mock +from mock import patch import yaml from tacker.common import exceptions @@ -62,6 +63,7 @@ class TestVNFMPlugin(db_base.SqlTestCase): self._stub_get_vim() self._mock_device_manager() self._mock_vnf_monitor() + self._mock_vnf_alarm_monitor() self._mock_green_pool() self._insert_dummy_vim() self.vnfm_plugin = plugin.VNFMPlugin() @@ -108,6 +110,13 @@ class TestVNFMPlugin(db_base.SqlTestCase): self._mock( 'tacker.vnfm.monitor.VNFMonitor', fake_vnf_monitor) + def _mock_vnf_alarm_monitor(self): + self._vnf_alarm_monitor = mock.Mock(wraps=FakeVNFMonitor()) + fake_vnf_alarm_monitor = mock.Mock() + fake_vnf_alarm_monitor.return_value = self._vnf_alarm_monitor + self._mock( + 'tacker.vnfm.monitor.VNFAlarmMonitor', fake_vnf_alarm_monitor) + def _insert_dummy_device_template(self): session = self.context.session device_template = vnfm_db.VNFD( @@ -121,6 +130,17 @@ class TestVNFMPlugin(db_base.SqlTestCase): session.flush() return device_template + def _insert_dummy_vnfd_attributes(self, template): + session = self.context.session + vnfd_attr = vnfm_db.VNFDAttribute( + id='eb094833-995e-49f0-a047-dfb56aaf7c4e', + vnfd_id='eb094833-995e-49f0-a047-dfb56aaf7c4e', + key='vnfd', + value=template) + session.add(vnfd_attr) + session.flush() + return vnfd_attr + def _insert_dummy_device(self): session = self.context.session device_db = vnfm_db.VNF( @@ -365,3 +385,26 @@ class TestVNFMPlugin(db_base.SqlTestCase): def test_scale_vnf_in(self): self._test_scale_vnf('in', constants.PENDING_SCALE_IN) + + @patch('tacker.vnfm.monitor.ActionPolicy') + def test_create_vnf_trigger_respawn(self, mock_action_policy): + action_policy_cls = mock_action_policy.return_value + action_policy_cls.get_policy.return_value = mock.Mock() + vnf_id = "6261579e-d6f3-49ad-8bc3-a9cb974778fe" + trigger_request = {"trigger": {"action_name": "respawn", "params": { + "credential": "026kll6n", "data": {"current": "alarm", + 'alarm_id': + "b7fa9ffd-0a4f-4165-954b-5a8d0672a35f"}}, + "policy_name": "vdu1_cpu_usage_monitoring_policy"}} + self._insert_dummy_device_template() + self._insert_dummy_vnfd_attributes(utils.vnfd_alarm_tosca_template) + self._insert_dummy_device() + expected_result = {"action_name": "respawn", "params": { + "credential": "026kll6n", "data": {"current": "alarm", + "alarm_id": "b7fa9ffd-0a4f-4165-954b-5a8d0672a35f"}}, + "policy_name": "vdu1_cpu_usage_monitoring_policy"} + self._vnf_alarm_monitor.process_alarm_for_vnf.return_value = True + + trigger_result = self.vnfm_plugin.create_vnf_trigger( + self.context, vnf_id, trigger_request) + self.assertEqual(expected_result, trigger_result) diff --git a/tacker/vnfm/plugin.py b/tacker/vnfm/plugin.py index 0bd1e24f0..2cf4ecab3 100644 --- a/tacker/vnfm/plugin.py +++ b/tacker/vnfm/plugin.py @@ -730,12 +730,14 @@ class VNFMPlugin(vnfm_db.VNFMPluginDb, VNFMMgmtMixin): # validate policy action action = policy['action_name'] - policy_ = self.get_vnf_policy(context, action, vnf_id) - if not policy_ and action not in constants.DEFAULT_ALARM_ACTIONS: - raise exceptions.VnfPolicyNotFound( - vnf_id=action, - policy=policy['id'] - ) + policy_ = None + if action not in constants.DEFAULT_ALARM_ACTIONS: + policy_ = self.get_vnf_policy(context, action, vnf_id) + if not policy_: + raise exceptions.VnfPolicyNotFound( + vnf_id=action, + policy=policy['id'] + ) LOG.debug(_("Policy %s is validated successfully") % policy) return policy_ # validate url @@ -751,7 +753,7 @@ class VNFMPlugin(vnfm_db.VNFMPluginDb, VNFMMgmtMixin): if action_cls: action_cls.execute_action(self, vnf_dict) - if policy['bckend_policy']: + if policy.get('bckend_policy'): bckend_policy = policy['bckend_policy'] bckend_policy_type = bckend_policy['type'] cp = policy['properties']['resize_compute']['condition'].\ @@ -781,7 +783,8 @@ class VNFMPlugin(vnfm_db.VNFMPluginDb, VNFMMgmtMixin): policy_.update({'action_name': trigger['trigger']['action_name']}) policy_.update({'params': trigger['trigger']['params']}) bk_policy = self._validate_alarming_policy(context, policy_) - policy_.update({'bckend_policy': bk_policy}) + if bk_policy: + policy_.update({'bckend_policy': bk_policy}) self._handle_vnf_monitoring(context, policy_) return trigger['trigger']