Merge "Use new enginefacade for dvr mac db"

This commit is contained in:
Jenkins 2017-01-18 12:50:24 +00:00 committed by Gerrit Code Review
commit 1111fc61a1
2 changed files with 20 additions and 15 deletions

View File

@ -68,7 +68,7 @@ def _delete_mac_associated_with_agent(resource, event, trigger, context, agent,
# until they are all deleted.
return
try:
with context.session.begin(subtransactions=True):
with db_api.context_manager.writer.using(context):
entry = (context.session.query(
dvr_models.DistributedVirtualRouterMacAddress).
filter(
@ -101,6 +101,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
self._plugin = directory.get_plugin()
return self._plugin
@db_api.context_manager.reader
def _get_dvr_mac_address_by_host(self, context, host):
try:
query = context.session.query(
@ -115,7 +116,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
@utils.transaction_guard
@db_api.retry_if_session_inactive()
def _create_dvr_mac_address_retry(self, context, host, base_mac):
with context.session.begin(subtransactions=True):
with db_api.context_manager.writer.using(context):
mac_address = utils.get_random_mac(base_mac)
dvr_mac_binding = dvr_models.DistributedVirtualRouterMacAddress(
host=host, mac_address=mac_address)
@ -139,10 +140,10 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
db_api.MAX_RETRIES)
raise ext_dvr.MacAddressGenerationFailure(host=host)
@db_api.context_manager.reader
def get_dvr_mac_address_list(self, context):
with context.session.begin(subtransactions=True):
return (context.session.
query(dvr_models.DistributedVirtualRouterMacAddress).all())
return (context.session.
query(dvr_models.DistributedVirtualRouterMacAddress).all())
def get_dvr_mac_address_by_host(self, context, host):
"""Determine the MAC for the DVR port associated to host."""

View File

@ -21,6 +21,7 @@ from neutron.callbacks import events
from neutron.callbacks import registry
from neutron.callbacks import resources
from neutron import context
from neutron.db import api as db_api
from neutron.db import dvr_mac_db
from neutron.db.models import dvr as dvr_models
from neutron.extensions import dvr
@ -42,17 +43,18 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
self.mixin = DVRDbMixinImpl(mock.Mock())
def _create_dvr_mac_entry(self, host, mac_address):
with self.ctx.session.begin(subtransactions=True):
with db_api.context_manager.writer.using(self.ctx):
entry = dvr_models.DistributedVirtualRouterMacAddress(
host=host, mac_address=mac_address)
self.ctx.session.add(entry)
def test__get_dvr_mac_address_by_host(self):
with self.ctx.session.begin(subtransactions=True):
with db_api.context_manager.writer.using(self.ctx):
entry = dvr_models.DistributedVirtualRouterMacAddress(
host='foo_host', mac_address='foo_mac_address')
self.ctx.session.add(entry)
result = self.mixin._get_dvr_mac_address_by_host(self.ctx, 'foo_host')
result = self.mixin._get_dvr_mac_address_by_host(self.ctx,
'foo_host')
self.assertEqual(entry, result)
def test__get_dvr_mac_address_by_host_not_found(self):
@ -95,13 +97,15 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
def test_mac_cleared_on_agent_delete_event(self):
plugin = directory.get_plugin()
self._create_dvr_mac_entry('host_1', 'mac_1')
self._create_dvr_mac_entry('host_2', 'mac_2')
agent = {'host': 'host_1', 'id': 'a1'}
with mock.patch.object(plugin, 'notifier') as notifier:
registry.notify(resources.AGENT, events.BEFORE_DELETE, self,
context=self.ctx, agent=agent)
mac_list = self.mixin.get_dvr_mac_address_list(self.ctx)
with db_api.context_manager.writer.using(self.ctx):
self._create_dvr_mac_entry('host_1', 'mac_1')
self._create_dvr_mac_entry('host_2', 'mac_2')
agent = {'host': 'host_1', 'id': 'a1'}
with mock.patch.object(plugin, 'notifier') as notifier:
registry.notify(resources.AGENT, events.BEFORE_DELETE, self,
context=self.ctx, agent=agent)
mac_list = self.mixin.get_dvr_mac_address_list(self.ctx)
self.assertEqual(1, len(mac_list))
self.assertEqual('host_2', mac_list[0]['host'])
notifier.dvr_mac_address_update.assert_called_once_with(