OpenStack Networking (Neutron)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
11KB

  1. # Copyright (c) 2015 Red Hat Inc.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from neutron.common import exceptions as n_exc
  16. from neutron.db import api as db_api
  17. from neutron.db import db_base_plugin_common
  18. from neutron.extensions import qos
  19. from neutron.objects.qos import policy as policy_object
  20. from neutron.objects.qos import rule_type as rule_type_object
  21. from neutron.services.qos.notification_drivers import manager as driver_mgr
  22. from neutron.services.qos import qos_consts
  23. class QoSPlugin(qos.QoSPluginBase):
  24. """Implementation of the Neutron QoS Service Plugin.
  25. This class implements a Quality of Service plugin that provides quality of
  26. service parameters over ports and networks.
  27. """
  28. supported_extension_aliases = ['qos']
  29. def __init__(self):
  30. super(QoSPlugin, self).__init__()
  31. self.notification_driver_manager = (
  32. driver_mgr.QosServiceNotificationDriverManager())
  33. @db_base_plugin_common.convert_result_to_dict
  34. def create_policy(self, context, policy):
  35. """Create a QoS policy.
  36. :param context: neutron api request context
  37. :type context: neutron.context.Context
  38. :param policy: policy data to be applied
  39. :type policy: dict
  40. :returns: a QosPolicy object
  41. """
  42. policy_obj = policy_object.QosPolicy(context, **policy['policy'])
  43. policy_obj.create()
  44. self.notification_driver_manager.create_policy(context, policy_obj)
  45. return policy_obj
  46. @db_base_plugin_common.convert_result_to_dict
  47. def update_policy(self, context, policy_id, policy):
  48. """Update a QoS policy.
  49. :param context: neutron api request context
  50. :type context: neutron.context.Context
  51. :param policy_id: the id of the QosPolicy to update
  52. :param policy_id: str uuid
  53. :param policy: new policy data to be applied
  54. :type policy: dict
  55. :returns: a QosPolicy object
  56. """
  57. policy_data = policy['policy']
  58. policy_obj = policy_object.QosPolicy(context, id=policy_id)
  59. policy_obj.update_nonidentifying_fields(policy_data,
  60. reset_changes=True)
  61. policy_obj.update()
  62. self.notification_driver_manager.update_policy(context, policy_obj)
  63. return policy_obj
  64. def delete_policy(self, context, policy_id):
  65. """Delete a QoS policy.
  66. :param context: neutron api request context
  67. :type context: neutron.context.Context
  68. :param policy_id: the id of the QosPolicy to delete
  69. :type policy_id: str uuid
  70. :returns: None
  71. """
  72. policy = policy_object.QosPolicy(context)
  73. policy.id = policy_id
  74. self.notification_driver_manager.delete_policy(context, policy)
  75. policy.delete()
  76. def _get_policy_obj(self, context, policy_id):
  77. """Fetch a QoS policy.
  78. :param context: neutron api request context
  79. :type context: neutron.context.Context
  80. :param policy_id: the id of the QosPolicy to fetch
  81. :type policy_id: str uuid
  82. :returns: a QosPolicy object
  83. :raises: n_exc.QosPolicyNotFound
  84. """
  85. obj = policy_object.QosPolicy.get_object(context, id=policy_id)
  86. if obj is None:
  87. raise n_exc.QosPolicyNotFound(policy_id=policy_id)
  88. return obj
  89. @db_base_plugin_common.filter_fields
  90. @db_base_plugin_common.convert_result_to_dict
  91. def get_policy(self, context, policy_id, fields=None):
  92. """Get a QoS policy.
  93. :param context: neutron api request context
  94. :type context: neutron.context.Context
  95. :param policy_id: the id of the QosPolicy to update
  96. :type policy_id: str uuid
  97. :returns: a QosPolicy object
  98. """
  99. return self._get_policy_obj(context, policy_id)
  100. @db_base_plugin_common.filter_fields
  101. @db_base_plugin_common.convert_result_to_dict
  102. def get_policies(self, context, filters=None, fields=None, sorts=None,
  103. limit=None, marker=None, page_reverse=False):
  104. """Get QoS policies.
  105. :param context: neutron api request context
  106. :type context: neutron.context.Context
  107. :param filters: search criteria
  108. :type filters: dict
  109. :returns: QosPolicy objects meeting the search criteria
  110. """
  111. return policy_object.QosPolicy.get_objects(context, **filters)
  112. @db_base_plugin_common.filter_fields
  113. @db_base_plugin_common.convert_result_to_dict
  114. def get_rule_types(self, context, filters=None, fields=None,
  115. sorts=None, limit=None,
  116. marker=None, page_reverse=False):
  117. if not filters:
  118. filters = {}
  119. return rule_type_object.QosRuleType.get_objects(**filters)
  120. @db_base_plugin_common.convert_result_to_dict
  121. def create_policy_rule(self, context, rule_obj, policy_id, rule_data):
  122. """Create a QoS policy rule.
  123. :param context: neutron api request context
  124. :type context: neutron.context.Context
  125. :param rule_obj: the rule object
  126. :type rule_obj: a class from the rule_object (qos.objects.rule) module
  127. :param policy_id: the id of the QosPolicy for which to create the rule
  128. :type policy_id: str uuid
  129. :param rule_data: the rule data to be applied
  130. :type rule_data: dict
  131. :returns: a QoS policy rule object
  132. """
  133. rule_type = rule_obj.rule_type
  134. rule_data = rule_data[rule_type + '_rule']
  135. with db_api.autonested_transaction(context.session):
  136. # Ensure that we have access to the policy.
  137. policy = self._get_policy_obj(context, policy_id)
  138. rule = rule_obj(context, qos_policy_id=policy_id, **rule_data)
  139. rule.create()
  140. policy.reload_rules()
  141. self.notification_driver_manager.update_policy(context, policy)
  142. return rule
  143. @db_base_plugin_common.convert_result_to_dict
  144. def update_policy_rule(self, context, rule_obj, rule_id, policy_id,
  145. rule_data):
  146. """Update a QoS policy rule.
  147. :param context: neutron api request context
  148. :type context: neutron.context.Context
  149. :param rule_obj: the rule object
  150. :type rule_obj: a class from the rule_object (qos.objects.rule) module
  151. :param rule_id: the id of the QoS policy rule to update
  152. :type rule_id: str uuid
  153. :param policy_id: the id of the rule's policy
  154. :type policy_id: str uuid
  155. :param rule_data: the new rule data to update
  156. :type rule_data: dict
  157. :returns: a QoS policy rule object
  158. """
  159. rule_type = rule_obj.rule_type
  160. rule_data = rule_data[rule_type + '_rule']
  161. with db_api.autonested_transaction(context.session):
  162. # Ensure we have access to the policy.
  163. policy = self._get_policy_obj(context, policy_id)
  164. # Ensure the rule belongs to the policy.
  165. policy.get_rule_by_id(rule_id)
  166. rule = rule_obj(context, id=rule_id)
  167. rule.update_nonidentifying_fields(rule_data, reset_changes=True)
  168. rule.update()
  169. policy.reload_rules()
  170. self.notification_driver_manager.update_policy(context, policy)
  171. return rule
  172. def delete_policy_rule(self, context, rule_obj, rule_id, policy_id):
  173. """Delete a QoS policy rule.
  174. :param context: neutron api request context
  175. :type context: neutron.context.Context
  176. :param rule_obj: the rule object
  177. :type rule_obj: a class from the rule_object (qos.objects.rule) module
  178. :param rule_id: the id of the QosPolicy Rule to delete
  179. :type rule_id: str uuid
  180. :param policy_id: the id of the rule's policy
  181. :type policy_id: str uuid
  182. :returns: None
  183. """
  184. with db_api.autonested_transaction(context.session):
  185. # Ensure we have access to the policy.
  186. policy = self._get_policy_obj(context, policy_id)
  187. rule = policy.get_rule_by_id(rule_id)
  188. rule.delete()
  189. policy.reload_rules()
  190. self.notification_driver_manager.update_policy(context, policy)
  191. @db_base_plugin_common.filter_fields
  192. @db_base_plugin_common.convert_result_to_dict
  193. def get_policy_rule(self, context, rule_obj, rule_id, policy_id,
  194. fields=None):
  195. """Get a QoS policy rule.
  196. :param context: neutron api request context
  197. :type context: neutron.context.Context
  198. :param rule_obj: the rule object
  199. :type rule_obj: a class from the rule_object (qos.objects.rule) module
  200. :param rule_id: the id of the QoS policy rule to get
  201. :type rule_id: str uuid
  202. :param policy_id: the id of the rule's policy
  203. :type policy_id: str uuid
  204. :returns: a QoS policy rule object
  205. :raises: n_exc.QosRuleNotFound
  206. """
  207. with db_api.autonested_transaction(context.session):
  208. # Ensure we have access to the policy.
  209. self._get_policy_obj(context, policy_id)
  210. rule = rule_obj.get_object(context, id=rule_id)
  211. if not rule:
  212. raise n_exc.QosRuleNotFound(policy_id=policy_id, rule_id=rule_id)
  213. return rule
  214. # TODO(QoS): enforce rule types when accessing rule objects
  215. @db_base_plugin_common.filter_fields
  216. @db_base_plugin_common.convert_result_to_dict
  217. def get_policy_rules(self, context, rule_obj, policy_id, filters=None,
  218. fields=None, sorts=None, limit=None, marker=None,
  219. page_reverse=False):
  220. """Get QoS policy rules.
  221. :param context: neutron api request context
  222. :type context: neutron.context.Context
  223. :param rule_obj: the rule object
  224. :type rule_obj: a class from the rule_object (qos.objects.rule) module
  225. :param policy_id: the id of the QosPolicy for which to get rules
  226. :type policy_id: str uuid
  227. :returns: QoS policy rule objects meeting the search criteria
  228. """
  229. with db_api.autonested_transaction(context.session):
  230. # Ensure we have access to the policy.
  231. self._get_policy_obj(context, policy_id)
  232. filters = filters or dict()
  233. filters[qos_consts.QOS_POLICY_ID] = policy_id
  234. return rule_obj.get_objects(context, **filters)