currently QosPolicyInUse check for delete_policy is in policy.delete().
it should be done before notifying the backends.
eg. test_delete_not_allowed_if_policy_in_use_by_network tempest test triggers the situation.
the following was seen on networking-midonet gate. while the first delete_qos_policy failed
from POV of neutron api, it deleted the policy in the backend. and the second delete_qos_policy
ends up with the following error.
http://logs.openstack.org/27/400627/12/check/gate-tempest-dsvm-networking-midonet-v2-ubuntu-xenial/c4dad99/logs/screen-q-svc.txt.gz
2016-12-01 05:44:49.116 23924 ERROR midonetclient.api_lib [req-91b72dde-2942-44c5-bb12-1bc479b9916c tempest-QosTestJSON-1358267085 -] Got http error(response={'status': '404', 'content-length': '92', 'access-control-expose-headers': 'Location', 'vary': 'Accept-Encoding', 'server': 'Jetty(9.3.z-SNAPSHOT)', '-content-encoding': 'gzip', 'date': 'Thu, 01 Dec 2016 05:44:49 GMT', 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS', 'content-type': 'application/vnd.org.midonet.Error-v1+json'}, content='{"message":"There is no QosPolicy with ID 2474a2ca-9629-4c51-ae2f-18518a4aaaf0.","code":404}') for request(uri=u'http://10.12.148.148:8181/midonet-api/neutron/networks/114f5da9-9bae-4fd7-8095-44fad0d18da0', method='PUT', body={'status': u'ACTIVE', 'subnets': [], 'name': u'test network', 'admin_state_up': True, 'tenant_id': u'a030938a0e554f4fa31dec5651446fbf', 'description': u'', 'tags': [], 'updated_at': '2016-12-01T05:44:49Z', 'mtu': 1500, 'router:external': False, 'provider:network_type': 'midonet', 'revision_number': 4, 'port_security_enabled': True, 'shared': True, 'created_at': '2016-12-01T05:44:48Z', 'project_id': u'a030938a0e554f4fa31dec5651446fbf', 'id': u'114f5da9-9bae-4fd7-8095-44fad0d18da0', 'qos_policy_id': u'2474a2ca-9629-4c51-ae2f-18518a4aaaf0'}, query={},headers={'Content-Type': 'application/vnd.org.midonet.neutron.Network-v1+json', 'X-Auth-Token': 'gAAAAABYP7Y30jWsOPThAVKsDC8uc7JzrXDhiE5Qem4JMuz074sMgOLV7n6azX3LYdGKt0kB3-j4r74fExTuzo1oCQgRG1pv763rSgTD5nrpOnvphnr9ew1cJpsHjofVWOitV8Y3lwTFxaKAiK2ovKa3ifH5tLOJm7v_-uXaRaTd8vYb5n0QCf0'}). Raising exception=<HTTPNotFound at 0x7ff8dd456e60 404 Not Found>
Yeah, it seems like the order of operations in delete_policy is wrong (we first notify, then delete in db, while we should do vice versa).