Skip to content
Snippets Groups Projects
Commit 16e4cb93 authored by Neda Moeini's avatar Neda Moeini
Browse files

Fixed review suggestions.

parent 1dff1cc8
No related branches found
No related tags found
1 merge request!18Support ASN dot notation.
...@@ -252,12 +252,25 @@ def asn_to_int(asn_string: str) -> int: ...@@ -252,12 +252,25 @@ def asn_to_int(asn_string: str) -> int:
Returns: Returns:
int: ASN in integer format. int: ASN in integer format.
Raises:
ValueError: If the ASN string is not in the expected format or exceeds valid range.
""" """
if '.' in asn_string:
high_order, low_order = map(int, asn_string.split('.')) dotted_asn_pattern = re.compile(r'^(\d+)\.(\d+)$')
match = dotted_asn_pattern.match(asn_string)
if match:
high_order, low_order = map(int, match.groups())
if high_order > 0xffff or low_order > 0xffff:
raise ValueError(f'Invalid ASN format: {asn_string}. Both components must be <= 0xffff.')
return (high_order << 16) | low_order return (high_order << 16) | low_order
else: elif asn_string.isdigit():
return int(asn_string) return int(asn_string)
else:
raise ValueError(f'Unable to parse ASN string: {asn_string}. Expected either a pure integer or a dot notation.')
def _system_bgp_peers(system_node): def _system_bgp_peers(system_node):
......
...@@ -77,11 +77,15 @@ def test_asn_to_int_functionality(): ...@@ -77,11 +77,15 @@ def test_asn_to_int_functionality():
"""Test that ASN to int conversion works with and without dot notation.""" """Test that ASN to int conversion works with and without dot notation."""
# Test with dot notation # Test with dot notation
assert asn_to_int("1.1") == 65537 assert asn_to_int('1.1') == 65537
assert asn_to_int("64512.2") == 4227858434 assert asn_to_int('64512.2') == 4227858434
assert asn_to_int("0.65535") == 65535 assert asn_to_int('0.65535') == 65535
# Test without dot notation # Test without dot notation
assert asn_to_int("65537") == 65537 assert asn_to_int('65537') == 65537
assert asn_to_int("0") == 0 assert asn_to_int('0') == 0
assert asn_to_int("4227858434") == 4227858434 assert asn_to_int('4227858434') == 4227858434
with pytest.raises(ValueError):
asn_to_int('66512.2')
asn_to_int('Test')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment