Refactor code to deprecate get_interface_mac

The ip_lib code is able to get the MAC address of a device
already using the IpLinkCommand class, let's remove
get_interface_mac() since it's not necessary.

Changed all callers to use the new get_device_mac() code
and tagged get_interface_mac() for deprecation since it
could be in use by other callers outside the neutron tree.

Closes-Bug: #1630439

Change-Id: I1695d7e46efe5245eb581bd40d5420250a3bad89
This commit is contained in:
Brian Haley 2016-10-06 17:01:19 -04:00
parent eb1efc7ace
commit 0963aec572
8 changed files with 50 additions and 17 deletions

View File

@ -933,6 +933,12 @@ def device_exists_with_ips_and_mac(device_name, ip_cidrs, mac, namespace=None):
return True
def get_device_mac(device_name, namespace=None):
"""Return the MAC address of the device."""
dev = device_name[:constants.DEVICE_NAME_MAX_LEN]
return IPDevice(dev, namespace=namespace).link.address
def get_routing_table(ip_version, namespace=None):
"""Return a list of dictionaries, each representing a route.

View File

@ -149,6 +149,10 @@ def execute(cmd, process_input=None, addl_env=None,
return (_stdout, _stderr) if return_stderr else _stdout
@debtcollector.removals.remove(
version='Ocata', removal_version='Pike',
message="Use 'neutron.agent.linux.ip_lib.get_device_mac' instead."
)
def get_interface_mac(interface):
MAC_START = 18
MAC_END = 24

View File

@ -721,12 +721,12 @@ class LinuxBridgeManager(amb.CommonAgentManagerBase):
def get_agent_id(self):
if self.bridge_mappings:
mac = utils.get_interface_mac(
mac = ip_lib.get_device_mac(
list(self.bridge_mappings.values())[0])
else:
devices = ip_lib.IPWrapper().get_devices(True)
if devices:
mac = utils.get_interface_mac(devices[0].name)
mac = ip_lib.get_device_mac(devices[0].name)
else:
LOG.error(_LE("Unable to obtain MAC address for unique ID. "
"Agent terminated!"))

View File

@ -25,7 +25,6 @@ from oslo_service import service
from neutron._i18n import _LE, _LI
from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils
from neutron.agent import securitygroups_rpc as sg_rpc
from neutron.common import config as common_config
from neutron.common import topics
@ -114,7 +113,7 @@ class MacvtapManager(amb.CommonAgentManagerBase):
def get_agent_id(self):
devices = ip_lib.IPWrapper().get_devices(True)
if devices:
mac = utils.get_interface_mac(devices[0].name)
mac = ip_lib.get_device_mac(devices[0].name)
return 'macvtap%s' % mac.replace(":", "")
else:
LOG.error(_LE("Unable to obtain MAC address for unique ID. "
@ -133,7 +132,7 @@ class MacvtapManager(amb.CommonAgentManagerBase):
self.mac_device_name_mappings = dict()
for device_name in all_device_names:
if device_name.startswith(constants.MACVTAP_DEVICE_PREFIX):
mac = utils.get_interface_mac(device_name)
mac = ip_lib.get_device_mac(device_name)
self.mac_device_name_mappings[mac] = device_name
devices.add(mac)
return devices

View File

@ -16,6 +16,7 @@
import collections
import netaddr
from neutron_lib import constants
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
@ -158,6 +159,30 @@ class IpLibTestCase(IpLibTestFramework):
device.link.delete()
def test_get_device_mac(self):
attr = self.generate_device_details()
device = self.manage_device(attr)
mac_address = ip_lib.get_device_mac(attr.name,
namespace=attr.namespace)
self.assertEqual(attr.mac_address, mac_address)
device.link.delete()
def test_get_device_mac_too_long_name(self):
name = utils.get_rand_name(
max_length=constants.DEVICE_NAME_MAX_LEN + 5)
attr = self.generate_device_details(name=name)
device = self.manage_device(attr)
mac_address = ip_lib.get_device_mac(attr.name,
namespace=attr.namespace)
self.assertEqual(attr.mac_address, mac_address)
device.link.delete()
def test_get_routing_table(self):
attr = self.generate_device_details()
device = self.manage_device(attr)

View File

@ -860,8 +860,8 @@ class TestLinuxBridgeManager(base.BaseTestCase):
def test_get_agent_id_bridge_mappings(self):
lbm = get_linuxbridge_manager(BRIDGE_MAPPINGS, INTERFACE_MAPPINGS)
with mock.patch.object(utils,
"get_interface_mac",
with mock.patch.object(ip_lib,
"get_device_mac",
return_value='16:63:69:10:a0:59') as mock_gim:
agent_id = lbm.get_agent_id()
@ -881,8 +881,8 @@ class TestLinuxBridgeManager(base.BaseTestCase):
'get_devices',
return_value=devices_mock), \
mock.patch.object(
utils,
"get_interface_mac",
ip_lib,
"get_device_mac",
return_value='16:63:69:10:a0:59') as mock_gim:
agent_id = lbm.get_agent_id()

View File

@ -23,7 +23,6 @@ from oslo_config import cfg
from oslo_service import service
from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils
from neutron.common import config as common_config
from neutron.common import topics
from neutron.common import utils as n_utils
@ -119,7 +118,7 @@ class TestMacvtapManager(base.BaseTestCase):
self.mgr.mac_device_name_mappings = {'foo': 'bar'}
with mock.patch.object(os, 'listdir', return_value=listing)\
as mock_ld,\
mock.patch.object(utils, 'get_interface_mac') as mock_gdn:
mock.patch.object(ip_lib, 'get_device_mac') as mock_gdn:
mock_gdn.side_effect = ['mac0', 'mac1']
result = self.mgr.get_all_devices()
@ -136,7 +135,7 @@ class TestMacvtapManager(base.BaseTestCase):
mock_devices = [ip_lib.IPDevice('macvtap1')]
with mock.patch.object(ip_lib.IPWrapper, 'get_devices',
return_value=mock_devices),\
mock.patch.object(utils, 'get_interface_mac',
mock.patch.object(ip_lib, 'get_device_mac',
return_value='foo:bar'):
self.assertEqual('macvtapfoobar', self.mgr.get_agent_id())

View File

@ -129,7 +129,7 @@ class TestOvsNeutronAgent(object):
mock.patch.object(self.mod_agent.OVSNeutronAgent,
'setup_ancillary_bridges',
return_value=[]),\
mock.patch('neutron.agent.linux.utils.get_interface_mac',
mock.patch('neutron.agent.linux.ip_lib.get_device_mac',
return_value='00:00:00:00:00:01'),\
mock.patch(
'neutron.agent.common.ovs_lib.BaseOVS.get_bridges'),\
@ -182,7 +182,7 @@ class TestOvsNeutronAgent(object):
mock.patch.object(self.mod_agent.OVSNeutronAgent,
'setup_ancillary_bridges',
return_value=[]), \
mock.patch('neutron.agent.linux.utils.get_interface_mac',
mock.patch('neutron.agent.linux.ip_lib.get_device_mac',
return_value='00:00:00:00:00:01'), \
mock.patch(
'neutron.agent.common.ovs_lib.BaseOVS.get_bridges'), \
@ -215,7 +215,7 @@ class TestOvsNeutronAgent(object):
mock.patch.object(self.mod_agent.OVSNeutronAgent,
'setup_ancillary_bridges',
return_value=[]), \
mock.patch('neutron.agent.linux.utils.get_interface_mac',
mock.patch('neutron.agent.linux.ip_lib.get_device_mac',
return_value='00:00:00:00:00:01'), \
mock.patch(
'neutron.agent.common.ovs_lib.BaseOVS.get_bridges'), \
@ -2162,7 +2162,7 @@ class AncillaryBridgesTest(object):
with mock.patch.object(self.mod_agent.OVSNeutronAgent,
'setup_integration_br'),\
mock.patch('neutron.agent.linux.utils.get_interface_mac',
mock.patch('neutron.agent.linux.ip_lib.get_device_mac',
return_value='00:00:00:00:00:01'),\
mock.patch('neutron.agent.common.ovs_lib.BaseOVS.get_bridges',
return_value=bridges),\
@ -2271,7 +2271,7 @@ class TestOvsDvrNeutronAgent(object):
mock.patch.object(self.mod_agent.OVSNeutronAgent,
'setup_ancillary_bridges',
return_value=[]),\
mock.patch('neutron.agent.linux.utils.get_interface_mac',
mock.patch('neutron.agent.linux.ip_lib.get_device_mac',
return_value='00:00:00:00:00:01'),\
mock.patch(
'neutron.agent.common.ovs_lib.BaseOVS.get_bridges'),\