Skip to content

Commit

Permalink
fix #54
Browse files Browse the repository at this point in the history
handle empty list of communities
  • Loading branch information
thatmattlove committed Jul 14, 2020
1 parent fe84d72 commit 85519da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion hyperglass/parsing/juniper.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
8 changes: 6 additions & 2 deletions hyperglass/parsing/models/juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -105,7 +105,11 @@ def validate_as_path(cls, value):
@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):
Expand Down

0 comments on commit 85519da

Please sign in to comment.