-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathprep_105_fabric_overlay.py
More file actions
158 lines (145 loc) · 10.5 KB
/
Copy pathprep_105_fabric_overlay.py
File metadata and controls
158 lines (145 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# SPDX-License-Identifier: MIT
class PreparePlugin:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.keys = []
def prepare(self):
data_model = self.kwargs['results']['model_extended']
# We don't have switches for Multisite fabrics so need special handling
if data_model['vxlan']['fabric']['type'] in ('MSD', 'MCFG'):
switches = []
else:
switches = data_model['vxlan']['topology']['switches']
if data_model['vxlan']['fabric']['type'] in ('VXLAN_EVPN', 'eBGP_VXLAN'):
# Rebuild sm_data['vxlan']['overlay']['vrf_attach_groups'] into
# a structure that is easier to use.
vrf_grp_name_list = []
data_model['vxlan']['overlay']['vrf_attach_groups_dict'] = {}
for grp in data_model['vxlan']['overlay']['vrf_attach_groups']:
data_model['vxlan']['overlay']['vrf_attach_groups_dict'][grp['name']] = []
vrf_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['overlay']['vrf_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['overlay']['vrf_attach_groups_dict'][grp['name']]:
if any(sw['name'] == switch['hostname'] for sw in switches):
found_switch = next((item for item in switches if item["name"] == switch['hostname']))
if found_switch.get('management').get('management_ipv4_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv4_address']
elif found_switch.get('management').get('management_ipv6_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv6_address']
# Remove vrf_attach_group from vrf if the group_name is not defined
for vrf in data_model['vxlan']['overlay']['vrfs']:
if 'vrf_attach_group' in vrf:
if vrf.get('vrf_attach_group') not in vrf_grp_name_list:
del vrf['vrf_attach_group']
# Rebuild sm_data['vxlan']['overlay']['network_attach_groups'] into
# a structure that is easier to use.
net_grp_name_list = []
data_model['vxlan']['overlay']['network_attach_groups_dict'] = {}
for grp in data_model['vxlan']['overlay']['network_attach_groups']:
data_model['vxlan']['overlay']['network_attach_groups_dict'][grp['name']] = []
net_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['overlay']['network_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['overlay']['network_attach_groups_dict'][grp['name']]:
if any(sw['name'] == switch['hostname'] for sw in switches):
found_switch = next((item for item in switches if item["name"] == switch['hostname']))
if found_switch.get('management').get('management_ipv4_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv4_address']
elif found_switch.get('management').get('management_ipv6_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv6_address']
# Remove network_attach_group or attach_groups from net if the group_name is not defined
for net in data_model['vxlan']['overlay']['networks']:
if 'network_attach_group' in net:
if net.get('network_attach_group') not in net_grp_name_list:
del net['network_attach_group']
elif 'attach_groups' in net:
for grp in net.get('attach_groups'):
if grp not in net_grp_name_list:
net['attach_groups'].remove(grp)
if net.get('attach_groups') == []:
del net['attach_groups']
if data_model['vxlan']['fabric']['type'] in ('MSD', 'MCFG'):
# Rebuild sm_data['vxlan']['multisite']['overlay']['vrf_attach_groups'] into
# a structure that is easier to use.
vrf_grp_name_list = []
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'] = {}
data_model['vxlan']['multisite']['overlay']['vrf_attach_switches_list'] = []
for grp in data_model['vxlan']['multisite']['overlay']['vrf_attach_groups']:
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']] = []
vrf_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']]:
if any(sw['name'] == switch['hostname'] for sw in switches):
found_switch = next((item for item in switches if item["name"] == switch['hostname']))
if found_switch.get('management').get('management_ipv4_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv4_address']
elif found_switch.get('management').get('management_ipv6_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv6_address']
# Append switch to a flat list of switches for cross comparison later when we query the
# MSD fabric information. We need to stop execution if the list returned by the MSD query
# does not include one of these switches.
data_model['vxlan']['multisite']['overlay']['vrf_attach_switches_list'].append(switch['hostname'])
# Remove vrf_attach_group from vrf if the group_name is not defined
for vrf in data_model['vxlan']['multisite']['overlay']['vrfs']:
if 'vrf_attach_group' in vrf:
if vrf.get('vrf_attach_group') not in vrf_grp_name_list:
del vrf['vrf_attach_group']
# Rebuild sm_data['vxlan']['overlay']['network_attach_groups'] into
# a structure that is easier to use.
net_grp_name_list = []
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'] = {}
data_model['vxlan']['multisite']['overlay']['network_attach_switches_list'] = []
for grp in data_model['vxlan']['multisite']['overlay']['network_attach_groups']:
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']] = []
net_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']]:
if any(sw['name'] == switch['hostname'] for sw in switches):
found_switch = next((item for item in switches if item["name"] == switch['hostname']))
if found_switch.get('management').get('management_ipv4_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv4_address']
elif found_switch.get('management').get('management_ipv6_address'):
switch['mgmt_ip_address'] = found_switch['management']['management_ipv6_address']
# Append switch to a flat list of switches for cross comparison later when we query the
# MSD fabric information. We need to stop execution if the list returned by the MSD query
# does not include one of these switches.
data_model['vxlan']['multisite']['overlay']['network_attach_switches_list'].append(switch['hostname'])
# Remove network_attach_group or attach_groups from net if the group_name is not defined
for net in data_model['vxlan']['multisite']['overlay']['networks']:
if 'network_attach_group' in net:
if net.get('network_attach_group') not in net_grp_name_list:
del net['network_attach_group']
elif 'attach_groups' in net:
for grp in net.get('attach_groups'):
if grp not in net_grp_name_list:
net['attach_groups'].remove(grp)
if net.get('attach_groups') == []:
del net['attach_groups']
self.kwargs['results']['model_extended'] = data_model
return self.kwargs['results']