Skip to content

Commit

Permalink
Improve IPFabric matching of InterfaceTypes
Browse files Browse the repository at this point in the history
Adds regex matching of interface names for when IPFabric does not
provide a media_type.
  • Loading branch information
jmcgill298 committed Apr 4, 2024
1 parent 04b9110 commit 00968b2
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 120 deletions.
1 change: 1 addition & 0 deletions changes/414.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed IPFabric interface media matching to fall back on interface names.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def load_device_interfaces(self, device_model, interfaces, device_primary_ip, ne
if iface.get("mac")
else DEFAULT_INTERFACE_MAC,
mtu=iface.get("mtu") if iface.get("mtu") else DEFAULT_INTERFACE_MTU,
type=ipfabric_utils.convert_media_type(iface.get("media") or ""),
type=ipfabric_utils.convert_media_type(iface.get("media"), iface_name),
mgmt_only=iface.get("mgmt_only", False),
ip_address=ip_address,
subnet_mask=subnet_mask,
Expand Down
195 changes: 110 additions & 85 deletions nautobot_ssot/integrations/ipfabric/utilities/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""General utils for IPFabric."""
import re

from nautobot_ssot.integrations.ipfabric.constants import DEFAULT_INTERFACE_TYPE


Expand Down Expand Up @@ -37,99 +39,122 @@

def convert_media_type( # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements
media_type: str,
interface_name: str,
) -> str:
"""Convert provided `media_type` to value used by Nautobot.
Args:
media_type: The media type of an inteface (i.e. SFP-10GBase)
interface_name: The name of the interface with `media_type`.
Returns:
str: The corresponding represention of `media_type` in Nautobot.
"""
media_type = media_type.lower().replace("-", "")
if VIRTUAL in media_type:
return VIRTUAL
if BRIDGE in media_type:
return BRIDGE
if LAG in media_type:
return LAG

if TEN_GIG_BASE_T in media_type:
return "10gbase-t"

# Going from 10Gig to lower bandwidths to allow media that supports multiple
# bandwidths to use highest supported bandwidth
if TEN_GIG in media_type:
nautobot_media_type = "10gbase-x-"
if XFP in media_type:
nautobot_media_type += "xfp"
elif X2 in media_type:
nautobot_media_type += "x2"
elif XENPAK in media_type:
nautobot_media_type += "xenpak"
else:
nautobot_media_type += "sfpp"
return nautobot_media_type

# Flipping order of 5gig and 2.5g as both use the string 5gbase
if TWO_AND_HALF_GIG_BASE in media_type:
return "2.5gbase-t"

if FIVE_GIG_BASE in media_type:
return "5gbase-t"

if GIG_BASE in media_type or RJ45 in media_type or GIG in media_type:
nautobot_media_type = "1000base-"
if GBIC in media_type:
nautobot_media_type += "x-gbic"
elif SFP in media_type or SR in media_type or LR in media_type or SX in media_type or LX in media_type:
nautobot_media_type += "x-sfp"
else:
nautobot_media_type += "t"
return nautobot_media_type

if HUNDRED_MEG_BASE in media_type or HUNDRED_MEG in media_type:
return "100base-tx"

if TWENTY_FIVE_GIG in media_type:
return "25gbase-x-sfp28"

if FORTY_GIG in media_type:
return "40gbase-x-qsfpp"

if FIFTY_GIG in media_type:
return "50gbase-x-sfp56"

if HUNDRED_GIG in media_type:
nautobot_media_type = "100gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp28"
else:
nautobot_media_type += "cfp"
return nautobot_media_type

if TWO_HUNDRED_GIG in media_type:
nautobot_media_type = "200gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp56"
else:
nautobot_media_type += "cfp2"
return nautobot_media_type

if FOUR_HUNDRED_GIG in media_type:
nautobot_media_type = "400gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp112"
else:
nautobot_media_type += "osfp"
return nautobot_media_type

if EIGHT_HUNDRED_GIG in media_type:
nautobot_media_type = "800gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfpdd"
else:
nautobot_media_type += "osfp"
return nautobot_media_type
if media_type:
media_type = media_type.lower().replace("-", "")
if VIRTUAL in media_type:
return VIRTUAL
if BRIDGE in media_type:
return BRIDGE
if LAG in media_type:
return LAG

if TEN_GIG_BASE_T in media_type:
return "10gbase-t"

# Going from 10Gig to lower bandwidths to allow media that supports multiple
# bandwidths to use highest supported bandwidth
if TEN_GIG in media_type:
nautobot_media_type = "10gbase-x-"
if XFP in media_type:
nautobot_media_type += "xfp"
elif X2 in media_type:
nautobot_media_type += "x2"
elif XENPAK in media_type:
nautobot_media_type += "xenpak"
else:
nautobot_media_type += "sfpp"
return nautobot_media_type

# Flipping order of 5gig and 2.5g as both use the string 5gbase
if TWO_AND_HALF_GIG_BASE in media_type:
return "2.5gbase-t"

if FIVE_GIG_BASE in media_type:
return "5gbase-t"

if GIG_BASE in media_type or RJ45 in media_type or GIG in media_type:
nautobot_media_type = "1000base-"
if GBIC in media_type:
nautobot_media_type += "x-gbic"
elif SFP in media_type or SR in media_type or LR in media_type or SX in media_type or LX in media_type:
nautobot_media_type += "x-sfp"
else:
nautobot_media_type += "t"
return nautobot_media_type

if HUNDRED_MEG_BASE in media_type or HUNDRED_MEG in media_type:
return "100base-tx"

if TWENTY_FIVE_GIG in media_type:
return "25gbase-x-sfp28"

if FORTY_GIG in media_type:
return "40gbase-x-qsfpp"

if FIFTY_GIG in media_type:
return "50gbase-x-sfp56"

if HUNDRED_GIG in media_type:
nautobot_media_type = "100gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp28"
else:
nautobot_media_type += "cfp"
return nautobot_media_type

if TWO_HUNDRED_GIG in media_type:
nautobot_media_type = "200gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp56"
else:
nautobot_media_type += "cfp2"
return nautobot_media_type

if FOUR_HUNDRED_GIG in media_type:
nautobot_media_type = "400gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfp112"
else:
nautobot_media_type += "osfp"
return nautobot_media_type

if EIGHT_HUNDRED_GIG in media_type:
nautobot_media_type = "800gbase-x-"
if QSFP in media_type:
nautobot_media_type += "qsfpdd"
else:
nautobot_media_type += "osfp"
return nautobot_media_type
else:
interface_name = interface_name.lower()
regex_to_type = (
(r"po(rt-?channel)?\d", "lag"),
(r"vl(an)?\d", "virtual"),
(r"lo(opback)?\d", "virtual"),
(r"tu(nnel)?\d", "virtual"),
(r"vx(lan)?\d", "virtual"),
(r"fa(stethernet)?\d", "100base-tx"),
(r"gi(gabitethernet)?\d", "1000base-t"),
(r"te(ngigabitethernet)?\d", "10gbase-x-sfpp"),
(r"twentyfivegigabitethernet\d", "25gbase-x-sfp28"),
(r"fo(rtygigabitethernet)?\d", "40gbase-x-qsfpp"),
(r"fi(ftygigabitethernet)?\d", "50gbase-x-sfp56"),
(r"hu(ndredgigabitethernet)?\d", "100gbase-x-qsfp28"),
(r"twohundredgigabitethernet\d", "200gbase-x-qsfp56"),
)
for regex, iface_type in regex_to_type:
if re.match(regex, interface_name):
return iface_type

return DEFAULT_INTERFACE_TYPE
Loading

0 comments on commit 00968b2

Please sign in to comment.