🪐
OpenStack 源码分析之 Neutron
  • 前言
  • 整体结构
  • bin
  • doc
  • etc
    • init.d/
    • neutron/
    • api-paste.ini
    • dhcp_agent.ini
    • fwaas_driver.ini
    • l3_agent.ini
    • lbaas_agent.ini
    • metadata_agent.ini
    • metering_agent.ini
    • vpn_agent.ini
    • neutron.conf
    • policy.json
    • rootwrap.conf
    • services.conf
  • neutron
    • agent
      • common/
      • linux/
      • metadata/
      • dhcp_agent.py
      • firewall.py
      • l2population_rpc.py
      • l3_agent.py
      • l3_ha_agent.py
      • netns_cleanup_util.py
      • ovs_cleanup_util.py
      • rpc.py
      • securitygroups_rpc.py
    • api
      • rpc
      • v2
      • views
      • api_common.py
      • extensions.py
      • versions.py
    • cmd
    • common
      • config.py
      • constants.py
      • exceptions.py
      • ipv6_utils.py
      • log.py
      • rpc.py
      • test_lib.py
      • topics.py
      • utils.py
    • db
      • agents_db.py
      • agentschedulers_db.py
      • api.py
      • common_db_mixin.py
      • db_base_plugin_v2.py
      • migration
      • model_base.py
      • models_v2.py
      • securitygroups_rpc_base.py
      • sqlalchemyutils.py
      • 扩展资源和操作类
        • allowedaddresspairs_db.py
        • dvr_mac_db.py
        • external_net_db.py
        • extradhcpopt_db.py
        • extraroute_db.py
        • firewall
        • l3_agentschedulers_db.py
        • l3_attrs_db.py
        • l3_db.py
        • l3_dvr_db.py
        • l3_dvrscheduler_db.py
        • l3_gwmode_db.py
        • l3_hamode_db.py
        • l3_hascheduler_db.py
        • loadbalancer
        • metering
        • portbindings_base.py
        • portbindings_db.py
        • portsecurity_db.py
        • quota_db.py
        • routedserviceinsertion_db.py
        • routerservicetype_db.py
        • securitygroups_db.py
        • servicetype_db.py
        • vpn
    • debug
      • commands.py
      • debug_agent.py
      • shell.py
    • extensions
      • agent.py
      • allowedaddresspairs.py
      • dhcpagentscheduler.py
      • dvr.py
      • external_net.py
      • extraroute.py
      • extra_dhcp_opt.py
      • firewall.py
      • flavor.py
      • l3.py
      • l3agentscheduler.py
      • l3_ext_gw_mode.py
      • l3_ext_ha_mode.py
      • lbaas_agentscheduler.py
      • loadbalancer.py
      • metering.py
      • multiprovidernet.py
      • portbindings.py
      • portsecurity.py
      • providernet.py
      • quotasv2.py
      • routedserviceinsertion.py
      • routerservicetype.py
      • securitygroup.py
      • servicetype.py
      • vpnaas.py
      • init.py
    • hacking
      • checks.py
      • init.py
    • locale
    • notifiers
      • nova.py
      • init.py
    • openstack
      • common
        • cache
        • context.py
        • eventlet_backdoor.py
        • fileutils.py
        • fixture
        • local.py
        • lockutils.py
        • log.py
        • loopingcall.py
        • middleware
        • periodic_task.py
        • policy.py
        • processutils.py
        • service.py
        • systemd.py
        • threadgroup.py
        • uuidutils.py
        • versionutils.py
        • _i18n.py
    • plugins
      • bigswitch
        • agent
        • config.py
        • db
        • extensions
        • l3_router_plugin.py
        • plugin.py
        • routerrule_db.py
        • servermanager.py
        • tests
        • vcsversion.py
        • version.py
        • init.py
      • brocade
        • db
        • NeutronPlugin.py
        • nos
        • tests
        • vlanbm.py
        • init.py
      • cisco
        • cfg_agent
        • common
        • db
        • extensions
        • l2device_plugin_base.py
        • l3
        • models
        • n1kv
        • network_plugin.py
        • service_plugins
        • init.py
      • common
        • constants.py
        • utils.py
        • init.py
      • embrane
        • agent
        • base_plugin.py
        • common
        • l2base
        • plugins
        • init.py
      • hyperv
        • agent
        • agent_notifier_api.py
        • common
        • db.py
        • hyperv_neutron_plugin.py
        • model.py
        • rpc_callbacks.py
        • init.py
      • ibm
        • agent
        • common
        • sdnve_api.py
        • sdnve_api_fake.py
        • sdnve_neutron_plugin.py
      • linuxbridge
        • agent
        • common
        • db
        • init.py
      • metaplugin
        • common
        • meta_db_v2.py
        • meta_models_v2.py
        • meta_neutron_plugin.py
        • proxy_neutron_plugin.py
        • init.py
      • midonet
        • agent
        • common
        • midonet_lib.py
        • plugin.py
        • init.py
      • ml2
        • common
        • config.py
        • db.py
        • drivers
        • driver_api.py
        • driver_context.py
        • managers.py
        • models.py
        • plugin.py
        • rpc.py
      • mlnx
        • agent
        • agent_notify_api.py
        • common
        • db
        • mlnx_plugin.py
        • rpc_callbacks.py
        • init.py
      • nec
        • agent
        • common
        • db
        • drivers
        • extensions
        • nec_plugin.py
        • nec_router.py
        • ofc_driver_base.py
        • ofc_manager.py
        • packet_filter.py
        • router_drivers.py
        • init.py
      • nuage
        • common
        • extensions
        • nuagedb.py
        • nuage_models.py
        • plugin.py
        • syncmanager.py
        • init.py
      • ofagent
        • agent
        • common
      • oneconvergence
        • agent
        • lib
        • plugin.py
        • init.py
      • opencontrail
        • common
        • contrail_plugin.py
        • init.py
      • openvswitch
        • agent
        • common
        • ovs_models_v2.py
        • init.py
      • plumgrid
        • common
        • drivers
        • plumgrid_plugin
        • init.py
      • sriovnicagent
        • common
        • eswitch_manager.py
        • pci_lib.py
        • sriov_nic_agent.py
        • init.py
      • vmware
        • api_client
        • check_nsx_config.py
        • common
        • dbexts
        • dhcpmeta_modes.py
        • dhcp_meta
        • extensions
        • nsxlib
        • nsx_cluster.py
        • plugin.py
        • plugins
        • shell
        • vshield
        • init.py
    • scheduler
      • dhcp_agent_scheduler.py
      • l3_agent_scheduler.py
    • server
    • service.py
    • services
      • firewall
        • agents
        • drivers
        • fwaas_plugin.py
      • l3_router
        • brocade
        • l3_apic.py
        • l3_arista.py
        • l3_router_plugin.py
        • init.py
      • loadbalancer
        • agent
        • agent_scheduler.py
        • constants.py
        • drivers
        • plugin.py
      • metering
        • agents
        • drivers
        • metering_plugin.py
      • provider_configuration.py
      • service_base.py
      • vpn
        • agent.py
        • common
        • device_drivers
        • plugin.py
        • service_drivers
        • init.py
      • init.py
    • tests
      • base.py
      • common
        • agents
        • init.py
      • etc
        • rootwrap.d
      • fake_notifier.py
      • functional
        • agent
        • base.py
        • contrib
        • db
        • sanity
        • init.py
      • post_mortem_debug.py
      • tools.py
      • unit
      • var
      • init.py
    • auth.py
    • context.py
    • hooks.py
    • i18n.py
    • manager.py
    • neutron_plugin_base_v2.py
    • policy.py
    • quota.py
    • service.py
    • version.py
    • wsgi.py
  • rally-jobs
    • extra
      • README.rst
    • plugins
      • README.rst
      • init.py
    • neutron-neutron.yaml
    • README.rst
  • tools
    • check_bash.sh
    • check_i18n.py
    • check_i18n_test_case.txt
    • clean.sh
    • i18n_cfg.py
    • install_venv.py
    • install_venv_common.py
    • pretty_tox.sh
    • with_venv.sh
  • 理解代码
    • 调用逻辑
    • REST API 专题
    • RPC 专题
      • agent RPC
      • plugin RPC
      • neutron-server RPC
    • Plugin 专题
    • Extension 专题
    • Agent 专题
    • Driver 专题
Powered by GitBook
On this page
  • attributes.py
  • base.py
  • resource.py
  • resource_helper.py
  • router.py

Was this helpful?

  1. neutron
  2. api

v2

实现neutron api第2个版本的定义。 主要方法包括index、update、create、show和delete。

attributes.py

这里面定义了一系列的_validate_xxx方法,包括_validate_mac_address、_validate_ip_address、_validate_boolean等,对传入的参数进行格式检查。

base.py

定义了Controller类和create_resource方法。 后者根据传入参数声明一个Controller并用它初始化一个wsgi的资源。

def create_resource(collection, resource, plugin, params, allow_bulk=False,
                    member_actions=None, parent=None, allow_pagination=False,
                    allow_sorting=False):
    controller = Controller(plugin, collection, resource, params, allow_bulk,
                            member_actions=member_actions, parent=parent,
                            allow_pagination=allow_pagination,
                            allow_sorting=allow_sorting)

    return wsgi_resource.Resource(controller, FAULT_MAP)

Controller类负责对rest API调用的资源进行处理,将对资源的请求转化为对应的plugin中方法的调用。 成员包括一个对dhcp agent的notifier。

resource.py

主要定义了Request类和Resource方法。 Request类继承自wsgi.Request,代表一个资源请求。 Resource方法会根据传入的Controller构造一个resource对象。

resource_helper.py

包括build_plural_mappings和build_resource_info两个方法。 前者对所有的资源创建从其复数到单数形式的映射;后者为advanced services扩展创建API资源对象。

router.py

此处的router意味着是在wsgi框架下对请求的rest api进行调度的router,并非网络中的router。从外面api-paste.ini文件中,可以看到最终app指向的是

[app:neutronapiapp_v2_0]
paste.app_factory = neutron.api.v2.router:APIRouter.factory

该文件主要包括了APIRouter类,继承自wsgi.Router类,定义了factory方法。 其中factory()方法返回一个该类的实体。 分析其初始化方法:

def __init__(self, **local_config):
        mapper = routes_mapper.Mapper()
        plugin = manager.NeutronManager.get_plugin()
        ext_mgr = extensions.PluginAwareExtensionManager.get_instance()
        ext_mgr.extend_resources("2.0", attributes.RESOURCE_ATTRIBUTE_MAP)

        col_kwargs = dict(collection_actions=COLLECTION_ACTIONS,
                          member_actions=MEMBER_ACTIONS)

        def _map_resource(collection, resource, params, parent=None):
            allow_bulk = cfg.CONF.allow_bulk
            allow_pagination = cfg.CONF.allow_pagination
            allow_sorting = cfg.CONF.allow_sorting
            controller = base.create_resource(
                collection, resource, plugin, params, allow_bulk=allow_bulk,
                parent=parent, allow_pagination=allow_pagination,
                allow_sorting=allow_sorting)
            path_prefix = None
            if parent:
                path_prefix = "/%s/{%s_id}/%s" % (parent['collection_name'],
                                                  parent['member_name'],
                                                  collection)
            mapper_kwargs = dict(controller=controller,
                                 requirements=REQUIREMENTS,
                                 path_prefix=path_prefix,
                                 **col_kwargs)
            return mapper.collection(collection, resource,
                                     **mapper_kwargs)

        mapper.connect('index', '/', controller=Index(RESOURCES))
        for resource in RESOURCES:
            _map_resource(RESOURCES[resource], resource,
                          attributes.RESOURCE_ATTRIBUTE_MAP.get(
                              RESOURCES[resource], dict()))

        for resource in SUB_RESOURCES:
            _map_resource(SUB_RESOURCES[resource]['collection_name'], resource,
                          attributes.RESOURCE_ATTRIBUTE_MAP.get(
                              SUB_RESOURCES[resource]['collection_name'],
                              dict()),
                          SUB_RESOURCES[resource]['parent'])

        super(APIRouter, self).__init__(mapper)

首先初始化一个router的mapper;之后获取plugin,通过调用NeutronManger类(负责解析配置文件并读取其中的plugin信息);然后获取支持的扩展的资源管理者,并把默认的对网络、子网和端口的资源的操作添加到扩展的资源管理者类中。

接下来,绑定资源的请求到各个资源上。_map_resource方法中创建了对应的控制器和映射关系。控制器在base.py文件中,其中定义了index、show、create、delete和update方法。这些方法中会获取plugin的对应方法对请求进行处理。例如,在create方法中有

obj_creator = getattr(self._plugin, action)
...
obj = obj_creator(request.context, **kwargs)
PreviousrpcNextviews

Last updated 5 years ago

Was this helpful?