diff --git a/vmware_nsx/services/qos/nsx_v3/utils.py b/vmware_nsx/services/qos/nsx_v3/utils.py index 9124e3d4a6..a7316d96de 100644 --- a/vmware_nsx/services/qos/nsx_v3/utils.py +++ b/vmware_nsx/services/qos/nsx_v3/utils.py @@ -115,10 +115,32 @@ class QosNotificationsHandler(object): policy_id, profile_id) + def _find_policy_profile_id(self, policy_id): + # Note: In Ocata the policy was already deleted from the DB and from + # the mapping table, so as a temporary fix we have to search the + # backend profiles by the id. + # In Pike the DM model will be different so the mapping entry will not + # be deleted. + all_profiles = self._nsxlib_qos.list()['results'] + for profile in all_profiles: + # look only at qos profiles + if profile.get('resource_type') == 'QosSwitchingProfile': + tags = profile.get('tags', []) + for tag in tags: + if (tag.get('scope') == 'os-neutron-qos-id' and + tag.get('tag') == policy_id): + return profile.get('id') + def delete_policy(self, context, policy_id): - profile_id = nsx_db.get_switch_profile_by_qos_policy( - context.session, policy_id) - self._nsxlib_qos.delete(profile_id) + try: + profile_id = self._find_policy_profile_id(policy_id) + if profile_id: + self._nsxlib_qos.delete(profile_id) + else: + LOG.warning(_LW("QOS delete_policy failed: Policy %w was not " + "found on backend"), policy_id) + except Exception as e: + LOG.warning(_LW("QOS delete_policy failed: %s"), e) def update_policy(self, context, policy_id, policy): profile_id = nsx_db.get_switch_profile_by_qos_policy( diff --git a/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py b/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py index e9e823fe35..d73e80b69a 100644 --- a/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py +++ b/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py @@ -316,12 +316,19 @@ class TestQosNsxV3Notification(base.BaseQosTestCase, qos_marking='trusted' ) - @mock.patch('neutron.objects.db.api.get_object', return_value=None) - def test_policy_delete_profile(self, *mocks): + def test_policy_delete_profile(self): # test the switch profile deletion when a QoS policy is deleted + backend_profile = { + 'id': self.fake_profile_id, + 'resource_type': 'QosSwitchingProfile', + 'tags': [{'scope': 'os-neutron-qos-id', 'tag': self.policy.id}]} with mock.patch( 'vmware_nsxlib.v3.NsxLibQosSwitchingProfile.delete', return_value=self.fake_profile - ) as delete_profile: + ) as delete_profile,\ + mock.patch( + 'vmware_nsxlib.v3.NsxLibQosSwitchingProfile.list', + return_value={'results': [backend_profile]} + ): self.qos_plugin.delete_policy(self.ctxt, self.policy.id) delete_profile.assert_called_once_with(self.fake_profile_id)