mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
@@ -1,18 +1,24 @@
|
||||
"""Parse Juniper XML Response to Structured Data."""
|
||||
|
||||
# Standard Library
|
||||
from typing import Dict, Iterable
|
||||
|
||||
# Third Party
|
||||
import xmltodict
|
||||
from pydantic import ValidationError
|
||||
|
||||
# Project
|
||||
from hyperglass.log import log
|
||||
from hyperglass.util import validation_error_message
|
||||
from hyperglass.exceptions import ParsingError, ResponseEmpty
|
||||
from hyperglass.configuration import params
|
||||
from hyperglass.parsing.models.juniper import JuniperRoute
|
||||
|
||||
|
||||
def parse_juniper(output):
|
||||
def parse_juniper(output: Iterable) -> Dict: # noqa: C901
|
||||
"""Parse a Juniper BGP XML response."""
|
||||
data = {}
|
||||
|
||||
for i, response in enumerate(output):
|
||||
try:
|
||||
parsed = xmltodict.parse(
|
||||
@@ -48,4 +54,8 @@ def parse_juniper(output):
|
||||
log.critical(f"'{str(err)}' was not found in the response")
|
||||
raise ParsingError("Error parsing response data")
|
||||
|
||||
except ValidationError as err:
|
||||
log.critical(str(err))
|
||||
raise ParsingError(validation_error_message(*err.errors()))
|
||||
|
||||
return data
|
||||
|
||||
@@ -44,7 +44,7 @@ class JuniperRouteTableEntry(_JuniperBase):
|
||||
peer_as: int
|
||||
source_as: int
|
||||
source_rid: StrictStr
|
||||
communities: List[StrictStr]
|
||||
communities: List[StrictStr] = None
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_optional_flags(cls, values):
|
||||
@@ -105,7 +105,11 @@ class JuniperRouteTableEntry(_JuniperBase):
|
||||
@validator("communities", pre=True, always=True)
|
||||
def validate_communities(cls, value):
|
||||
"""Flatten community list."""
|
||||
return value.get("community", [])
|
||||
if value is not None:
|
||||
flat = value.get("community", [])
|
||||
else:
|
||||
flat = []
|
||||
return flat
|
||||
|
||||
|
||||
class JuniperRouteTable(_JuniperBase):
|
||||
|
||||
Reference in New Issue
Block a user