From f4671ab2b894c82693484e5f2bcb52a9c707529e Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Tue, 16 Jun 2020 23:25:26 -0700 Subject: [PATCH] daemon: refactored usages of hwaddr to mac and be consistent everywhere --- daemon/core/api/grpc/grpcutils.py | 2 +- daemon/core/nodes/base.py | 22 +++++++++++----------- daemon/core/nodes/interface.py | 14 +++++++------- daemon/core/nodes/network.py | 4 ++-- daemon/core/nodes/physical.py | 18 +++++++++--------- daemon/core/services/xorp.py | 4 ++-- daemon/core/xml/emanexml.py | 6 +++--- daemon/tests/test_nodes.py | 10 +++++----- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index 9d26e4cf..6f2911a4 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -466,7 +466,7 @@ def iface_to_proto(iface: CoreInterface) -> core_pb2.Interface: id=iface.node_id, net_id=net_id, name=iface.name, - mac=iface.hwaddr, + mac=iface.mac, mtu=iface.mtu, flow_id=iface.flow_id, ip4=ip4, diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index 97164cb6..97da63a4 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -731,9 +731,9 @@ class CoreNode(CoreNodeBase): flow_id = self.node_net_client.get_ifindex(veth.name) veth.flow_id = int(flow_id) logging.debug("interface flow index: %s - %s", veth.name, veth.flow_id) - hwaddr = self.node_net_client.get_mac(veth.name) - logging.debug("interface mac: %s - %s", veth.name, hwaddr) - veth.sethwaddr(hwaddr) + mac = self.node_net_client.get_mac(veth.name) + logging.debug("interface mac: %s - %s", veth.name, mac) + veth.set_mac(mac) try: # add network interface to the node. If unsuccessful, destroy the @@ -775,20 +775,20 @@ class CoreNode(CoreNodeBase): return iface_id - def sethwaddr(self, iface_id: int, addr: str) -> None: + def set_mac(self, iface_id: int, mac: str) -> None: """ Set hardware address for an interface. :param iface_id: id of interface to set hardware address for - :param addr: hardware address to set + :param mac: mac address to set :return: nothing :raises CoreCommandError: when a non-zero exit status occurs """ - addr = utils.validate_mac(addr) + mac = utils.validate_mac(mac) iface = self.get_iface(iface_id) - iface.sethwaddr(addr) + iface.set_mac(mac) if self.up: - self.node_net_client.device_mac(iface.name, addr) + self.node_net_client.device_mac(iface.name, mac) def addaddr(self, iface_id: int, addr: str) -> None: """ @@ -857,14 +857,14 @@ class CoreNode(CoreNodeBase): # save addresses with the interface now self.attachnet(iface_id, net) iface = self.get_iface(iface_id) - iface.sethwaddr(iface_data.mac) + iface.set_mac(iface_data.mac) for address in addresses: iface.addaddr(address) else: iface_id = self.newveth(iface_data.id, iface_data.name) self.attachnet(iface_id, net) if iface_data.mac: - self.sethwaddr(iface_id, iface_data.mac) + self.set_mac(iface_id, iface_data.mac) for address in addresses: self.addaddr(iface_id, address) self.ifup(iface_id) @@ -1094,7 +1094,7 @@ class CoreNetworkBase(NodeBase): unidirectional = 1 iface2 = InterfaceData( - id=linked_node.get_iface_id(iface), name=iface.name, mac=iface.hwaddr + id=linked_node.get_iface_id(iface), name=iface.name, mac=iface.mac ) for address in iface.addrlist: ip, _sep, mask = address.partition("/") diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index 1fb8b894..287723a7 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -53,7 +53,7 @@ class CoreInterface: self.othernet: Optional[CoreNetworkBase] = None self._params: Dict[str, float] = {} self.addrlist: List[str] = [] - self.hwaddr: Optional[str] = None + self.mac: Optional[str] = None # placeholder position hook self.poshook: Callable[[CoreInterface], None] = lambda x: None # used with EMANE @@ -150,16 +150,16 @@ class CoreInterface: """ self.addrlist.remove(addr) - def sethwaddr(self, addr: str) -> None: + def set_mac(self, mac: str) -> None: """ - Set hardware address. + Set mac address. - :param addr: hardware address to set to. + :param mac: mac address to set :return: nothing """ - if addr is not None: - addr = utils.validate_mac(addr) - self.hwaddr = addr + if mac is not None: + mac = utils.validate_mac(mac) + self.mac = mac def getparam(self, key: str) -> float: """ diff --git a/daemon/core/nodes/network.py b/daemon/core/nodes/network.py index 04d4e8f8..ef9456db 100644 --- a/daemon/core/nodes/network.py +++ b/daemon/core/nodes/network.py @@ -895,7 +895,7 @@ class PtpNet(CoreNetwork): unidirectional = 1 iface1_data = InterfaceData( - id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=iface1.hwaddr + id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=iface1.mac ) for address in iface1.addrlist: ip, _sep, mask = address.partition("/") @@ -908,7 +908,7 @@ class PtpNet(CoreNetwork): iface1.ip6_mask = mask iface2_data = InterfaceData( - id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=iface2.hwaddr + id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=iface2.mac ) for address in iface2.addrlist: ip, _sep, mask = address.partition("/") diff --git a/daemon/core/nodes/physical.py b/daemon/core/nodes/physical.py index 36bcb267..0ce8946a 100644 --- a/daemon/core/nodes/physical.py +++ b/daemon/core/nodes/physical.py @@ -65,20 +65,20 @@ class PhysicalNode(CoreNodeBase): """ return sh - def sethwaddr(self, iface_id: int, addr: str) -> None: + def set_mac(self, iface_id: int, mac: str) -> None: """ - Set hardware address for an interface. + Set mac address for an interface. :param iface_id: index of interface to set hardware address for - :param addr: hardware address to set + :param mac: mac address to set :return: nothing :raises CoreCommandError: when a non-zero exit status occurs """ - addr = utils.validate_mac(addr) + mac = utils.validate_mac(mac) iface = self.ifaces[iface_id] - iface.sethwaddr(addr) + iface.set_mac(mac) if self.up: - self.net_client.device_mac(iface.name, addr) + self.net_client.device_mac(iface.name, mac) def addaddr(self, iface_id: int, addr: str) -> None: """ @@ -111,7 +111,7 @@ class PhysicalNode(CoreNodeBase): self.net_client.delete_address(iface.name, addr) def adopt_iface( - self, iface: CoreInterface, iface_id: int, hwaddr: str, addrlist: List[str] + self, iface: CoreInterface, iface_id: int, mac: str, addrlist: List[str] ) -> None: """ When a link message is received linking this node to another part of @@ -126,8 +126,8 @@ class PhysicalNode(CoreNodeBase): self.net_client.device_down(iface.localname) self.net_client.device_name(iface.localname, iface.name) iface.localname = iface.name - if hwaddr: - self.sethwaddr(iface_id, hwaddr) + if mac: + self.set_mac(iface_id, mac) for addr in addrlist: self.addaddr(iface_id, addr) if self.up: diff --git a/daemon/core/services/xorp.py b/daemon/core/services/xorp.py index 3dfef56a..10b4fd9f 100644 --- a/daemon/core/services/xorp.py +++ b/daemon/core/services/xorp.py @@ -69,7 +69,7 @@ class XorpRtrmgr(CoreService): """ helper for adding link-local address entries (required by OSPFv3) """ - cfg = "\t address %s {\n" % iface.hwaddr.tolinklocal() + cfg = "\t address %s {\n" % iface.mac.tolinklocal() cfg += "\t\tprefix-length: 64\n" cfg += "\t }\n" return cfg @@ -305,7 +305,7 @@ class XorpRipng(XorpService): for iface in node.get_ifaces(control=False): cfg += "\tinterface %s {\n" % iface.name cfg += "\t vif %s {\n" % iface.name - cfg += "\t\taddress %s {\n" % iface.hwaddr.tolinklocal() + cfg += "\t\taddress %s {\n" % iface.mac.tolinklocal() cfg += "\t\t disable: false\n" cfg += "\t\t}\n" cfg += "\t }\n" diff --git a/daemon/core/xml/emanexml.py b/daemon/core/xml/emanexml.py index 4f511476..d716777b 100644 --- a/daemon/core/xml/emanexml.py +++ b/daemon/core/xml/emanexml.py @@ -18,7 +18,7 @@ if TYPE_CHECKING: from core.emane.emanemanager import EmaneManager from core.emane.emanemodel import EmaneModel -_hwaddr_prefix = "02:02" +_MAC_PREFIX = "02:02" def is_external(config: Dict[str, str]) -> bool: @@ -230,9 +230,9 @@ def build_node_platform_xml( platform_element.append(nem_element) node.setnemid(iface, nem_id) - macstr = _hwaddr_prefix + ":00:00:" + macstr = _MAC_PREFIX + ":00:00:" macstr += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}" - iface.sethwaddr(macstr) + iface.set_mac(macstr) # increment nem id nem_id += 1 diff --git a/daemon/tests/test_nodes.py b/daemon/tests/test_nodes.py index 327137d2..8af2e895 100644 --- a/daemon/tests/test_nodes.py +++ b/daemon/tests/test_nodes.py @@ -49,7 +49,7 @@ class TestNodes: with pytest.raises(CoreError): session.get_node(node.id, CoreNode) - def test_node_sethwaddr(self, session: Session): + def test_node_set_mac(self, session: Session): # given node = session.add_node(CoreNode) switch = session.add_node(SwitchNode) @@ -58,12 +58,12 @@ class TestNodes: mac = "aa:aa:aa:ff:ff:ff" # when - node.sethwaddr(iface.node_id, mac) + node.set_mac(iface.node_id, mac) # then - assert iface.hwaddr == mac + assert iface.mac == mac - def test_node_sethwaddr_exception(self, session: Session): + def test_node_set_mac_exception(self, session: Session): # given node = session.add_node(CoreNode) switch = session.add_node(SwitchNode) @@ -73,7 +73,7 @@ class TestNodes: # when with pytest.raises(CoreError): - node.sethwaddr(iface.node_id, mac) + node.set_mac(iface.node_id, mac) def test_node_addaddr(self, session: Session): # given