diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index 768fb200f5a22fdca4af9eb153c6662194cdc4a4..7c459303d1107d5ccf78565d89dd9a7b0f1086da 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -148,6 +148,12 @@ class PORT_TYPES(Enum): UNKNOWN = auto() +class VLAN_TYPES(Enum): + ACCESS = auto() + TRUNK = auto() + VLAN = auto() + + # only used in INTERFACE_LIST_SCHEMA and sphinx docs _DASHBOARD_IDS = [d.name for d in list(BRIAN_DASHBOARDS)] @@ -155,6 +161,8 @@ _PORT_TYPES = [t.name for t in list(PORT_TYPES)] _INTERFACE_TYPES = [i.name for i in list(INTERFACE_TYPES)] +_VLAN_TYPES = [v.name for v in list(VLAN_TYPES)] + INTERFACE_LIST_SCHEMA = { '$schema': 'https://json-schema.org/draft-07/schema#', @@ -212,7 +220,8 @@ INTERFACE_LIST_SCHEMA = { 'type': 'array', 'items': {'$ref': '#/definitions/db_info'} }, - 'port_type': {'enum': _PORT_TYPES} + 'port_type': {'enum': _PORT_TYPES}, + 'vlan_type': {'enum': _VLAN_TYPES} }, 'required': [ 'router', 'name', 'description', @@ -876,6 +885,35 @@ def load_interfaces_to_poll(config, hostname=None, no_lab=False, use_next_redis= for region_key in unique_regions: region_index[region_key] = {nren for nren, reg in nren_regions.items() if reg == region_key} + ports_and_vlans = defaultdict(lambda: defaultdict(list)) + + for ifc in basic_interfaces: + router = ifc['router'] + name = ifc['name'] + + if '.' in name: + name = name.split('.')[0] + + ports_and_vlans[router][name].append(ifc) + + # Add interface_type to each interface + # It's used as a filter in the dashboard manager to determine which interfaces are trunks & has VLANs + for router, ifcs in ports_and_vlans.items(): + for base_ifc, ifc_list in ifcs.items(): + + if len(ifc_list) == 1: + ifc_list[0]['vlan_type'] = VLAN_TYPES.ACCESS.name + continue + + ifc_list.sort(key=lambda x: x['name']) + base = ifc_list.pop(0) + if base['name'] != base_ifc: + continue + base['vlan_type'] = VLAN_TYPES.TRUNK.name + + for ifc in ifc_list: + ifc['vlan_type'] = VLAN_TYPES.VLAN.name + def _get_populated_interfaces(all_interfaces): if use_next_redis: r = tasks_common.get_next_redis(config) diff --git a/test/test_worker.py b/test/test_worker.py index 452550b31d9ac8f40cb272147904b8ec3302bee6..f0fb78d112eca5d89fe331803c56c48db6426a06 100644 --- a/test/test_worker.py +++ b/test/test_worker.py @@ -811,7 +811,8 @@ def test_populate_poller_interfaces_cache( 'circuits': [], 'port_type': 'UNKNOWN', 'snmp-index': 12, - 'dashboards': [] + 'dashboards': [], + 'vlan_type': 'ACCESS' }, { 'router': 'router_a.geant.net', @@ -822,7 +823,8 @@ def test_populate_poller_interfaces_cache( 'circuits': [], 'port_type': 'ACCESS', 'snmp-index': 1, - 'dashboards': [] + 'dashboards': [], + 'vlan_type': 'TRUNK' }, { 'router': 'router_a.geant.net', @@ -838,7 +840,8 @@ def test_populate_poller_interfaces_cache( }], 'port_type': 'UNKNOWN', 'snmp-index': 1231, - 'dashboards': [] + 'dashboards': [], + 'vlan_type': 'VLAN' } ] lab_res = [ @@ -851,7 +854,8 @@ def test_populate_poller_interfaces_cache( "circuits": [], "snmp-index": 3, "dashboards": [], - "port_type": "SERVICE" + "port_type": "SERVICE", + 'vlan_type': 'ACCESS' } ] nren_regions = {