|
19 | 19 | # |
20 | 20 | # SPDX-License-Identifier: MIT |
21 | 21 |
|
| 22 | +from ansible_collections.cisco.nac_dc_vxlan.plugins.plugin_utils.helper_functions import restructure_leaf_tor_data |
| 23 | + |
| 24 | + |
22 | 25 | class PreparePlugin: |
23 | 26 | def __init__(self, **kwargs): |
24 | 27 | self.kwargs = kwargs |
25 | 28 | self.keys = [] |
26 | 29 |
|
27 | | - def _restructure_flat_to_nested(self, switches_list, topology_switches, tor_peers): |
28 | | - """Transform a flat switches list into the nested structure with TOR |
29 | | - entries under their parent leaf switches. |
30 | | -
|
31 | | - Each TOR is placed under ALL of its parent leaves that are present in the |
32 | | - attach group. If a parent leaf is not explicitly listed in the attach group, |
33 | | - a synthetic leaf entry is created automatically from topology data. |
34 | | -
|
35 | | - Args: |
36 | | - switches_list: List of switch dicts from network_attach_group |
37 | | - topology_switches: List of switch dicts from vxlan.topology.switches |
38 | | - tor_peers: List of tor_peers dicts from vxlan.topology.tor_peers |
39 | | -
|
40 | | - Returns: |
41 | | - Restructured switches list with TOR entries nested under parent leaves |
42 | | - """ |
43 | | - # Build set of TOR hostnames from topology |
44 | | - tor_hostnames = { |
45 | | - sw['name'] for sw in topology_switches if sw.get("role") == "tor" |
46 | | - } |
47 | | - |
48 | | - # Build TOR -> parent leaves mapping from tor_peers |
49 | | - # Each TOR maps to a list of its parent leaf hostnames |
50 | | - tor_to_parents = {} |
51 | | - for peer in tor_peers: |
52 | | - parent_leaves = [] |
53 | | - if peer.get("parent_leaf1"): |
54 | | - parent_leaves.append(peer['parent_leaf1']) |
55 | | - if peer.get("parent_leaf2"): |
56 | | - parent_leaves.append(peer['parent_leaf2']) |
57 | | - |
58 | | - for key in ("tor1", "tor2"): |
59 | | - tor_name = peer.get(key) |
60 | | - if tor_name: |
61 | | - tor_to_parents[tor_name] = parent_leaves |
62 | | - |
63 | | - # Build topology hostname -> switch data mapping for creating synthetic entries |
64 | | - topology_by_name = {sw['name']: sw for sw in topology_switches} |
65 | | - |
66 | | - # Separate leaf entries and TOR entries |
67 | | - leaf_entries = [] |
68 | | - tor_entries = [] |
69 | | - |
70 | | - for sw in switches_list: |
71 | | - hostname = sw.get("hostname", "") |
72 | | - if hostname in tor_hostnames: |
73 | | - tor_entries.append(sw) |
74 | | - else: |
75 | | - leaf_entries.append(sw) |
76 | | - |
77 | | - # Initialize empty 'tors' list for each leaf entry |
78 | | - for leaf in leaf_entries: |
79 | | - if "tors" not in leaf: |
80 | | - leaf['tors'] = [] |
81 | | - |
82 | | - # Build hostname -> leaf entry mapping for quick lookup |
83 | | - leaf_by_hostname = {leaf['hostname']: leaf for leaf in leaf_entries} |
84 | | - |
85 | | - # Collect all required parent leaf hostnames from TOR entries |
86 | | - # and create synthetic leaf entries for any that are not in the attach group |
87 | | - for tor_entry in tor_entries: |
88 | | - tor_hostname = tor_entry['hostname'] |
89 | | - parent_leaves = tor_to_parents.get(tor_hostname, []) |
90 | | - for parent_hostname in parent_leaves: |
91 | | - if parent_hostname not in leaf_by_hostname: |
92 | | - # Create a synthetic leaf entry from topology data |
93 | | - synthetic_leaf = {'hostname': parent_hostname, 'tors': []} |
94 | | - leaf_entries.append(synthetic_leaf) |
95 | | - leaf_by_hostname[parent_hostname] = synthetic_leaf |
96 | | - |
97 | | - # Assign each TOR to ALL of its parent leaves present in this attach group |
98 | | - for tor_entry in tor_entries: |
99 | | - tor_hostname = tor_entry['hostname'] |
100 | | - parent_leaves = tor_to_parents.get(tor_hostname, []) |
101 | | - valid_parents = [p for p in parent_leaves if p in leaf_by_hostname] |
102 | | - |
103 | | - if valid_parents: |
104 | | - for parent_hostname in valid_parents: |
105 | | - leaf_by_hostname[parent_hostname]['tors'].append(tor_entry) |
106 | | - else: |
107 | | - # No parent leaf defined in tor_peers - keep TOR as top-level entry |
108 | | - leaf_entries.append(tor_entry) |
109 | | - |
110 | | - return leaf_entries |
111 | | - |
112 | 30 | def prepare(self): |
113 | 31 | data_model = self.kwargs['results']['model_extended'] |
114 | 32 |
|
@@ -154,7 +72,7 @@ def prepare(self): |
154 | 72 | net_grp_name_list.append(grp['name']) |
155 | 73 |
|
156 | 74 | # Restructure flat TOR entries under parent leaves |
157 | | - grp['switches'] = self._restructure_flat_to_nested( |
| 75 | + grp['switches'] = restructure_leaf_tor_data( |
158 | 76 | grp['switches'], switches, tor_peers |
159 | 77 | ) |
160 | 78 |
|
|
0 commit comments