1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-05-07 12:43:05 +00:00

Fix: Standardize BGP parsers to use BGPRoute objects and harmonize variable naming

- Add BGPRoute import to Juniper, Arista, and FRR parsers
- Change all parsers to instantiate BGPRoute objects instead of raw dictionaries
- Harmonize variable naming across all BGP parsers:
  - Use 'route_data' for dictionary variable
  - Use 'routes' for output list
  - Use 'route' for loop variable
- Ensure consistent validation behavior across MikroTik, Huawei, Juniper, Arista, and FRR
- All parsers now properly invoke BGP table class validation
This commit is contained in:
Wilhelm Schonfeldt 2025-09-26 16:53:36 +02:00
parent d025630b83
commit 6af39fb5be
4 changed files with 72 additions and 75 deletions

View file

@ -9,7 +9,7 @@ from pydantic import ConfigDict
# Project
from hyperglass.log import log
from hyperglass.models.data import BGPRouteTable
from hyperglass.models.data import BGPRoute, BGPRouteTable
# Local
from ..main import HyperglassModel
@ -138,23 +138,22 @@ class AristaBGPTable(_AristaBase):
if len(as_path) != 0:
source_as = as_path[0]
routes.append(
{
"prefix": prefix,
"active": route.route_type.active,
"age": self._get_route_age(route.timestamp),
"weight": route.weight,
"med": route.med,
"local_preference": route.local_preference,
"as_path": as_path,
"communities": communities,
"next_hop": route.next_hop,
"source_as": source_as,
"source_rid": route.peer_entry.peer_router_id,
"peer_rid": route.peer_entry.peer_router_id,
"rpki_state": rpki_state,
}
)
route_data = {
"prefix": prefix,
"active": route.route_type.active,
"age": self._get_route_age(route.timestamp),
"weight": route.weight,
"med": route.med,
"local_preference": route.local_preference,
"as_path": as_path,
"communities": communities,
"next_hop": route.next_hop,
"source_as": source_as,
"source_rid": route.peer_entry.peer_router_id,
"peer_rid": route.peer_entry.peer_router_id,
"rpki_state": rpki_state,
}
routes.append(BGPRoute(**route_data))
serialized = BGPRouteTable(
vrf=self.vrf,

View file

@ -9,7 +9,7 @@ from pydantic import ConfigDict, model_validator
# Project
from hyperglass.log import log
from hyperglass.models.data import BGPRouteTable
from hyperglass.models.data import BGPRoute, BGPRouteTable
# Local
from ..main import HyperglassModel
@ -91,25 +91,24 @@ class FRRBGPTable(_FRRBase):
now = datetime.utcnow().timestamp()
then = datetime.utcfromtimestamp(route.last_update).timestamp()
age = int(now - then)
routes.append(
{
"prefix": self.prefix,
"active": route.bestpath,
"age": age,
"weight": route.weight,
"med": route.med,
"local_preference": route.loc_prf,
"as_path": route.aspath,
"communities": route.community,
"next_hop": route.nexthops[0].ip,
"source_as": route.aggregator_as,
"source_rid": route.aggregator_id,
"peer_rid": route.peer.peer_id,
# TODO: somehow, get the actual RPKI state
# This depends on whether or not the RPKI module is enabled in FRR
"rpki_state": 3,
}
)
route_data = {
"prefix": self.prefix,
"active": route.bestpath,
"age": age,
"weight": route.weight,
"med": route.med,
"local_preference": route.loc_prf,
"as_path": route.aspath,
"communities": route.community,
"next_hop": route.nexthops[0].ip,
"source_as": route.aggregator_as,
"source_rid": route.aggregator_id,
"peer_rid": route.peer.peer_id,
# TODO: somehow, get the actual RPKI state
# This depends on whether or not the RPKI module is enabled in FRR
"rpki_state": 3,
}
routes.append(BGPRoute(**route_data))
serialized = BGPRouteTable(
vrf=vrf,

View file

@ -9,7 +9,7 @@ from pydantic import ConfigDict, field_validator, model_validator
# Project
from hyperglass.log import log
from hyperglass.util import deep_convert_keys
from hyperglass.models.data.bgp_route import BGPRouteTable
from hyperglass.models.data.bgp_route import BGPRoute, BGPRouteTable
# Local
from ..main import HyperglassModel
@ -176,23 +176,22 @@ class JuniperBGPTable(JuniperBase):
count += table.rt_entry_count
prefix = "/".join(str(i) for i in (table.rt_destination, table.rt_prefix_length))
for route in table.rt_entry:
routes.append(
{
"prefix": prefix,
"active": route.active_tag,
"age": route.age,
"weight": route.preference,
"med": route.metric,
"local_preference": route.local_preference,
"as_path": route.as_path,
"communities": route.communities,
"next_hop": route.next_hop,
"source_as": route.source_as,
"source_rid": route.source_rid,
"peer_rid": route.peer_rid,
"rpki_state": route.validation_state,
}
)
route_data = {
"prefix": prefix,
"active": route.active_tag,
"age": route.age,
"weight": route.preference,
"med": route.metric,
"local_preference": route.local_preference,
"as_path": route.as_path,
"communities": route.communities,
"next_hop": route.next_hop,
"source_as": route.source_as,
"source_rid": route.source_rid,
"peer_rid": route.peer_rid,
"rpki_state": route.validation_state,
}
routes.append(BGPRoute(**route_data))
serialized = BGPRouteTable(vrf=vrf, count=count, routes=routes, winning_weight="low")
log.bind(platform="juniper", response=repr(serialized)).debug("Serialized response")

View file

@ -282,28 +282,28 @@ class MikrotikBGPTable(MikrotikBase):
return inst
def bgp_table(self) -> BGPRouteTable:
out = []
for r in self.routes:
route_dict = {
"prefix": r.prefix,
"active": r.active,
"age": r.age,
"weight": r.weight,
"med": r.med,
"local_preference": r.local_preference,
"as_path": r.as_path,
"communities": r.all_communities,
"next_hop": r.next_hop,
"source_as": r.source_as,
"source_rid": r.source_rid,
"peer_rid": r.peer_rid,
"rpki_state": r.rpki_state,
routes = []
for route in self.routes:
route_data = {
"prefix": route.prefix,
"active": route.active,
"age": route.age,
"weight": route.weight,
"med": route.med,
"local_preference": route.local_preference,
"as_path": route.as_path,
"communities": route.all_communities,
"next_hop": route.next_hop,
"source_as": route.source_as,
"source_rid": route.source_rid,
"peer_rid": route.peer_rid,
"rpki_state": route.rpki_state,
}
# Instantiate BGPRoute to trigger validation (including external RPKI)
out.append(BGPRoute(**route_dict))
routes.append(BGPRoute(**route_data))
return MikrotikBGPRouteTable(
vrf="default",
count=len(out),
routes=out,
count=len(routes),
routes=routes,
winning_weight="low",
)