Openstack plugin开发(二)qos插件源码

1.配置插件映射

vi /neutron/neutron/plugins/common/constants.py

from neutron_lib.plugins import constants as p_const


# Maps extension alias to service type that
# can be implemented by the core plugin.
EXT_TO_SERVICE_MAPPING = {
    'fwaas': p_const.FIREWALL,
    'vpnaas': p_const.VPN,
    'metering': p_const.METERING,
    'router': p_const.L3,
    # qos映射
    'qos': p_const.QOS,
}

# Maps default service plugins entry points to their extension aliases
DEFAULT_SERVICE_PLUGINS = {
    'auto_allocate': 'auto-allocated-topology',
    'tag': 'tag',
    'timestamp': 'timestamp',
    'network_ip_availability': 'network-ip-availability',
    'flavors': 'flavors',
    'revisions': 'revisions',
}

constants路径

vi /usr/lib/python2.7/site-packages/neutron_lib/plugins/constants.py

# Well-known service type constants:
LOADBALANCER = "LOADBALANCER"
LOADBALANCERV2 = "LOADBALANCERV2"
FIREWALL = "FIREWALL"
VPN = "VPN"
METERING = "METERING"
FLAVORS = "FLAVORS"
QOS = "QOS"
CORE = 'CORE'
L3 = 'L3_ROUTER_NAT'
LOG_API = "LOGGING"
PORTFORWARDING = "PORTFORWARDING"
FLOATINGIPPOOL = "FLOATINGIPPOOL"
NETWORK_SEGMENT_RANGE = "NETWORK_SEGMENT_RANGE"
CONNTRACKHELPER = "CONNTRACKHELPER"

2.extension实现

vi /neutron/neutron/extensions/qos.py

import abc
import itertools
import re

from neutron_lib.api.definitions import qos as apidef
from neutron_lib.api import extensions as api_extensions
from neutron_lib.plugins import constants
from neutron_lib.plugins import directory
from neutron_lib.services import base as service_base
import six

from neutron.api import extensions
from neutron.api.v2 import base
from neutron.api.v2 import resource_helper
from neutron.objects.qos import rule as rule_object


class Qos(api_extensions.APIExtensionDescriptor):
    """Quality of Service API extension."""

    api_definition = apidef

    @classmethod
    def get_plugin_interface(cls):
        return QoSPluginBase

    @classmethod
    def get_resources(cls):
        """Returns Ext Resources."""
        special_mappings = {'policies': 'policy'}
        plural_mappings = resource_helper.build_plural_mappings(
            special_mappings, itertools.chain(
                apidef.RESOURCE_ATTRIBUTE_MAP,
                apidef.SUB_RESOURCE_ATTRIBUTE_MAP))

        resources = resource_helper.build_resource_info(
                plural_mappings,
                apidef.RESOURCE_ATTRIBUTE_MAP,
                constants.QOS,
                translate_name=True,
                allow_bulk=True)

        plugin = directory.get_plugin(constants.QOS)
        for collection_name in apidef.SUB_RESOURCE_ATTRIBUTE_MAP:
            resource_name = collection_name[:-1]
            parent = apidef.SUB_RESOURCE_ATTRIBUTE_MAP[
                collection_name].get('parent')
            params = apidef.SUB_RESOURCE_ATTRIBUTE_MAP[collection_name].get(
                'parameters')

            controller = base.create_resource(collection_name, resource_name,
                                              plugin, params,
                                              allow_bulk=True,
                                              parent=parent,
                                              allow_pagination=True,
                                              allow_sorting=True)

            resource = extensions.ResourceExtension(
                collection_name,
                controller, parent,
                path_prefix=apidef.API_PREFIX,
                attr_map=params)
            resources.append(resource)

        return resources


@six.add_metaclass(abc.ABCMeta)
class QoSPluginBase(service_base.ServicePluginBase):

    path_prefix = apidef.API_PREFIX

    # The rule object type to use for each incoming rule-related request.
    rule_objects = {'bandwidth_limit': rule_object.QosBandwidthLimitRule,
                    'dscp_marking': rule_object.QosDscpMarkingRule,
                    'minimum_bandwidth': rule_object.QosMinimumBandwidthRule}

    # Patterns used to call method proxies for all policy-rule-specific
    # method calls (see __getattr__ docstring, below).
    qos_rule_method_patterns = [
            re.compile(
                r"^((create|update|delete)_policy_(?P<rule_type>.*)_rule)$"),
            re.compile(
                r"^(get_policy_(?P<rule_type>.*)_(rules|rule))$"),
            # The following entry handles rule alias calls
            re.compile(
                r"^((update|delete|get)_alias_(?P<rule_type>.*)_rule)$"),
                               ]

    def __getattr__(self, attrib):
        """Implement method proxies for all policy-rule-specific requests. For
        a given request type (such as to update a rule), a single method will
        handle requests for all rule types.  For example, the
        update_policy_rule method will handle requests for both
        update_policy_dscp_marking_rule and update_policy_bandwidth_limit_rule.

        In the case of rule alias calls, the update_rule method will handle
        requests for both update_dscp_marking_rule and
        update_bandwidth_limit_rule.

        :param attrib: the requested method; in the normal case, this will be,
                       for example, "update_policy_dscp_marking_rule"
        :type attrib: str
        """
        # Find and call the proxy method that implements the requested one.
        for pattern in self.qos_rule_method_patterns:
            res = re.match(pattern, attrib)
            if res:
                rule_type = res.group('rule_type')
                if rule_type in self.rule_objects:
                    # Remove the rule_type value (plus underscore) from attrib
                    # in order to get the proxy method name. So, for instance,
                    # from "delete_policy_dscp_marking_rule" we'll get
                    # "delete_policy_rule".
                    proxy_method = attrib.replace(rule_type + '_', '')
                    proxy_method = proxy_method.replace('alias_', '')

                    rule_cls = self.rule_objects[rule_type]
                    return self._call_proxy_method(proxy_method, rule_cls)

        # If we got here, then either attrib matched no pattern or the
        # rule_type embedded in attrib wasn't in self.rule_objects.
        raise AttributeError(attrib)

    def _call_proxy_method(self, method_name, rule_cls):
        """Call proxy method. We need to add the rule_cls, obtained from the
        self.rule_objects dictionary, to the incoming args.  The context is
        passed to proxy method as first argument; the remaining args will
        follow rule_cls.

        Some of the incoming method calls have the policy rule name as one of
        the keys in the kwargs.  For instance, the incoming kwargs for the
        create_policy_bandwidth_limit_rule take this form:

            { 'bandwidth_limit_rule': {
                  u'bandwidth_limit_rule':
                  { 'max_burst_kbps': 0,
                    u'max_kbps': u'100',
                    'tenant_id': u'a8a31c9434ff431cb789c809777505ec'}
                  },
              'policy_id': u'46985da5-9684-402e-b0d7-b7adac909c3a'
            }

        We need to generalize this structure for all rule types so will
        (effectively) rename the rule-specific keyword (e.g., in the above, the
        first occurrence of 'bandwidth_limit_rule') to be 'rule_data'.

        :param method_name: the name of the method to call
        :type method_name: str
        :param rule_cls: the rule class, which is sent as an argument to the
                         proxy method
        :type rule_cls: a class from the rule_object (qos.objects.rule) module
        """
        def _make_call(method_name, rule_cls, *args, **kwargs):
            context = args[0]
            args_list = list(args[1:])
            params = kwargs
            rule_data_name = rule_cls.rule_type + "_rule"
            alias_rule_data_name = 'alias_' + rule_data_name
            if rule_data_name in params:
                params['rule_data'] = params.pop(rule_data_name)
            elif alias_rule_data_name in params:
                params['rule_data'] = params.pop(alias_rule_data_name)

            return getattr(self, method_name)(
                context, rule_cls, *args_list, **params
            )

        return lambda *args, **kwargs: _make_call(
            method_name, rule_cls, *args, **kwargs)

    def get_plugin_description(self):
        return "QoS Service Plugin for ports and networks"

    @classmethod
    def get_plugin_type(cls):
        return constants.QOS

    @abc.abstractmethod
    def get_rule_type(self, context, rule_type_name, fields=None):
        pass

    @abc.abstractmethod
    def get_rule_types(self, context, filters=None, fields=None, sorts=None,
                       limit=None, marker=None, page_reverse=False):
        pass

    @abc.abstractmethod
    def create_policy(self, context, policy):
        pass

    @abc.abstractmethod
    def update_policy(self, context, policy_id, policy):
        pass

    @abc.abstractmethod
    def delete_policy(self, context, policy_id):
        pass

    @abc.abstractmethod
    def get_policy(self, context, policy_id, fields=None):
        pass

    @abc.abstractmethod
    def get_policies(self, context, filters=None, fields=None, sorts=None,
                     limit=None, marker=None, page_reverse=False):
        pass

    @abc.abstractmethod
    def create_policy_rule(self, context, rule_cls, policy_id, rule_data):
        pass

    @abc.abstractmethod
    def update_policy_rule(self, context, rule_cls, rule_id, policy_id,
                           rule_data):
        pass

    @abc.abstractmethod
    def delete_policy_rule(self, context, rule_cls, rule_id, policy_id):
        pass

    @abc.abstractmethod
    def get_policy_rule(self, context, rule_cls, rule_id, policy_id,
                        fields=None):
        pass

    @abc.abstractmethod
    def get_policy_rules(self, context, rule_cls, policy_id,
                         filters=None, fields=None, sorts=None, limit=None,
                         marker=None, page_reverse=False):
        pass

    @abc.abstractmethod
    def update_rule(self, context, rule_cls, rule_id, rule_data):
        pass

    @abc.abstractmethod
    def delete_rule(self, context, rule_cls, rule_id):
        pass

    @abc.abstractmethod
    def get_rule(self, context, rule_cls, rule_id, fields=None):
        pass

Extension的内容主要有RESOURCE_ATTRIBUTE_MAP、Extension类和一个QoSPluginBase抽象类,该类定义了api。

RESOURCE_ATTRIBUTE_MAP定义了资源的属性及其访问权限。

创建一个ExtensionDescriptor的子类,子类的名称必须和文件名一致,且类名第一个字母大写,文件名为qos.py,类名为Qos。

创建一个抽象类,定义API的接口函数,接口主要定义对资源的create、get、update、delete操作,接口函数名必须为action_resource的形式,例如delete_policy。

3.plugin实现

vi /neutron/neutron/services/qos/qos_plugin.py

@resource_extend.has_resource_extenders
class QoSPlugin(qos.QoSPluginBase):
    """Implementation of the Neutron QoS Service Plugin.

    This class implements a Quality of Service plugin that provides quality of
    service parameters over ports and networks.

    """
    supported_extension_aliases = [qos_apidef.ALIAS,
                                   qos_bw_limit_direction.ALIAS,
                                   qos_default.ALIAS,
                                   qos_rule_type_details.ALIAS,
                                   port_resource_request.ALIAS,
                                   qos_bw_minimum_ingress.ALIAS,
                                   qos_rules_alias.ALIAS]

    __native_pagination_support = True
    __native_sorting_support = True
    __filter_validation_support = True

    def __init__(self):
        super(QoSPlugin, self).__init__()
        self.driver_manager = manager.QosServiceDriverManager()

        callbacks_registry.subscribe(
            self._validate_create_port_callback,
            callbacks_resources.PORT,
            callbacks_events.PRECOMMIT_CREATE)
        callbacks_registry.subscribe(
            self._validate_update_port_callback,
            callbacks_resources.PORT,
            callbacks_events.PRECOMMIT_UPDATE)
        callbacks_registry.subscribe(
            self._validate_update_network_callback,
            callbacks_resources.NETWORK,
            callbacks_events.PRECOMMIT_UPDATE)
...

编写QoSPlugin类,该类继承了QoSPluginBase,并实现了相关的接口函数。这里的api只是简单地输出日志。

4.plugin的导入路径

vi /neutron/setup.cfg

neutron.service_plugins =
        dummy = neutron.tests.unit.dummy_plugin:DummyServicePlugin
        router = neutron.services.l3_router.l3_router_plugin:L3RouterPlugin
        metering = neutron.services.metering.metering_plugin:MeteringPlugin
        qos = neutron.services.qos.qos_plugin:QoSPlugin
        tag = neutron.services.tag.tag_plugin:TagPlugin
        flavors = neutron.services.flavors.flavors_plugin:FlavorsPlugin
        auto_allocate = neutron.services.auto_allocate.plugin:Plugin
        segments = neutron.services.segments.plugin:Plugin
        network_ip_availability = neutron.services.network_ip_availability.plugin:NetworkIPAvailabilityPlugin
        network_segment_range = neutron.services.network_segment_range.plugin:NetworkSegmentRangePlugin
        revisions = neutron.services.revisions.revision_plugin:RevisionPlugin
        timestamp = neutron.services.timestamp.timestamp_plugin:TimeStampPlugin
        trunk = neutron.services.trunk.plugin:TrunkPlugin
        loki = neutron.services.loki.loki_plugin:LokiPlugin
        log = neutron.services.logapi.logging_plugin:LoggingPlugin
        port_forwarding = neutron.services.portforwarding.pf_plugin:PortForwardingPlugin
        placement = neutron.services.placement_report.plugin:PlacementReportPlugin
        conntrack_helper = neutron.services.conntrack_helper.plugin:Plugin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值