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

Use pydantic alias for serialization of BGP/BFD events #335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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 changelog.d/331.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use Zino1 field names for serialization of BGP/BFD events
24 changes: 15 additions & 9 deletions src/zino/statemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def model_dump_simple_attrs(self) -> dict[str, str]:
serialization formats (one for JSON state dumps, one for the legacy Zino protocol), and it's not clear how
Pydantic can support that.
"""
attrs = self.model_dump(mode="python", exclude={"log", "history"}, exclude_none=True)
attrs = self.model_dump(mode="python", exclude={"log", "history"}, exclude_none=True, by_alias=True)
return {attr.replace("_", "-"): self.zinoify_value(value) for attr, value in attrs.items()}

@staticmethod
Expand Down Expand Up @@ -355,26 +355,32 @@ def subindex(self) -> SubIndex:


class BGPEvent(Event):
# Allow populating fields by name or alias
model_config = ConfigDict(populate_by_name=True)

type: Literal["bgp"] = "bgp"
remote_addr: Optional[IPAddress] = None
remote_as: Optional[int] = None
remote_as: Optional[int] = Field(default=None, alias="remote-AS")
peer_uptime: Optional[int] = None
bgpos: Optional[BGPOperState] = None
bgpas: Optional[BGPAdminStatus] = None
bgpos: Optional[BGPOperState] = Field(default=None, alias="bgpOS")
bgpas: Optional[BGPAdminStatus] = Field(default=None, alias="bgpAS")

@property
def subindex(self) -> SubIndex:
return self.remote_addr


class BFDEvent(Event):
# Allow populating fields by name or alias
model_config = ConfigDict(populate_by_name=True)

type: Literal["bfd"] = "bfd"
ifindex: Optional[int] = None
bfdstate: Optional[BFDSessState] = None
bfdix: Optional[int] = None
bfddiscr: Optional[int] = None
bfdaddr: Optional[IPAddress] = None
neigh_rdns: Optional[str] = None
bfdstate: Optional[BFDSessState] = Field(default=None, alias="bfdState")
bfdix: Optional[int] = Field(default=None, alias="bfdIx")
bfddiscr: Optional[int] = Field(default=None, alias="bfdDiscr")
bfdaddr: Optional[IPAddress] = Field(default=None, alias="bfdAddr")
neigh_rdns: Optional[str] = Field(default=None, alias="Neigh-rDNS")

@property
def subindex(self) -> SubIndex:
Expand Down
20 changes: 20 additions & 0 deletions tests/api/legacy_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from datetime import timedelta
from io import BytesIO
from ipaddress import IPv4Address
from unittest.mock import Mock, patch

import pytest
Expand All @@ -17,6 +18,8 @@
from zino.config.models import PollDevice
from zino.state import ZinoState
from zino.statemodels import (
BGPEvent,
BGPOperState,
DeviceMaintenance,
Event,
EventState,
Expand Down Expand Up @@ -446,6 +449,23 @@ async def test_when_caseid_is_invalid_it_should_output_error(self, authenticated
output = authenticated_protocol.transport.data_buffer.getvalue().decode()
assert "\r\n500 " in output

@pytest.mark.asyncio
async def test_should_output_correct_attrs_for_alias(self, authenticated_protocol):
state = authenticated_protocol._state
event1 = state.events.create_event("foo", IPv4Address("127.0.0.1"), BGPEvent)
event1.bgpos = BGPOperState.ESTABLISHED
event1.remote_as = 2
state.events.commit(event1)

await authenticated_protocol.message_received(f"GETATTRS {event1.id}")

output = authenticated_protocol.transport.data_buffer.getvalue().decode()
assert f"id: {event1.id}\r\n" in output
assert f"router: {event1.router}\r\n" in output
assert f"state: {event1.state.value}\r\n" in output
assert f"bgpOS: {event1.bgpos}\r\n" in output
assert f"remote-AS: {event1.remote_as}\r\n" in output


class TestZino1ServerProtocolGethistCommand:
@pytest.mark.asyncio
Expand Down
Loading