跟我一起探索Neutron (4) - 创建网络代码分析 2
2. 创建 network
Q:创建网络的同时,会创建该网络对应的网络空间吗?
A:不会,在创建属于该网络的子网的时候才会(见下一篇分析)。
neutron.api.v2.base.Controller
def create(self, request, body=None, **kwargs):
self._notifier.info(request.context, self._resource + '.create.start', body)
return self._create(request, body, **kwargs)
def _create(self, request, body, **kwargs): ...
# (输入数据) body: {u'network': {u'name': u'net1', u'admin_state_up': True}} body = Controller.prepare_request_body(request.context, body, True, self._resource, self._attr_info, allow_bulk=self._allow_bulk)
...
# 检查网络资源限制,在数据库中标记资源使用,预留资源 reservations = [] try: for (tenant, delta) in request_deltas.items():
# neutron.db.quota.driver.DbQuotaDriver.make_reservation reservation = quota.QUOTAS.make_reservation(request.context, tenant, {self._resource: delta}, self._plugin) reservations.append(reservation) except exceptions.QuotaResourceUnknown as e: LOG.debug(e)
...
else:
... else: obj = do_create(body)
# 提交预留资源记录(commit reservation),发送消息通知网络创建结束 return notify({self._resource: self._view(request.context, obj)})
def do_create(body, bulk=False, emulated=False):
...
else:
# action:create_network
obj_creator = getattr(self._plugin, action)
try:
...
else:
...
# obj_creator:neutron.plugins.ml2.plugin.Ml2Plugin.create_network
return obj_creator(request.context, **kwargs)
except Exception:
# 异常则撤销预留的资源
with excutils.save_and_reraise_exception():
for reservation in reservations:
quota.QUOTAS.cancel_reservation(request.context, reservation.reservation_id)
neutron.plugins.ml2.plugin.Ml2Plugin
def create_network(self, context, network):
# 发送 notification,通知有 before create 事件
self._before_create_network(context, network)
result, mech_context = self._create_network_db(context, network)
# 发送消息通知,network 在数据库中已经有记录
return self._after_create_network(context, result, mech_context)
def _create_network_db(self, context, network):
......
with db_api.CONTEXT_WRITER.using(context):
# neutron.db.db_base_plugin_v2.NeutronDbPluginV2.create_network_db,生成 network id,新建并获取数据库 network 对象
net_db = self.create_network_db(context, network)
net_data['id'] = net_db.id
# neutron.plugins.ml2.managers.create_network_segments,分配 network 的 segment id(数据库的 ml2_vxlan_allocations 表),并将信息保存至 networksegments 表
self.type_manager.create_network_segments(context, net_data, tenant_id)
......
# neutron.plugins.ml2.extensions.port_security.PortSecurityExtensionDriver.process_create_network,保存信息(该网络的 port_security_enabled 属性)到数据库表 networksecuritybindings
self.extension_manager.process_create_network(context, {k: v for k, v in net_data.items() if k != 'id'}, result)
......
registry.notify(resources.NETWORK, events.PRECOMMIT_CREATE, self, context=context, request=net_data, network=result)
......
return result, mech_context