-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig_generator.py
More file actions
1312 lines (1104 loc) · 50.2 KB
/
config_generator.py
File metadata and controls
1312 lines (1104 loc) · 50.2 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
"""Configuration generation logic for SONiC."""
import copy
import ipaddress
import json
import os
import re
from loguru import logger
from osism import utils
from osism.tasks.conductor.netbox import (
get_device_interface_ips,
get_device_loopbacks,
get_device_oob_ip,
get_device_vlans,
)
from .bgp import calculate_local_asn_from_ipv4
from .device import get_device_platform, get_device_hostname, get_device_mac_address
from .interface import (
get_port_config,
get_speed_from_port_type,
convert_netbox_interface_to_sonic,
convert_sonic_interface_to_alias,
detect_breakout_ports,
detect_port_channels,
clear_port_config_cache,
)
from .connections import (
get_connected_interfaces,
get_connected_device_for_sonic_interface,
)
from .cache import get_cached_device_interfaces
# Global cache for NTP servers to avoid multiple queries
_ntp_servers_cache = None
def natural_sort_key(port_name):
"""Extract numeric part from port name for natural sorting."""
match = re.search(r"(\d+)", port_name)
return int(match.group(1)) if match else 0
def generate_sonic_config(device, hwsku, device_as_mapping=None):
"""Generate minimal SONiC config.json for a device.
Args:
device: NetBox device object
hwsku: Hardware SKU name
device_as_mapping: Dict mapping device IDs to pre-calculated AS numbers for spine/superspine groups
Returns:
dict: Minimal SONiC configuration dictionary
"""
# Get port configuration for the HWSKU
port_config = get_port_config(hwsku)
# Get port channel configuration from NetBox first (needed by get_connected_interfaces)
portchannel_info = detect_port_channels(device)
# Get connected interfaces to determine admin_status
connected_interfaces, connected_portchannels = get_connected_interfaces(
device, portchannel_info
)
# Get OOB IP for management interface
oob_ip_result = get_device_oob_ip(device)
# Get VLAN configuration from NetBox
vlan_info = get_device_vlans(device)
# Get Loopback configuration from NetBox
loopback_info = get_device_loopbacks(device)
# Get interface IP addresses from NetBox
interface_ips = get_device_interface_ips(device)
# Get IPv4 addresses from transfer role prefixes
transfer_ips = _get_transfer_role_ipv4_addresses(device)
# Get breakout port configuration from NetBox
breakout_info = detect_breakout_ports(device)
# Get all interfaces from NetBox with their speeds and types
netbox_interfaces = {}
try:
interfaces = get_cached_device_interfaces(device.id)
for interface in interfaces:
# Convert NetBox interface name to SONiC format for lookup
interface_speed = getattr(interface, "speed", None)
# If speed is not set, try to get it from port type
if not interface_speed and hasattr(interface, "type") and interface.type:
interface_speed = get_speed_from_port_type(interface.type.value)
sonic_name = convert_netbox_interface_to_sonic(interface, device)
netbox_interfaces[sonic_name] = {
"speed": interface_speed,
"type": (
getattr(interface.type, "value", None)
if hasattr(interface, "type") and interface.type
else None
),
"netbox_name": interface.name,
}
except Exception as e:
logger.warning(f"Could not get interface details from NetBox: {e}")
# Get device metadata using helper functions
platform = get_device_platform(device, hwsku)
hostname = get_device_hostname(device)
mac_address = get_device_mac_address(device)
# Try to load base configuration from /etc/sonic/config_db.json
# Always start with a fresh, empty configuration for each device
base_config_path = "/etc/sonic/config_db.json"
config = {}
try:
if os.path.exists(base_config_path):
with open(base_config_path, "r") as f:
base_config = json.load(f)
# Create a deep copy to ensure no cross-device contamination
config = copy.deepcopy(base_config)
logger.info(
f"Loaded fresh base configuration from {base_config_path} for device {device.name}"
)
else:
logger.debug(
f"Base config file {base_config_path} not found, starting with empty config for device {device.name}"
)
except Exception as e:
logger.warning(
f"Could not load base configuration from {base_config_path} for device {device.name}: {e}"
)
# Ensure we start fresh even on error
config = {}
# Update DEVICE_METADATA with NetBox information
if "localhost" not in config["DEVICE_METADATA"]:
config["DEVICE_METADATA"]["localhost"] = {}
config["DEVICE_METADATA"]["localhost"].update(
{
"hostname": hostname,
"hwsku": hwsku,
"platform": platform,
"mac": mac_address,
}
)
# Add BGP_GLOBALS configuration with router_id set to primary IP address
primary_ip = None
if device.primary_ip4:
primary_ip = str(device.primary_ip4.address).split("/")[0]
elif device.primary_ip6:
primary_ip = str(device.primary_ip6.address).split("/")[0]
if primary_ip:
if "default" not in config["BGP_GLOBALS"]:
config["BGP_GLOBALS"]["default"] = {}
config["BGP_GLOBALS"]["default"]["router_id"] = primary_ip
# Calculate and add local_asn from router_id (only for IPv4)
if device.primary_ip4:
try:
# Check if device is in a spine/superspine group with pre-calculated AS
if device_as_mapping and device.id in device_as_mapping:
local_asn = device_as_mapping[device.id]
logger.debug(
f"Using group-calculated AS {local_asn} for spine/superspine device {device.name}"
)
else:
# Use normal AS calculation for leaf switches and non-grouped devices
local_asn = calculate_local_asn_from_ipv4(primary_ip)
config["BGP_GLOBALS"]["default"]["local_asn"] = str(local_asn)
except ValueError as e:
logger.warning(
f"Could not calculate local ASN for device {device.name}: {e}"
)
# Add port configurations
_add_port_configurations(
config,
port_config,
connected_interfaces,
portchannel_info,
breakout_info,
netbox_interfaces,
vlan_info,
device,
)
# Add interface configurations
_add_interface_configurations(
config,
connected_interfaces,
portchannel_info,
interface_ips,
netbox_interfaces,
device,
)
# Add BGP configurations
_add_bgp_configurations(
config,
connected_interfaces,
connected_portchannels,
portchannel_info,
device,
device_as_mapping,
interface_ips,
netbox_interfaces,
transfer_ips,
)
# Add BFD configuration for BGP neighbors
_add_bfd_configurations(
config,
connected_interfaces,
connected_portchannels,
portchannel_info,
interface_ips,
netbox_interfaces,
transfer_ips,
)
# Add NTP server configuration (device-specific)
_add_ntp_configuration(config, device)
# Add DNS server configuration (device-specific)
_add_dns_configuration(config, device)
# Add VLAN configuration
_add_vlan_configuration(config, vlan_info, netbox_interfaces, device)
# Add Loopback configuration
_add_loopback_configuration(config, loopback_info)
# Add management interface configuration
if oob_ip_result:
oob_ip, prefix_len = oob_ip_result
config["MGMT_INTERFACE"]["eth0"] = {"admin_status": "up"}
config["MGMT_INTERFACE"][f"eth0|{oob_ip}/{prefix_len}"] = {}
# Add breakout configuration
if breakout_info["breakout_cfgs"]:
config["BREAKOUT_CFG"].update(breakout_info["breakout_cfgs"])
if breakout_info["breakout_ports"]:
config["BREAKOUT_PORTS"].update(breakout_info["breakout_ports"])
# Add port channel configuration
_add_portchannel_configuration(config, portchannel_info)
return config
def _add_port_configurations(
config,
port_config,
connected_interfaces,
portchannel_info,
breakout_info,
netbox_interfaces,
vlan_info,
device,
):
"""Add port configurations to config."""
# Sort ports naturally (Ethernet0, Ethernet4, Ethernet8, ...)
sorted_ports = sorted(port_config.keys(), key=natural_sort_key)
for port_name in sorted_ports:
port_info = port_config[port_name]
# Skip master ports that have breakout configurations
# These will be replaced by their individual breakout ports
if port_name in breakout_info["breakout_cfgs"]:
logger.debug(
f"Skipping master port {port_name} - has breakout configuration"
)
continue
# Set admin_status to "up" if port is connected or is a port channel member, otherwise "down"
admin_status = (
"up"
if (
port_name in connected_interfaces
or port_name in portchannel_info["member_mapping"]
)
else "down"
)
# Check if this port is a breakout port and adjust speed and lanes accordingly
port_speed = port_info["speed"]
port_lanes = port_info["lanes"]
# Override with NetBox data if available and hardware config has no speed
if port_name in netbox_interfaces:
netbox_speed = netbox_interfaces[port_name]["speed"]
if netbox_speed and (not port_speed or port_speed == "0"):
logger.info(
f"Using NetBox speed {netbox_speed} for port {port_name} (hardware config had: {port_speed})"
)
port_speed = str(netbox_speed)
if port_name in breakout_info["breakout_ports"]:
# Get the master port to determine original speed and lanes
master_port = breakout_info["breakout_ports"][port_name]["master"]
# Override with individual breakout port speed from NetBox if available
if port_name in netbox_interfaces and netbox_interfaces[port_name]["speed"]:
port_speed = str(netbox_interfaces[port_name]["speed"])
logger.debug(
f"Using NetBox speed {port_speed} for breakout port {port_name}"
)
elif master_port in breakout_info["breakout_cfgs"]:
# Fallback to extracting speed from breakout mode
brkout_mode = breakout_info["breakout_cfgs"][master_port]["brkout_mode"]
if "25G" in brkout_mode:
port_speed = "25000"
elif "50G" in brkout_mode:
port_speed = "50000"
elif "100G" in brkout_mode:
port_speed = "100000"
elif "200G" in brkout_mode:
port_speed = "200000"
# Calculate individual lane for this breakout port
port_lanes = _calculate_breakout_port_lane(
port_name, master_port, port_config
)
# Generate correct alias based on port name and speed
interface_speed = int(port_speed) if port_speed else None
is_breakout_port = port_name in breakout_info["breakout_ports"]
correct_alias = convert_sonic_interface_to_alias(
port_name, interface_speed, is_breakout_port, port_config
)
# Use master port index for breakout ports
port_index = port_info["index"]
if is_breakout_port:
master_port = breakout_info["breakout_ports"][port_name]["master"]
if master_port in port_config:
port_index = port_config[master_port]["index"]
port_data = {
"admin_status": admin_status,
"alias": correct_alias,
"index": port_index,
"lanes": port_lanes,
"speed": port_speed,
"mtu": "9100",
"adv_speeds": "all",
"autoneg": "off",
"link_training": "off",
"unreliable_los": "auto",
}
# Add valid_speeds if available in port_info
if "valid_speeds" in port_info:
port_data["valid_speeds"] = port_info["valid_speeds"]
# Override valid_speeds for breakout ports based on their individual speed
if port_name in breakout_info["breakout_ports"]:
# For breakout ports, set valid_speeds based on the port's speed
breakout_valid_speeds = _get_breakout_port_valid_speeds(port_speed)
if breakout_valid_speeds:
port_data["valid_speeds"] = breakout_valid_speeds
config["PORT"][port_name] = port_data
# Add all breakout ports (since master ports were skipped above)
_add_missing_breakout_ports(
config,
breakout_info,
port_config,
connected_interfaces,
portchannel_info,
netbox_interfaces,
)
# Add tagged VLANs to PORT configuration
_add_tagged_vlans_to_ports(config, vlan_info, netbox_interfaces, device)
def _get_breakout_port_valid_speeds(port_speed):
"""Get valid speeds for a breakout port based on its configured speed."""
if not port_speed:
return None
speed_int = int(port_speed)
if speed_int == 25000:
return "25000,10000,1000"
elif speed_int == 50000:
return "50000,25000,10000,1000"
elif speed_int == 100000:
return "100000,50000,25000,10000,1000"
elif speed_int == 200000:
return "200000,100000,50000,25000,10000,1000"
else:
# For other speeds, include common lower speeds
return f"{port_speed},10000,1000"
def _calculate_breakout_port_lane(port_name, master_port, port_config):
"""Calculate individual lane for a breakout port."""
# Get master port's lanes from port_config
if master_port in port_config:
master_lanes = port_config[master_port]["lanes"]
# Parse lane range (e.g., "1,2,3,4" or "1-4")
if "," in master_lanes:
lanes_list = [int(lane.strip()) for lane in master_lanes.split(",")]
elif "-" in master_lanes:
start, end = map(int, master_lanes.split("-"))
lanes_list = list(range(start, end + 1))
else:
# Single lane or simple number
lanes_list = [int(master_lanes)]
# Calculate which lane this breakout port should use
port_match = re.match(r"Ethernet(\d+)", port_name)
if port_match:
sonic_port_num = int(port_match.group(1))
master_port_match = re.match(r"Ethernet(\d+)", master_port)
if master_port_match:
master_port_num = int(master_port_match.group(1))
# Calculate subport index (0, 1, 2, 3 for 4x breakout)
subport_index = sonic_port_num - master_port_num
if 0 <= subport_index < len(lanes_list):
return str(lanes_list[subport_index])
else:
logger.warning(
f"Breakout port {port_name}: subport_index {subport_index} out of range for lanes_list {lanes_list}"
)
return "1" # Default fallback
def _add_missing_breakout_ports(
config,
breakout_info,
port_config,
connected_interfaces,
portchannel_info,
netbox_interfaces,
):
"""Add all breakout ports to config (master ports are skipped in main loop)."""
for port_name in breakout_info["breakout_ports"]:
if port_name not in config["PORT"]:
# Get the master port to determine configuration
master_port = breakout_info["breakout_ports"][port_name]["master"]
# Override with individual breakout port speed from NetBox if available
if port_name in netbox_interfaces and netbox_interfaces[port_name]["speed"]:
port_speed = str(netbox_interfaces[port_name]["speed"])
logger.debug(
f"Using NetBox speed {port_speed} for missing breakout port {port_name}"
)
elif master_port in breakout_info["breakout_cfgs"]:
# Fallback to extracting speed from breakout mode
brkout_mode = breakout_info["breakout_cfgs"][master_port]["brkout_mode"]
if "25G" in brkout_mode:
port_speed = "25000"
elif "50G" in brkout_mode:
port_speed = "50000"
elif "100G" in brkout_mode:
port_speed = "100000"
elif "200G" in brkout_mode:
port_speed = "200000"
else:
port_speed = "25000" # Default fallback
else:
port_speed = "25000" # Default fallback
# Set admin_status based on connection or port channel membership
admin_status = (
"up"
if (
port_name in connected_interfaces
or port_name in portchannel_info["member_mapping"]
)
else "down"
)
# Generate correct alias (breakout port always gets subport notation)
interface_speed = int(port_speed)
correct_alias = convert_sonic_interface_to_alias(
port_name, interface_speed, is_breakout=True, port_config=port_config
)
# Use master port index for breakout ports
port_index = "1" # Default fallback
if master_port in port_config:
port_index = port_config[master_port]["index"]
# Calculate individual lane for this breakout port
port_lanes = _calculate_breakout_port_lane(
port_name, master_port, port_config
)
port_data = {
"admin_status": admin_status,
"alias": correct_alias,
"index": port_index,
"lanes": port_lanes,
"speed": port_speed,
"mtu": "9100",
"adv_speeds": "all",
"autoneg": "off",
"link_training": "off",
"unreliable_los": "auto",
}
# For breakout ports, check if master port has valid_speeds
if (
master_port in port_config
and "valid_speeds" in port_config[master_port]
):
port_data["valid_speeds"] = port_config[master_port]["valid_speeds"]
# Override valid_speeds for breakout ports based on their individual speed
breakout_valid_speeds = _get_breakout_port_valid_speeds(port_speed)
if breakout_valid_speeds:
port_data["valid_speeds"] = breakout_valid_speeds
config["PORT"][port_name] = port_data
def _add_tagged_vlans_to_ports(config, vlan_info, netbox_interfaces, device):
"""Add tagged VLANs to PORT configuration."""
# Build a mapping of ports to their tagged VLANs
port_tagged_vlans = {}
for vid, members in vlan_info["vlan_members"].items():
for netbox_interface_name, tagging_mode in members.items():
# Convert NetBox interface name to SONiC format
# Try to find speed from netbox_interfaces
speed = None
for sonic_name, iface_info in netbox_interfaces.items():
if iface_info["netbox_name"] == netbox_interface_name:
speed = iface_info["speed"]
break
sonic_interface_name = convert_netbox_interface_to_sonic(
netbox_interface_name, device
)
# Only add if this is a tagged VLAN (not untagged)
if tagging_mode == "tagged":
if sonic_interface_name not in port_tagged_vlans:
port_tagged_vlans[sonic_interface_name] = []
port_tagged_vlans[sonic_interface_name].append(str(vid))
# Update PORT configuration with tagged VLANs
for port_name in config["PORT"]:
if port_name in port_tagged_vlans:
# Sort the VLAN IDs numerically for consistent ordering
tagged_vlans = sorted(port_tagged_vlans[port_name], key=int)
config["PORT"][port_name]["tagged_vlans"] = tagged_vlans
def _add_interface_configurations(
config,
connected_interfaces,
portchannel_info,
interface_ips,
netbox_interfaces,
device,
):
"""Add INTERFACE configuration for connected interfaces."""
for port_name in config["PORT"]:
# Check if this port is in the connected interfaces set and not a port channel member
if (
port_name in connected_interfaces
and port_name not in portchannel_info["member_mapping"]
):
# Find the NetBox interface name for this SONiC port
netbox_interface_name = None
if port_name in netbox_interfaces:
netbox_interface_name = netbox_interfaces[port_name]["netbox_name"]
# Check if this interface has an IPv4 address assigned
ipv4_address = None
if netbox_interface_name and netbox_interface_name in interface_ips:
ipv4_address = interface_ips[netbox_interface_name]
logger.info(
f"Interface {port_name} ({netbox_interface_name}) has IPv4 address: {ipv4_address}"
)
if ipv4_address:
# If IPv4 address is available, configure the interface with it
# Add base interface entry (similar to VLAN_INTERFACE and LOOPBACK_INTERFACE patterns)
config["INTERFACE"][port_name] = {}
# Add IP address suffixed entry with scope and family parameters
config["INTERFACE"][f"{port_name}|{ipv4_address}"] = {
"scope": "global",
"family": "IPv4",
}
logger.info(
f"Configured interface {port_name} with IPv4 address {ipv4_address}"
)
else:
# Add interface to INTERFACE section with ipv6_use_link_local_only enabled
config["INTERFACE"][port_name] = {"ipv6_use_link_local_only": "enable"}
logger.debug(
f"Configured interface {port_name} with IPv6 link-local only"
)
def _get_transfer_role_ipv4_addresses(device):
"""Get IPv4 addresses from IP prefixes with 'transfer' role.
Args:
device: NetBox device object
Returns:
dict: Dictionary mapping interface names to their transfer role IPv4 addresses
{
'interface_name': 'ip_address/prefix_length',
...
}
"""
transfer_ips = {}
try:
# Get all interfaces for the device
interfaces = list(utils.nb.dcim.interfaces.filter(device_id=device.id))
for interface in interfaces:
# Skip management interfaces and virtual interfaces
if interface.mgmt_only or (
hasattr(interface, "type")
and interface.type
and interface.type.value == "virtual"
):
continue
# Get IP addresses assigned to this interface
ip_addresses = utils.nb.ipam.ip_addresses.filter(
assigned_object_id=interface.id,
)
for ip_addr in ip_addresses:
if ip_addr.address:
try:
ip_obj = ipaddress.ip_interface(ip_addr.address)
if ip_obj.version == 4:
# Check if this IP belongs to a prefix with 'transfer' role
# Query for the prefix this IP belongs to
prefixes = utils.nb.ipam.prefixes.filter(
contains=str(ip_obj.ip)
)
for prefix in prefixes:
# Check if prefix has role and it's 'transfer'
if hasattr(prefix, "role") and prefix.role:
if prefix.role.slug == "transfer":
transfer_ips[interface.name] = ip_addr.address
logger.debug(
f"Found transfer role IPv4 {ip_addr.address} on interface {interface.name} of device {device.name}"
)
break
# Break after first IPv4 found (transfer or not)
if interface.name in transfer_ips:
break
except (ValueError, ipaddress.AddressValueError):
# Skip invalid IP addresses
continue
except Exception as e:
logger.warning(
f"Failed to get transfer role IPv4 addresses for device {device.name}: {e}"
)
return transfer_ips
def _has_direct_ipv4_address(port_name, interface_ips, netbox_interfaces):
"""Check if an interface has a direct IPv4 address assigned.
Args:
port_name: SONiC interface name (e.g., "Ethernet0")
interface_ips: Dict mapping NetBox interface names to IPv4 addresses
netbox_interfaces: Dict mapping SONiC names to NetBox interface info
Returns:
bool: True if interface has a direct IPv4 address, False otherwise
"""
if not interface_ips or not netbox_interfaces:
return False
if port_name in netbox_interfaces:
netbox_interface_name = netbox_interfaces[port_name]["netbox_name"]
return netbox_interface_name in interface_ips
return False
def _has_transfer_role_ipv4(port_name, transfer_ips, netbox_interfaces):
"""Check if an interface has an IPv4 from a transfer role prefix.
Args:
port_name: SONiC interface name (e.g., "Ethernet0")
transfer_ips: Dict mapping NetBox interface names to transfer role IPv4 addresses
netbox_interfaces: Dict mapping SONiC names to NetBox interface info
Returns:
bool: True if interface has a transfer role IPv4 address, False otherwise
"""
if not transfer_ips or not netbox_interfaces:
return False
if port_name in netbox_interfaces:
netbox_interface_name = netbox_interfaces[port_name]["netbox_name"]
return netbox_interface_name in transfer_ips
return False
def _add_bgp_configurations(
config,
connected_interfaces,
connected_portchannels,
portchannel_info,
device,
device_as_mapping=None,
interface_ips=None,
netbox_interfaces=None,
transfer_ips=None,
):
"""Add BGP configurations.
Args:
config: Configuration dictionary to update
connected_interfaces: Set of connected interface names
connected_portchannels: Set of connected port channel names
portchannel_info: Port channel membership information
device: NetBox device object
device_as_mapping: Mapping of device names to AS numbers
interface_ips: Dict of direct IPv4 addresses on interfaces
netbox_interfaces: Dict mapping SONiC names to NetBox interface info
transfer_ips: Dict of IPv4 addresses from transfer role prefixes
"""
# Add BGP_NEIGHBOR_AF configuration for connected interfaces
for port_name in config["PORT"]:
has_direct_ipv4 = _has_direct_ipv4_address(
port_name, interface_ips, netbox_interfaces
)
has_transfer_ipv4 = _has_transfer_role_ipv4(
port_name, transfer_ips, netbox_interfaces
)
if (
port_name in connected_interfaces
and port_name not in portchannel_info["member_mapping"]
):
# Include interfaces with transfer role IPv4 or no direct IPv4
if has_transfer_ipv4 or not has_direct_ipv4:
ipv4_key = f"default|{port_name}|ipv4_unicast"
config["BGP_NEIGHBOR_AF"][ipv4_key] = {"admin_status": "true"}
# Only add ipv6_unicast if v6only would be true (no transfer role IPv4)
if not has_transfer_ipv4:
ipv6_key = f"default|{port_name}|ipv6_unicast"
config["BGP_NEIGHBOR_AF"][ipv6_key] = {"admin_status": "true"}
logger.debug(
f"Added BGP_NEIGHBOR_AF with ipv4_unicast and ipv6_unicast for interface {port_name} (no direct IPv4)"
)
else:
logger.debug(
f"Added BGP_NEIGHBOR_AF with ipv4_unicast only for interface {port_name} (transfer role IPv4, v6only=false)"
)
elif has_direct_ipv4 and not has_transfer_ipv4:
logger.info(
f"Excluding interface {port_name} from BGP detection (has direct IPv4 address, not transfer role)"
)
# Add BGP_NEIGHBOR_AF configuration for connected port channels
for pc_name in connected_portchannels:
ipv4_key = f"default|{pc_name}|ipv4_unicast"
ipv6_key = f"default|{pc_name}|ipv6_unicast"
config["BGP_NEIGHBOR_AF"][ipv4_key] = {"admin_status": "true"}
config["BGP_NEIGHBOR_AF"][ipv6_key] = {"admin_status": "true"}
# Add BGP_NEIGHBOR configuration for connected interfaces
for port_name in config["PORT"]:
has_direct_ipv4 = _has_direct_ipv4_address(
port_name, interface_ips, netbox_interfaces
)
has_transfer_ipv4 = _has_transfer_role_ipv4(
port_name, transfer_ips, netbox_interfaces
)
if (
port_name in connected_interfaces
and port_name not in portchannel_info["member_mapping"]
):
# Include interfaces with transfer role IPv4 or no direct IPv4
if has_transfer_ipv4 or not has_direct_ipv4:
neighbor_key = f"default|{port_name}"
# Determine peer_type based on connected device AS
peer_type = "external" # Default
connected_device = get_connected_device_for_sonic_interface(
device, port_name
)
if connected_device:
peer_type = _determine_peer_type(
device, connected_device, device_as_mapping
)
# Set v6only based on whether interface has transfer role IPv4
# - Transfer role IPv4: v6only=false (dual-stack BGP)
# - No direct IPv4: v6only=true (IPv6-only BGP)
bgp_neighbor_config = {
"peer_type": peer_type,
"v6only": "false" if has_transfer_ipv4 else "true",
}
config["BGP_NEIGHBOR"][neighbor_key] = bgp_neighbor_config
if has_transfer_ipv4:
logger.debug(
f"Added BGP_NEIGHBOR for interface {port_name} (transfer role IPv4, v6only=false)"
)
else:
logger.debug(
f"Added BGP_NEIGHBOR for interface {port_name} (no direct IPv4, v6only=true)"
)
# Add BGP_NEIGHBOR configuration for connected port channels
for pc_name in connected_portchannels:
neighbor_key = f"default|{pc_name}"
# Determine peer_type based on connected device AS
peer_type = "external" # Default
connected_device = get_connected_device_for_sonic_interface(device, pc_name)
if connected_device:
peer_type = _determine_peer_type(
device, connected_device, device_as_mapping
)
config["BGP_NEIGHBOR"][neighbor_key] = {
"peer_type": peer_type,
"v6only": "true",
}
def _get_connected_device_for_interface(device, interface_name):
"""Get the connected device for a given interface name.
Args:
device: NetBox device object
interface_name: SONiC interface name (e.g., "Ethernet0")
Returns:
NetBox device object or None if not found
"""
return get_connected_device_for_sonic_interface(device, interface_name)
def _determine_peer_type(local_device, connected_device, device_as_mapping=None):
"""Determine BGP peer type (internal/external) based on AS number comparison.
Args:
local_device: Local NetBox device object
connected_device: Connected NetBox device object
device_as_mapping: Dict mapping device IDs to pre-calculated AS numbers
Returns:
str: "internal" if AS numbers match, "external" otherwise
"""
try:
# Get local AS number
local_as = None
if device_as_mapping and local_device.id in device_as_mapping:
local_as = device_as_mapping[local_device.id]
elif local_device.primary_ip4:
local_as = calculate_local_asn_from_ipv4(
str(local_device.primary_ip4.address)
)
# Get connected device AS number
connected_as = None
if device_as_mapping and connected_device.id in device_as_mapping:
connected_as = device_as_mapping[connected_device.id]
else:
# If connected device is not in device_as_mapping, check if it's a spine/superspine
# and calculate AS for its group
if connected_device.role and connected_device.role.slug in [
"spine",
"superspine",
]:
# Import here to avoid circular imports
from .bgp import calculate_minimum_as_for_group
from .connections import find_interconnected_devices
# Get all devices to find the group
all_devices = list(
utils.nb.dcim.devices.filter(role=["spine", "superspine"])
)
spine_groups = find_interconnected_devices(
all_devices, ["spine", "superspine"]
)
# Find which group the connected device belongs to
for group in spine_groups:
if any(dev.id == connected_device.id for dev in group):
connected_as = calculate_minimum_as_for_group(group)
if connected_as:
logger.debug(
f"Calculated AS {connected_as} for connected spine/superspine device {connected_device.name}"
)
break
# Fallback to calculating from IPv4 if still no AS
if not connected_as and connected_device.primary_ip4:
connected_as = calculate_local_asn_from_ipv4(
str(connected_device.primary_ip4.address)
)
# Compare AS numbers
if local_as and connected_as and local_as == connected_as:
return "internal"
else:
return "external"
except Exception as e:
logger.debug(
f"Could not determine peer type between {local_device.name} and {connected_device.name}: {e}"
)
return "external" # Default to external on error
def _add_bfd_configurations(
config,
connected_interfaces,
connected_portchannels,
portchannel_info,
interface_ips,
netbox_interfaces,
transfer_ips,
):
"""Add BFD configuration for all BGP neighbors.
This function iterates through all BGP_NEIGHBOR entries and creates
corresponding BFD_PEER_SINGLE_HOP configuration for each neighbor.
Args:
config: The configuration dictionary to update
connected_interfaces: Set of connected interface names
connected_portchannels: Set of connected port channel names
portchannel_info: Dictionary with port channel information
interface_ips: Dict mapping NetBox interface names to IPv4 addresses
netbox_interfaces: Dict mapping SONiC names to NetBox interface info
transfer_ips: Dict of IPv4 addresses from transfer role prefixes
"""
if "BFD_PEER_SINGLE_HOP" not in config:
config["BFD_PEER_SINGLE_HOP"] = {}
# Iterate through all BGP_NEIGHBOR entries
for neighbor_key in config.get("BGP_NEIGHBOR", {}):
# neighbor_key format is "default|interface_name" (e.g., "default|Ethernet0")
parts = neighbor_key.split("|")
if len(parts) == 2:
vrf = parts[0] # Usually "default"
interface_name = parts[1]
# Create BFD peer configuration key
# BFD peer key format: "default|interface_name|null"
bfd_key = f"{vrf}|{interface_name}|null"
# Add BFD configuration for this neighbor
config["BFD_PEER_SINGLE_HOP"][bfd_key] = {
"local_addr": interface_name,
"transmit_interval": "300",
"receive_interval": "300",
"detect_multiplier": "3",
"echo": "true",
}
logger.debug(
f"Added BFD configuration for BGP neighbor on interface {interface_name}"
)
def _get_metalbox_ip_for_device(device):
"""Get Metalbox IP for a SONiC device based on OOB connection.
Returns the IP address of the metalbox device interface that is connected to the
OOB switch. If VLANs are used, returns the IP of the VLAN interface where the
SONiC switch management interface (eth0) has access.
This IP is used for both NTP and DNS services.
Args:
device: SONiC device object
Returns: