Skip to content
Snippets Groups Projects
Commit b11787ac authored by Robert Latta's avatar Robert Latta
Browse files

moved asn_to_int function into tasks.common

parent aa1dafa6
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,8 @@ from jnpr.junos import exception as EzErrors
from lxml import etree
import netifaces
from inventory_provider.tasks.common import asn_to_int
CONFIG_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
......@@ -280,36 +282,6 @@ def list_interfaces(netconf_config):
yield u
def asn_to_int(asn_string: str) -> int:
"""
Convert a possibly dotted ASN to an integer.
Args:
asn_string (str): ASN to be converted, can be in dot notation or not.
Returns:
int: ASN in integer format.
Raises:
ValueError: If the ASN string is not in the expected format or exceeds valid range.
"""
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
elif asn_string.isdigit():
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 _peering_params(neighbor_node):
address = neighbor_node.find('name').text
......
......@@ -255,3 +255,33 @@ def ims_sorted_service_type_key(service_type):
# prettification ... e.g. no trailing _ for 'MD-VPN (NATIVE)'
service_type = re.sub('_+$', '', service_type)
return re.sub('^_+', '', service_type)
def asn_to_int(asn_string: str) -> int:
"""
Convert a possibly dotted ASN to an integer.
Args:
asn_string (str): ASN to be converted, can be in dot notation or not.
Returns:
int: ASN in integer format.
Raises:
ValueError: If the ASN string is not in the expected format or exceeds valid range.
"""
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
elif asn_string.isdigit():
return int(asn_string)
else:
raise ValueError(f'Unable to parse ASN string: {asn_string}. Expected either a pure integer or a dot notation.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment