Make MechanismDriverContext plugin_context public

MechanismDriverContext has an attribute _plugin_context, which carries
the current context with it. This is used by many ml2 drivers, as it is
the only way for them to get the current context. We now make this a
public API by adding a property to MechanismDriverContext that returns
_plugin_context as a read-only attribute.

Change-Id: If9b05655286f42081cf26c90c563429ca2e63244
This commit is contained in:
Sebastian Lohff 2022-11-16 16:16:22 +01:00
parent bf44e70db6
commit 9a483f02be
9 changed files with 63 additions and 55 deletions

View File

@ -74,7 +74,7 @@ class OVNMechanismDriver(mech_driver.OVNMechanismDriver):
def create_port_postcommit(self, context):
port = context.current
self.ovn_client.create_port(context._plugin_context, port)
self.ovn_client.create_port(context.plugin_context, port)
def update_port_precommit(self, context):
pass
@ -82,7 +82,7 @@ class OVNMechanismDriver(mech_driver.OVNMechanismDriver):
def update_port_postcommit(self, context):
port = context.current
original_port = context.original
self.ovn_client.update_port(context._plugin_context, port,
self.ovn_client.update_port(context.plugin_context, port,
original_port)
def delete_port_precommit(self, context):
@ -91,9 +91,7 @@ class OVNMechanismDriver(mech_driver.OVNMechanismDriver):
def delete_port_postcommit(self, context):
port = copy.deepcopy(context.current)
port['network'] = context.network.current
# FIXME(lucasagomes): PortContext does not have a session, therefore
# we need to use the _plugin_context attribute.
self.ovn_client.delete_port(context._plugin_context, port['id'])
self.ovn_client.delete_port(context.plugin_context, port['id'])
class AgentNotifierApi(object):

View File

@ -65,6 +65,10 @@ class MechanismDriverContext(object):
# method call of the plugin.
self._plugin_context = plugin_context
@property
def plugin_context(self):
return self._plugin_context
class NetworkContext(MechanismDriverContext, api.NetworkContext):
@ -111,9 +115,9 @@ class SubnetContext(MechanismDriverContext, api.SubnetContext):
def network(self):
if self._network_context is None:
network = self._plugin.get_network(
self._plugin_context, self.current['network_id'])
self.plugin_context, self.current['network_id'])
self._network_context = NetworkContext(
self._plugin, self._plugin_context, network)
self._plugin, self.plugin_context, network)
return self._network_context
@ -196,9 +200,9 @@ class PortContext(MechanismDriverContext, api.PortContext):
def network(self):
if not self._network_context:
network = self._plugin.get_network(
self._plugin_context, self.current['network_id'])
self.plugin_context, self.current['network_id'])
self._network_context = NetworkContext(
self._plugin, self._plugin_context, network)
self._plugin, self.plugin_context, network)
return self._network_context
@property
@ -246,7 +250,7 @@ class PortContext(MechanismDriverContext, api.PortContext):
# TODO(kevinbenton): eliminate the query below. The above should
# always return since the port is bound to a network segment. Leaving
# in for now for minimally invasive change for back-port.
segment = segments_db.get_segment_by_id(self._plugin_context,
segment = segments_db.get_segment_by_id(self.plugin_context,
segment_id)
if not segment:
LOG.warning("Could not expand segment %s", segment_id)
@ -292,7 +296,7 @@ class PortContext(MechanismDriverContext, api.PortContext):
return self._segments_to_bind
def host_agents(self, agent_type):
return self._plugin.get_agents(self._plugin_context,
return self._plugin.get_agents(self.plugin_context,
filters={'agent_type': [agent_type],
'host': [self._binding.host]})
@ -327,4 +331,4 @@ class PortContext(MechanismDriverContext, api.PortContext):
def release_dynamic_segment(self, segment_id):
return self._plugin.type_manager.release_dynamic_segment(
self._plugin_context, segment_id)
self.plugin_context, segment_id)

View File

@ -75,7 +75,7 @@ class L2populationMechanismDriver(api.MechanismDriver):
def delete_port_postcommit(self, context):
port = context.current
agent_host = context.host
plugin_context = context._plugin_context
plugin_context = context.plugin_context
fdb_entries = self._get_agent_fdb(
plugin_context, context.bottom_bound_segment, port, agent_host)
if fdb_entries and l3_hamode_db.is_ha_router_port(
@ -117,13 +117,13 @@ class L2populationMechanismDriver(api.MechanismDriver):
return
# We should not add arp responder for non tunnel network type
port_context = context._plugin_context
port_context = context.plugin_context
agent = l2pop_db.get_agent_by_host(port_context, agent_host)
segment = context.bottom_bound_segment
if not self._validate_segment(segment, port['id'], agent):
return
agent_ip = l2pop_db.get_agent_ip_by_host(context._plugin_context,
agent_ip = l2pop_db.get_agent_ip_by_host(context.plugin_context,
agent_host)
orig_mac_ip = [l2pop_rpc.PortInfo(mac_address=port['mac_address'],
@ -160,7 +160,7 @@ class L2populationMechanismDriver(api.MechanismDriver):
def update_port_postcommit(self, context):
port = context.current
orig = context.original
plugin_context = context._plugin_context
plugin_context = context.plugin_context
if l3_hamode_db.is_ha_router_port(plugin_context, port['device_owner'],
port['device_id']):
return
@ -258,7 +258,7 @@ class L2populationMechanismDriver(api.MechanismDriver):
"list_router_ids_on_host",
None):
admin_context = n_context.get_admin_context()
port_context = context._plugin_context
port_context = context.plugin_context
fdb_entries = self._get_agent_fdb(
port_context, context.bottom_bound_segment, port, agent_host,
include_ha_router_ports=True)
@ -274,7 +274,7 @@ class L2populationMechanismDriver(api.MechanismDriver):
def update_port_up(self, context, refresh_tunnels=False):
port = context.current
agent_host = context.host
port_context = context._plugin_context
port_context = context.plugin_context
agent = l2pop_db.get_agent_by_host(port_context, agent_host)
if not agent:
LOG.warning("Unable to retrieve active L2 agent on host %s",

View File

@ -84,7 +84,7 @@ class AgentMechanismDriverBase(api.MechanismDriver, metaclass=abc.ABCMeta):
return
if context.host_agents(self.agent_type):
provisioning_blocks.add_provisioning_component(
context._plugin_context, port['id'], resources.PORT,
context.plugin_context, port['id'], resources.PORT,
provisioning_blocks.L2_AGENT_ENTITY)
def bind_port(self, context):
@ -147,7 +147,7 @@ class AgentMechanismDriverBase(api.MechanismDriver, metaclass=abc.ABCMeta):
subnet_id = data.get('subnet_id')
if subnet_id:
subnets.append(context._plugin.get_subnet(
context._plugin_context, subnet_id))
context.plugin_context, subnet_id))
return subnets
@abc.abstractmethod
@ -216,7 +216,7 @@ class AgentMechanismDriverBase(api.MechanismDriver, metaclass=abc.ABCMeta):
# trying to bind with a dead agent.
}
return context._plugin.get_agents(
context._plugin_context,
context.plugin_context,
filters=agent_filters,
)

View File

@ -543,7 +543,7 @@ class OVNMechanismDriver(api.MechanismDriver):
"""
self._validate_network_segments(context.network_segments)
ovn_revision_numbers_db.create_initial_revision(
context._plugin_context, context.current['id'],
context.plugin_context, context.current['id'],
ovn_const.TYPE_NETWORKS,
std_attr_id=context.current['standard_attr_id'])
@ -559,7 +559,7 @@ class OVNMechanismDriver(api.MechanismDriver):
cause the deletion of the resource.
"""
network = context.current
self._ovn_client.create_network(context._plugin_context, network)
self._ovn_client.create_network(context.plugin_context, network)
def update_network_precommit(self, context):
"""Update resources of a network.
@ -596,7 +596,7 @@ class OVNMechanismDriver(api.MechanismDriver):
state or state changes that it does not know or care about.
"""
self._ovn_client.update_network(
context._plugin_context, context.current,
context.plugin_context, context.current,
original_network=context.original)
def delete_network_postcommit(self, context):
@ -612,26 +612,26 @@ class OVNMechanismDriver(api.MechanismDriver):
deleted.
"""
self._ovn_client.delete_network(
context._plugin_context,
context.plugin_context,
context.current['id'])
def create_subnet_precommit(self, context):
ovn_revision_numbers_db.create_initial_revision(
context._plugin_context, context.current['id'],
context.plugin_context, context.current['id'],
ovn_const.TYPE_SUBNETS,
std_attr_id=context.current['standard_attr_id'])
def create_subnet_postcommit(self, context):
self._ovn_client.create_subnet(context._plugin_context,
self._ovn_client.create_subnet(context.plugin_context,
context.current,
context.network.current)
def update_subnet_postcommit(self, context):
self._ovn_client.update_subnet(
context._plugin_context, context.current, context.network.current)
context.plugin_context, context.current, context.network.current)
def delete_subnet_postcommit(self, context):
self._ovn_client.delete_subnet(context._plugin_context,
self._ovn_client.delete_subnet(context.plugin_context,
context.current['id'])
def _validate_port_extra_dhcp_opts(self, port):
@ -663,18 +663,18 @@ class OVNMechanismDriver(api.MechanismDriver):
ovn_utils.validate_and_get_data_from_binding_profile(port)
self._validate_port_extra_dhcp_opts(port)
if self._is_port_provisioning_required(port, context.host):
self._insert_port_provisioning_block(context._plugin_context,
self._insert_port_provisioning_block(context.plugin_context,
port['id'])
ovn_revision_numbers_db.create_initial_revision(
context._plugin_context, port['id'], ovn_const.TYPE_PORTS,
context.plugin_context, port['id'], ovn_const.TYPE_PORTS,
std_attr_id=context.current['standard_attr_id'])
# in the case of router ports we also need to
# track the creation and update of the LRP OVN objects
if ovn_utils.is_lsp_router_port(port):
ovn_revision_numbers_db.create_initial_revision(
context._plugin_context, port['id'],
context.plugin_context, port['id'],
ovn_const.TYPE_ROUTER_PORTS,
std_attr_id=context.current['standard_attr_id'])
@ -782,7 +782,7 @@ class OVNMechanismDriver(api.MechanismDriver):
"""
port = copy.deepcopy(context.current)
port['network'] = context.network.current
self._ovn_client.create_port(context._plugin_context, port)
self._ovn_client.create_port(context.plugin_context, port)
self._notify_dhcp_updated(port['id'])
def update_port_precommit(self, context):
@ -807,7 +807,7 @@ class OVNMechanismDriver(api.MechanismDriver):
self._validate_port_extra_dhcp_opts(port)
if self._is_port_provisioning_required(port, context.host,
context.original_host):
self._insert_port_provisioning_block(context._plugin_context,
self._insert_port_provisioning_block(context.plugin_context,
port['id'])
if ovn_utils.is_lsp_router_port(port):
@ -815,7 +815,7 @@ class OVNMechanismDriver(api.MechanismDriver):
# logical router so we need to track the creation of the lrp
if not ovn_utils.is_lsp_router_port(original_port):
ovn_revision_numbers_db.create_initial_revision(
context._plugin_context, port['id'],
context.plugin_context, port['id'],
ovn_const.TYPE_ROUTER_PORTS, may_exist=True,
std_attr_id=context.current['standard_attr_id'])
@ -852,7 +852,7 @@ class OVNMechanismDriver(api.MechanismDriver):
LOG.info("Setting port %s status from DOWN to UP in order "
"to emit vif-interface-plugged event.",
port['id'])
self._plugin.update_port_status(context._plugin_context,
self._plugin.update_port_status(context.plugin_context,
port['id'],
const.PORT_STATUS_ACTIVE)
# The revision has been changed. In the meantime
@ -861,7 +861,7 @@ class OVNMechanismDriver(api.MechanismDriver):
# will fail that OVN has port with bigger revision.
return
self._ovn_update_port(context._plugin_context, port, original_port,
self._ovn_update_port(context.plugin_context, port, original_port,
retry_on_revision_mismatch=True)
self._notify_dhcp_updated(port['id'])
@ -879,9 +879,7 @@ class OVNMechanismDriver(api.MechanismDriver):
"""
port = copy.deepcopy(context.current)
port['network'] = context.network.current
# FIXME(lucasagomes): PortContext does not have a session, therefore
# we need to use the _plugin_context attribute.
self._ovn_client.delete_port(context._plugin_context, port['id'],
self._ovn_client.delete_port(context.plugin_context, port['id'],
port_object=port)
def bind_port(self, context):

View File

@ -867,7 +867,7 @@ class MechanismManager(stevedore.named.NamedExtensionManager):
segment = context._new_bound_segment
if segment:
pbl_obj = ports.PortBindingLevel(
context._plugin_context,
context.plugin_context,
port_id=port_id,
host=context.host,
level=level,

View File

@ -511,7 +511,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
def _clear_port_binding(self, mech_context, binding, port, original_host):
binding.vif_type = portbindings.VIF_TYPE_UNBOUND
binding.vif_details = ''
db.clear_binding_levels(mech_context._plugin_context, port['id'],
db.clear_binding_levels(mech_context.plugin_context, port['id'],
original_host)
mech_context._clear_binding_levels()
@ -547,7 +547,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
return changes, original_host
def _process_port_binding(self, mech_context, attrs):
plugin_context = mech_context._plugin_context
plugin_context = mech_context.plugin_context
binding = mech_context._binding
port = mech_context.current
changes, original_host = self._process_port_binding_attributes(binding,
@ -559,7 +559,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
original_host)
port['status'] = const.PORT_STATUS_DOWN
super(Ml2Plugin, self).update_port(
mech_context._plugin_context, port['id'],
mech_context.plugin_context, port['id'],
{port_def.RESOURCE_NAME: {'status': const.PORT_STATUS_DOWN}})
if port['device_owner'] == const.DEVICE_OWNER_DVR_INTERFACE:
@ -649,7 +649,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
)
self._update_port_dict_binding(port, new_binding)
new_context = driver_context.PortContext(
self, orig_context._plugin_context, port,
self, orig_context.plugin_context, port,
orig_context.network.current, new_binding, None,
original_port=orig_context.original)
@ -661,7 +661,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
def _commit_port_binding(self, orig_context, bind_context,
need_notify, update_binding_levels=True):
port_id = orig_context.current['id']
plugin_context = orig_context._plugin_context
plugin_context = orig_context.plugin_context
port = orig_context.current
original_port = orig_context.current
orig_binding = orig_context._binding
@ -952,7 +952,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
"port %(port_id)s on network %(network_id)s",
{'port_id': port['id'], 'network_id': network['id']})
return
self.notifier.port_update(mech_context._plugin_context, port,
self.notifier.port_update(mech_context.plugin_context, port,
segment[api.NETWORK_TYPE],
segment[api.SEGMENTATION_ID],
segment[api.PHYSICAL_NETWORK])
@ -1971,7 +1971,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
return bound_context.current
def _process_distributed_port_binding(self, mech_context, context, attrs):
plugin_context = mech_context._plugin_context
plugin_context = mech_context.plugin_context
binding = mech_context._binding
port = mech_context.current
port_id = port['id']
@ -2634,12 +2634,12 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
original_host)
port_dict['status'] = const.PORT_STATUS_DOWN
super(Ml2Plugin, self).update_port(
mech_context._plugin_context, port_dict['id'],
mech_context.plugin_context, port_dict['id'],
{port_def.RESOURCE_NAME: {'status': const.PORT_STATUS_DOWN}})
self._update_port_dict_binding(port_dict,
mech_context._binding)
mech_context._binding.persist_state_to_session(
mech_context._plugin_context.session)
mech_context.plugin_context.session)
@utils.transaction_guard
@db_api.retry_if_session_inactive()

View File

@ -336,6 +336,10 @@ class FakeNetworkContext(object):
self.fake_segments = segments
self._plugin_context = mock.MagicMock()
@property
def plugin_context(self):
return self._plugin_context
@property
def current(self):
return self.fake_network
@ -356,6 +360,10 @@ class FakeSubnetContext(object):
self.fake_network = FakeNetworkContext(network, None)
self._plugin_context = mock.MagicMock()
@property
def plugin_context(self):
return self._plugin_context
@property
def current(self):
return self.fake_subnet

View File

@ -2069,7 +2069,7 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase):
portbindings.PROFILE: {ovn_const.MIGRATING_ATTR: 'foo'},
portbindings.VIF_TYPE: portbindings.VIF_TYPE_OVS}).info()
fake_ctx = mock.Mock(current=fake_port, original=fake_port,
_plugin_context=fake_context)
plugin_context=fake_context)
self.mech_driver.update_port_postcommit(fake_ctx)
@ -2101,7 +2101,7 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase):
portbindings.VIF_TYPE: portbindings.VIF_TYPE_OVS}).info()
fake_ctx = mock.Mock(current=fake_port, original=original_fake_port,
_plugin_context=fake_context)
plugin_context=fake_context)
mock_update_port.side_effect = ovn_exceptions.RevisionConflict(
resource_id=fake_port['id'],
resource_type=ovn_const.TYPE_PORTS)
@ -2137,7 +2137,7 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase):
portbindings.VIF_TYPE: portbindings.VIF_TYPE_OVS}).info()
fake_ctx = mock.Mock(current=fake_port, original=original_fake_port,
_plugin_context=fake_context)
plugin_context=fake_context)
mock_update_port.side_effect = [
ovn_exceptions.RevisionConflict(
resource_id=fake_port['id'],
@ -2174,7 +2174,7 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase):
portbindings.VIF_TYPE: portbindings.VIF_TYPE_OVS}).info()
fake_ctx = mock.Mock(current=fake_port, original=original_fake_port,
_plugin_context=fake_context)
plugin_context=fake_context)
mock_update_port.side_effect = [
ovn_exceptions.RevisionConflict(
resource_id=fake_port['id'],
@ -2317,7 +2317,7 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase):
# Let's update the MTU to something different
network['network']['mtu'] = new_mtu
fake_ctx = mock.MagicMock(current=network['network'])
fake_ctx._plugin_context.session.is_active = False
fake_ctx.plugin_context.session.is_active = False
self.mech_driver.update_network_postcommit(fake_ctx)