Debian Codegraph @

ansible_collections/community/routeros/plugins/module_utils/_hardware_detect.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2026, Felix Ricke (@FelixRicke)
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # The data inside here is private to this collection. If you use this from outside the collection,
7 # you are on your own. There can be random changes to its format even in bugfix releases!
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12 """Hardware detection functions for community.routeros.
13
14 Each function takes an API connection object, queries the device,
15 and returns a string variant key.
16 """
17
18 # Cache: keyed by (detector_name, connection_id) to support
19 # multiple connections in the same process (unlikely but safe).
20 _detection_cache = {}
21
22
23 def _cache_key(detector_name, api):
24 """Build a cache key from the detector name and connection identity."""
25 # Use id(api) as a proxy for connection identity within one process run.
26 # This is safe because module runs are short-lived.
27 return (detector_name, id(api))
28
29
30 def get_cached_or_detect(detector_name, api):
31 """Return cached result or run detection and cache it."""
32 key = _cache_key(detector_name, api)
33 if key not in _detection_cache:
34 detector = HARDWARE_DETECTORS[detector_name]
35 _detection_cache[key] = detector(api)
36 return _detection_cache[key]
37
38
39 def clear_cache():
40 """Clear detection cache. Useful for testing."""
41 _detection_cache.clear()
42
43
44 # --- Individual detector implementations ---
45
46 def detect_switch_chip_type(api):
47 """Determine whether the switch chip uses single-entry or multi-entry semantics.
48
49 CRS1xx/2xx (e.g. QCA8519 chip):
50 /interface/ethernet/switch returns one entry, no meaningful .id,
51 no per-port sub-entries in switch port-isolation.
52
53 CRS3xx/5xx and others (e.g. MT7621, 88E6393X):
54 /interface/ethernet/switch returns entries with .id and name,
55 port-isolation has per-port entries keyed by name.
56
57 Returns:
58 'single_entry_switch' or 'multi_entry_switch'
59 """
60 try:
61 result = list(api.path('/interface/ethernet/switch'))
62
63 # Heuristic: CRS1xx/2xx have exactly one switch entry
64 # and port-isolation entries do NOT have a 'name' field
65 # CRS3xx/5xx: port-isolation entries HAVE a 'name' field
66 if len(result) == 1:
67 entry = result[0]
68 try:
69 pi_result = list(api.path('/interface/ethernet/switch/port-isolation'))
70 # Check if port-isolation entries have 'name' field
71 has_name_field = any('name' in e for e in pi_result)
72 if has_name_field:
73 return 'multi_entry_switch'
74 else:
75 return 'single_entry_switch'
76 except Exception:
77 pass
78
79 return 'multi_entry_switch'
80
81 except Exception:
82 # If detection fails, default to multi-entry (modern/common hardware)
83 return 'multi_entry_switch'
84
85
86 # --- Detector registry ---
87
88 HARDWARE_DETECTORS = {
89 'switch_chip_type': detect_switch_chip_type,
90 }