Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
dashboardv3
inventory-provider
Commits
1af6f147
Commit
1af6f147
authored
1 year ago
by
Neda Moeini
Browse files
Options
Downloads
Patches
Plain Diff
Support ASN dot notation.
parent
dc15e2f5
No related branches found
No related tags found
1 merge request
!18
Support ASN dot notation.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/juniper.py
+32
-2
32 additions, 2 deletions
inventory_provider/juniper.py
test/test_juniper_data_global.py
+19
-0
19 additions, 0 deletions
test/test_juniper_data_global.py
with
51 additions
and
2 deletions
inventory_provider/juniper.py
+
32
−
2
View file @
1af6f147
...
...
@@ -243,6 +243,36 @@ 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
):
...
...
@@ -251,11 +281,11 @@ def _system_bgp_peers(system_node):
peer_as
=
neighbor_node
.
find
(
'
peer-as
'
)
if
peer_as
is
not
None
:
# lxml usage warning: can't just test `if peer_as:`
info
[
'
remote-asn
'
]
=
int
(
peer_as
.
text
)
info
[
'
remote-asn
'
]
=
asn_to_
int
(
peer_as
.
text
)
local_as
=
neighbor_node
.
find
(
'
local-as
'
)
if
local_as
is
not
None
:
asn_value_node
=
local_as
.
find
(
'
as-number
'
)
info
[
'
local-asn
'
]
=
int
(
asn_value_node
.
text
)
info
[
'
local-asn
'
]
=
asn_to_
int
(
asn_value_node
.
text
)
description
=
neighbor_node
.
find
(
'
description
'
)
if
description
is
not
None
:
# lxml usage warning: can't just test `if description:`
...
...
This diff is collapsed.
Click to expand it.
test/test_juniper_data_global.py
+
19
−
0
View file @
1af6f147
...
...
@@ -5,6 +5,7 @@ import ipaddress
import
pytest
from
inventory_provider
import
juniper
from
inventory_provider.juniper
import
asn_to_int
NETIFACES_TEST_DATA_STRING
=
"""
{
'
lo0
'
: {{AF_INET}: [{
'
addr
'
:
'
127.0.0.1
'
,
'
netmask
'
:
'
255.0.0.0
'
,
'
peer
'
:
'
127.0.0.1
'
}],
...
...
@@ -70,3 +71,21 @@ def test_local_v6_interfaces(mocked_netifaces):
assert
len
(
addresses
)
==
4
for
a
in
addresses
:
assert
isinstance
(
a
,
ipaddress
.
IPv6Interface
)
def
test_asn_to_int_functionality
():
"""
Test that ASN to int conversion works with and without dot notation.
"""
# Test with dot notation
assert
asn_to_int
(
'
1.1
'
)
==
65537
assert
asn_to_int
(
'
64512.2
'
)
==
4227858434
assert
asn_to_int
(
'
0.65535
'
)
==
65535
# Test without dot notation
assert
asn_to_int
(
'
65537
'
)
==
65537
assert
asn_to_int
(
'
0
'
)
==
0
assert
asn_to_int
(
'
4227858434
'
)
==
4227858434
with
pytest
.
raises
(
ValueError
):
asn_to_int
(
'
66512.2
'
)
asn_to_int
(
'
Test
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment