From c2cb0f235a05518762f2200b11204705e5a1c4a6 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Tue, 25 Apr 2023 12:22:33 +0200 Subject: [PATCH] Support LLDP data coming in the new field Change-Id: Id8d6978035f81215c52ed5ab3406d7adac2c561e Depends-On: https://review.opendev.org/c/openstack/ironic-python-agent/+/881462 --- ironic_inspector/plugins/lldp_basic.py | 3 +- .../plugins/local_link_connection.py | 3 +- .../test/unit/test_plugins_lldp_basic.py | 111 ++++++++++++++++-- 3 files changed, 104 insertions(+), 13 deletions(-) diff --git a/ironic_inspector/plugins/lldp_basic.py b/ironic_inspector/plugins/lldp_basic.py index a770b292a..1c220b846 100644 --- a/ironic_inspector/plugins/lldp_basic.py +++ b/ironic_inspector/plugins/lldp_basic.py @@ -66,10 +66,11 @@ class LLDPBasicProcessingHook(base.ProcessingHook): """Process LLDP data and update all_interfaces with processed data""" inventory = utils.get_inventory(introspection_data) + lldp_raw = introspection_data.get('lldp_raw') or {} for iface in inventory['interfaces']: if_name = iface['name'] - tlvs = iface.get('lldp') + tlvs = lldp_raw.get(if_name) or iface.get('lldp') if tlvs is None: LOG.warning("No LLDP Data found for interface %s", if_name, node_info=node_info) diff --git a/ironic_inspector/plugins/local_link_connection.py b/ironic_inspector/plugins/local_link_connection.py index a9a6178a2..cb48e338f 100644 --- a/ironic_inspector/plugins/local_link_connection.py +++ b/ironic_inspector/plugins/local_link_connection.py @@ -115,6 +115,7 @@ class GenericLocalLinkConnectionHook(base.ProcessingHook): def before_update(self, introspection_data, node_info, **kwargs): """Process LLDP data and patch Ironic port local link connection""" inventory = utils.get_inventory(introspection_data) + lldp_raw = introspection_data.get('lldp_raw') or {} ironic_ports = node_info.ports() @@ -130,7 +131,7 @@ class GenericLocalLinkConnectionHook(base.ProcessingHook): node_info=node_info, data=introspection_data) continue - lldp_data = iface.get('lldp') + lldp_data = lldp_raw.get(iface['name']) or iface.get('lldp') if lldp_data is None: LOG.warning("No LLDP Data found for interface %s", mac_address, node_info=node_info, diff --git a/ironic_inspector/test/unit/test_plugins_lldp_basic.py b/ironic_inspector/test/unit/test_plugins_lldp_basic.py index 4ecaf2bea..8fee8bf89 100644 --- a/ironic_inspector/test/unit/test_plugins_lldp_basic.py +++ b/ironic_inspector/test/unit/test_plugins_lldp_basic.py @@ -41,6 +41,89 @@ class TestLLDPBasicProcessingHook(test_base.NodeTest): self.expected = {"em1": {"ip": self.ips[0], "mac": self.macs[0]}} def test_all_valid_data(self): + self.data['lldp_raw'] = { + 'em1': [ + [1, "04112233aabbcc"], # ChassisId + [2, "07373334"], # PortId + [3, "003c"], # TTL + [4, "686f737430322e6c61622e656e6720706f7274203320" + "28426f6e6429"], # PortDesc + [5, "737730312d646973742d31622d623132"], # SysName + [6, "4e6574776f726b732c20496e632e20353530302c2076657273696f" + "6e203132204275696c6420646174653a20323031342d30332d31332030" + "383a33383a33302055544320"], # SysDesc + [7, "00140014"], # SysCapabilities + [8, "0501c000020f020000000000"], # MgmtAddress + [8, "110220010db885a3000000008a2e03707334020000000000"], + [8, "0706aa11bb22cc3302000003e900"], # MgmtAddress + [127, "00120f01036c110010"], # dot3 MacPhyConfigStatus + [127, "00120f030300000002"], # dot3 LinkAggregation + [127, "00120f0405ea"], # dot3 MTU + [127, "0080c2010066"], # dot1 PortVlan + [127, "0080c20206000a"], # dot1 PortProtocolVlanId + [127, "0080c202060014"], # dot1 PortProtocolVlanId + [127, "0080c204080026424203000000"], # dot1 ProtocolIdentity + [127, "0080c203006507766c616e313031"], # dot1 VlanName + [127, "0080c203006607766c616e313032"], # dot1 VlanName + [127, "0080c203006807766c616e313034"], # dot1 VlanName + [127, "0080c2060058"], # dot1 MgmtVID + [0, ""], + ] + } + self.data['inventory']['interfaces'] = [{ + 'name': 'em1', + 'lldp': [[0, ""]] + }] + + expected = { + nv.LLDP_CAP_ENABLED_NM: ['Bridge', 'Router'], + nv.LLDP_CAP_SUPPORT_NM: ['Bridge', 'Router'], + nv.LLDP_CHASSIS_ID_NM: "11:22:33:aa:bb:cc", + nv.LLDP_MGMT_ADDRESSES_NM: ['192.0.2.15', + '2001:db8:85a3::8a2e:370:7334', + 'aa:11:bb:22:cc:33'], + nv.LLDP_PORT_LINK_AUTONEG_ENABLED_NM: True, + nv.LLDP_PORT_DESC_NM: 'host02.lab.eng port 3 (Bond)', + nv.LLDP_PORT_ID_NM: '734', + nv.LLDP_PORT_LINK_AGG_ENABLED_NM: True, + nv.LLDP_PORT_LINK_AGG_ID_NM: 2, + nv.LLDP_PORT_LINK_AGG_SUPPORT_NM: True, + nv.LLDP_PORT_MGMT_VLANID_NM: 88, + nv.LLDP_PORT_MAU_TYPE_NM: '100BASE-TX full duplex', + nv.LLDP_MTU_NM: 1514, + nv.LLDP_PORT_CAPABILITIES_NM: ['1000BASE-T fdx', + '100BASE-TX fdx', + '100BASE-TX hdx', + '10BASE-T fdx', + '10BASE-T hdx', + 'Asym and Sym PAUSE fdx'], + nv.LLDP_PORT_PROT_VLAN_ENABLED_NM: True, + nv.LLDP_PORT_PROT_VLANIDS_NM: [10, 20], + nv.LLDP_PORT_PROT_VLAN_SUPPORT_NM: True, + nv.LLDP_PORT_VLANID_NM: 102, + nv.LLDP_PORT_VLANS_NM: [{'id': 101, 'name': 'vlan101'}, + {'id': 102, 'name': 'vlan102'}, + {'id': 104, "name": 'vlan104'}], + nv.LLDP_PROTOCOL_IDENTITIES_NM: ['0026424203000000'], + nv.LLDP_SYS_DESC_NM: 'Networks, Inc. 5500, version 12' + ' Build date: 2014-03-13 08:38:30 UTC ', + nv.LLDP_SYS_NAME_NM: 'sw01-dist-1b-b12' + } + + self.hook.before_update(self.data, self.node_info) + + actual_all_int = self.data['all_interfaces'] + actual = actual_all_int['em1']['lldp_processed'] + + for name, value in expected.items(): + if name is nv.LLDP_PORT_VLANS_NM: + for d1, d2 in zip(expected[name], actual[name]): + for key, value in d1.items(): + self.assertEqual(d2[key], value) + else: + self.assertEqual(actual[name], expected[name]) + + def test_old_format(self): self.data['inventory']['interfaces'] = [{ 'name': 'em1', @@ -122,13 +205,10 @@ class TestLLDPBasicProcessingHook(test_base.NodeTest): def test_multiple_interfaces(self): self.data = { + # An artificial mix of old and new LLDP fields. 'inventory': { 'interfaces': [ - {'name': 'em1', - 'lldp': [ - [1, "04112233aabbcc"], - [2, "07373334"], - [3, "003c"]]}, + {'name': 'em1'}, {'name': 'em2', 'lldp': [ [1, "04112233aabbdd"], @@ -136,19 +216,28 @@ class TestLLDPBasicProcessingHook(test_base.NodeTest): [3, "003c"]]}, {'name': 'em3', 'lldp': [ - [1, "04112233aabbee"], - [2, "07373939"], [3, "003c"]]}], 'cpu': 1, 'disks': 1, 'memory': 1 - }, - 'all_interfaces': - { + }, + 'all_interfaces': { 'em1': {'mac': self.macs[0], 'ip': self.ips[0]}, 'em2': {'mac': self.macs[0], 'ip': self.ips[0]}, 'em3': {'mac': self.macs[0], 'ip': self.ips[0]} - } + }, + 'lldp_raw': { + 'em1': [ + [1, "04112233aabbcc"], + [2, "07373334"], + [3, "003c"] + ], + 'em3': [ + [1, "04112233aabbee"], + [2, "07373939"], + [3, "003c"] + ], + }, } expected = {"em1": {"ip": self.ips[0], "mac": self.macs[0],