Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework _parse_route_data to allow multiple routing tables. #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
coverage.xml
dist/
site/
env/
48 changes: 23 additions & 25 deletions pybird/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import socket
from datetime import datetime, timedelta
from subprocess import PIPE, Popen
from more_itertools import peekable


class PyBird:
Expand Down Expand Up @@ -262,30 +263,37 @@ def _parse_route_data(self, data):
[....]
0000
"""
lines = data.splitlines()
splitted = data.splitlines()
lines = peekable(splitted)
routes = []

route_summary = None
route_summar = None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no! a typo!


self.log.debug("PyBird: parse route data: lines=%d", len(lines))
line_counter = -1
while line_counter < len(lines) - 1:
line_counter += 1
line = lines[line_counter].strip()
self.log.debug("PyBird: parse route data: lines=%d", len(splitted))
for line in lines:
self.log.debug("PyBird: parse route data: %s", line)

# A line starts vwith either a space or a number. If it is a number,
# it is a new block, if it is a space, it belongs to the previous number.
# At this point it should not be a space, but if it is, log and skip it.
(field_number, line) = self._extract_field_number(line)
if None == field_number:
self.log.debug("PyBird: WARNING: Line star with unexpected character, skipping")
continue

# Collect all data lines
datablock = [ line.strip() ]
while lines.peek("default").startswith(" "):
self.log.debug("PyBird: Collecting field %s data line: %s", (field_number, lines.peek()))
datablock.append(next(lines).strip())

if field_number in self.ignored_field_numbers:
continue

if field_number == 1007:
try:
route_summary = self._parse_route_summary(line)
except ValueError:
# bird2 sends route summary on a new line
line_counter += 1
line = lines[line_counter].strip()
route_summary = self._parse_route_summary(line)
# As far as foreseen, the info is always on the last line in the datablock.
# Allso, if desired, here is the place to to extract the tablename.
route_summary = self._parse_route_summary(datablock[-1])

route_detail = None

Expand All @@ -294,17 +302,7 @@ def _parse_route_data(self, data):
# This is not detail of a BGP route
continue

# A route detail spans multiple lines, read them all
route_detail_raw = []
while "BGP." in line:
route_detail_raw.append(line)
line_counter += 1
line = lines[line_counter]
self.log.debug("PyBird: parse route data: %s", line)
# this loop will have walked a bit too far, correct it
line_counter -= 1

route_detail = self._parse_route_detail(route_detail_raw)
route_detail = self._parse_route_detail(datablock)

# Save the summary+detail info in our result
route_detail.update(route_summary)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ packages = [{ include = "pybird" }]

[tool.poetry.dependencies]
python = "^3.7"
mote_itertools = "*"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another small typo. I was testing your PR locally and PIP threw an error.


[tool.poetry.dev-dependencies]
# testing
Expand Down
1 change: 1 addition & 0 deletions tests/data/parse/route/data/ipv6_table.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"origin": "IGP", "as_path": "2914 3491 55644 45271", "next_hop": "89.188.4.16", "med": "1", "local_pref": "210", "atomic_aggr": true, "aggregator": "10.100.230.241 AS65010", "community": "31477:204 31477:2600", "prefix": "49.14.107.0/24", "peer": "89.188.4.16", "interface": null, "source": "eqam7_4", "time": "2023-12-29"}, {"origin": "IGP", "as_path": "1299 6762 28008 23201", "next_hop": "89.188.4.16", "med": "1", "local_pref": "210", "community": "31477:203 31477:2700", "originator_id": "89.188.4.5", "cluster_list": "89.188.4.16", "prefix": "181.123.200.0/22", "peer": "89.188.4.16", "interface": null, "source": "eqam7_4", "time": "2023-12-29"}, {"origin": "IGP", "as_path": "6939 15412 142505 141047 24550 45157 4613 4613", "next_hop": "2a01:1b0:1::16", "med": "0", "local_pref": "500", "community": "31477:202 31477:2300 31477:2320", "prefix": "2400:a400::/32", "peer": "2a01:1b0:1::16", "interface": null, "source": "eqam7_6", "time": "2023-12-29"}, {"origin": "IGP", "as_path": "6939 23911 38255 146244", "next_hop": "2a01:1b0:1::16", "med": "0", "local_pref": "500", "community": "31477:202 31477:2300 31477:2320", "prefix": "240a:ae0a::/32", "peer": "2a01:1b0:1::16", "interface": null, "source": "eqam7_6", "time": "2023-12-29"}]
40 changes: 40 additions & 0 deletions tests/data/parse/route/data/ipv6_table.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
0001 BIRD 2.0.7 ready.
1007-Table master4:
49.14.107.0/24 unreachable [eqam7_4 2023-12-29 from 89.188.4.16] * (100) [AS45271i]
1008- Type: BGP univ
1012- BGP.origin: IGP
BGP.as_path: 2914 3491 55644 45271
BGP.next_hop: 89.188.4.16
BGP.med: 1
BGP.local_pref: 210
BGP.atomic_aggr:
BGP.aggregator: 10.100.230.241 AS65010
BGP.community: (31477,204) (31477,2600)
1007-181.123.200.0/22 unreachable [eqam7_4 2023-12-29 from 89.188.4.16] * (100) [AS23201i]
1008- Type: BGP univ
1012- BGP.origin: IGP
BGP.as_path: 1299 6762 28008 23201
BGP.next_hop: 89.188.4.16
BGP.med: 1
BGP.local_pref: 210
BGP.community: (31477,203) (31477,2700)
BGP.originator_id: 89.188.4.5
BGP.cluster_list: 89.188.4.16
1007-
Table master6:
2400:a400::/32 unreachable [eqam7_6 2023-12-29 from 2a01:1b0:1::16] * (100) [AS4613i]
1008- Type: BGP univ
1012- BGP.origin: IGP
BGP.as_path: 6939 15412 142505 141047 24550 45157 4613 4613
BGP.next_hop: 2a01:1b0:1::16
BGP.med: 0
BGP.local_pref: 500
BGP.community: (31477,202) (31477,2300) (31477,2320)
1007-240a:ae0a::/32 unreachable [eqam7_6 2023-12-29 from 2a01:1b0:1::16] * (100) [AS146244i]
1008- Type: BGP univ
1012- BGP.origin: IGP
BGP.as_path: 6939 23911 38255 146244
BGP.next_hop: 2a01:1b0:1::16
BGP.med: 0
BGP.local_pref: 500
BGP.community: (31477,202) (31477,2300) (31477,2320)