import netaddr
from oslo_config import cfg
from oslo_log import log as logging
+import oslo_messaging
from oslo_utils import uuidutils
import six
for port in network.ports:
port_device_id = getattr(port, 'device_id', None)
if port_device_id == constants.DEVICE_ID_RESERVED_DHCP_PORT:
- port = self.plugin.update_dhcp_port(
- port.id, {'port': {'network_id': network.id,
- 'device_id': device_id}})
+ try:
+ port = self.plugin.update_dhcp_port(
+ port.id, {'port': {'network_id': network.id,
+ 'device_id': device_id}})
+ except oslo_messaging.RemoteError as e:
+ if e.exc_type == exceptions.DhcpPortInUse:
+ LOG.info(_LI("Skipping DHCP port %s as it is "
+ "already in use"), port.id)
+ continue
+ raise
if port:
return port
host = kwargs.get('host')
port = kwargs.get('port')
port['id'] = kwargs.get('port_id')
+ port['port'][portbindings.HOST_ID] = host
+ plugin = manager.NeutronManager.get_plugin()
+ old_port = plugin.get_port(context, port['id'])
+ if (old_port['device_id'] != constants.DEVICE_ID_RESERVED_DHCP_PORT
+ and old_port['device_id'] !=
+ utils.get_dhcp_agent_device_id(port['port']['network_id'], host)):
+ raise n_exc.DhcpPortInUse(port_id=port['id'])
LOG.debug('Update dhcp port %(port)s '
'from %(host)s.',
{'port': port,
'host': host})
- port['port'][portbindings.HOST_ID] = host
- plugin = manager.NeutronManager.get_plugin()
return self._port_action(plugin, context, port, 'update_port')
from neutron.api.rpc.handlers import dhcp_rpc
from neutron.common import constants
from neutron.common import exceptions as n_exc
+from neutron.common import utils
from neutron.tests import base
def _fake_port_action(plugin, context, port, action):
self.assertEqual(expected_port, port)
+ self.plugin.get_port.return_value = {
+ 'device_id': constants.DEVICE_ID_RESERVED_DHCP_PORT}
self.callbacks._port_action = _fake_port_action
self.callbacks.update_dhcp_port(mock.Mock(),
host='foo_host',
port_id='foo_port_id',
port=port)
+ def test_update_reserved_dhcp_port(self):
+ port = {'port': {'network_id': 'foo_network_id',
+ 'device_owner': constants.DEVICE_OWNER_DHCP,
+ 'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]}
+ }
+ expected_port = {'port': {'network_id': 'foo_network_id',
+ 'device_owner': constants.DEVICE_OWNER_DHCP,
+ 'binding:host_id': 'foo_host',
+ 'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]
+ },
+ 'id': 'foo_port_id'
+ }
+
+ def _fake_port_action(plugin, context, port, action):
+ self.assertEqual(expected_port, port)
+
+ self.plugin.get_port.return_value = {
+ 'device_id': utils.get_dhcp_agent_device_id('foo_network_id',
+ 'foo_host')}
+ self.callbacks._port_action = _fake_port_action
+ self.callbacks.update_dhcp_port(
+ mock.Mock(), host='foo_host', port_id='foo_port_id', port=port)
+
+ self.plugin.get_port.return_value = {
+ 'device_id': 'other_id'}
+ self.assertRaises(n_exc.DhcpPortInUse,
+ self.callbacks.update_dhcp_port,
+ mock.Mock(),
+ host='foo_host',
+ port_id='foo_port_id',
+ port=port)
+
def test_update_dhcp_port(self):
port = {'port': {'network_id': 'foo_network_id',
'device_owner': constants.DEVICE_OWNER_DHCP,
},
'id': 'foo_port_id'
}
+ self.plugin.get_port.return_value = {
+ 'device_id': constants.DEVICE_ID_RESERVED_DHCP_PORT}
self.callbacks.update_dhcp_port(mock.Mock(),
host='foo_host',
port_id='foo_port_id',