Skip to content

Commit 2c62534

Browse files
sbasanWojCzu
andauthored
[dev]: update route policy (#170)
* draft * feat: add community default_factory * feat: add IntStrAsNum without PlainSerializer --------- Co-authored-by: Wojciech Czuma <czuma.wojciech@gmail.com>
1 parent 30adac3 commit 2c62534

4 files changed

Lines changed: 46 additions & 11 deletions

File tree

catalystwan/models/common.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ def str_as_str_list(val: Union[str, Sequence[str]]) -> Sequence[str]:
187187
BeforeValidator(lambda x: int(x)),
188188
]
189189

190+
IntStrAsNum = Annotated[
191+
int,
192+
BeforeValidator(lambda x: int(x)),
193+
]
194+
190195
IntRange = Tuple[int, Optional[int]]
191196

192197
DualToneCadenceInterval = Annotated[int, Field(ge=50, le=10_000)]
@@ -203,6 +208,17 @@ def str_as_cadence_pairs(val: Union[str, Sequence[DualToneCadenceStartStop]]) ->
203208
return val
204209

205210

211+
AsNum = Annotated[IntStrAsNum, Field(ge=1, le=4294967295)]
212+
AsRegExp = Annotated[str, Field(pattern=r"^([0-9]+|[0-9]*\.[0-9]+)$")]
213+
AsPrepend = Union[AsNum, AsRegExp]
214+
215+
216+
def str_as_as_prepend_list(val: Any) -> Any:
217+
if isinstance(val, str):
218+
return [item.strip() for item in val.split() if item.strip()]
219+
return val
220+
221+
206222
def tuple_to_str(val: Tuple[Any, ...]) -> str:
207223
return " ".join(map(str, val))
208224

@@ -249,6 +265,20 @@ def tuple_to_str(val: Tuple[Any, ...]) -> str:
249265
Field(min_length=1),
250266
]
251267

268+
SpaceSeparatedAsNumList = Annotated[
269+
List[AsNum],
270+
PlainSerializer(lambda x: " ".join(map(str, x)), return_type=str, when_used="json-unless-none"),
271+
BeforeValidator(str_as_positive_int_list),
272+
Field(min_length=1),
273+
]
274+
275+
SpaceSeparatedAsPrependList = Annotated[
276+
List[AsPrepend],
277+
PlainSerializer(lambda x: " ".join(map(str, x)), return_type=str, when_used="json-unless-none"),
278+
BeforeValidator(str_as_as_prepend_list),
279+
Field(min_length=1),
280+
]
281+
252282
Ipv4PrefixLen = Annotated[
253283
int,
254284
Field(ge=0, le=32),

catalystwan/models/configuration/feature_profile/sdwan/service/route_policy.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
as_global,
1717
as_variable,
1818
)
19-
from catalystwan.models.common import AcceptRejectActionType
19+
from catalystwan.models.common import AcceptRejectActionType, AsPrepend
2020
from catalystwan.models.configuration.feature_profile.common import RefIdItem
2121

2222
Community = Literal["internet", "local-AS", "no-advertise", "no-export"]
@@ -130,11 +130,11 @@ class SetAsPath(BaseModel):
130130
model_config = ConfigDict(
131131
populate_by_name=True,
132132
)
133-
prepend: Optional[List[Global[int]]] = None
133+
prepend: Optional[Global[List[AsPrepend]]] = None
134134

135135
@classmethod
136-
def from_list(cls, prepend: List[int]) -> SetAsPath:
137-
return cls(prepend=[as_global(i) for i in prepend])
136+
def from_list(cls, prepend: List[Union[str, int]]) -> SetAsPath:
137+
return cls(prepend=Global[List[AsPrepend]](value=prepend))
138138

139139

140140
class SetCommunity(BaseModel):
@@ -146,7 +146,7 @@ class SetCommunity(BaseModel):
146146
populate_by_name=True,
147147
)
148148
additive: Union[Global[bool], Default[bool]] = as_default(False)
149-
community: Optional[Union[Global[List[str]], Variable]] = None
149+
community: Union[Global[List[str]], Variable] = Global[List[str]](value=[])
150150

151151

152152
class Accept(BaseModel):
@@ -167,8 +167,11 @@ class Accept(BaseModel):
167167
as_path: Optional[SetAsPath] = Field(
168168
default=None, serialization_alias="setAsPath", validation_alias="setAsPath", description="Set AS Path"
169169
)
170-
community: Optional[SetCommunity] = Field(
171-
default=None, serialization_alias="setCommunity", validation_alias="setCommunity", description="Set Community"
170+
community: SetCommunity = Field(
171+
default_factory=SetCommunity,
172+
serialization_alias="setCommunity",
173+
validation_alias="setCommunity",
174+
description="Set Community",
172175
)
173176
local_preference: Optional[Global[int]] = Field(
174177
default=None,
@@ -330,7 +333,7 @@ def match_ipv6_next_hop(self, ipv6_next_hop: UUID) -> None:
330333
def associate_reject_action(self) -> None:
331334
self.actions = [(RejectActions(reject=as_default(True)))]
332335

333-
def associate_as_path_action(self, prepend: List[int]) -> None:
336+
def associate_as_path_action(self, prepend: List[Union[str, int]]) -> None:
334337
self._accept_action.as_path = SetAsPath.from_list(prepend)
335338

336339
def associate_communities_action(self, additive: bool, communities: List[str]) -> None:

catalystwan/models/policy/definition/route_policy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def associate_aggregator_action(self, aggregator_value: int, ip_address: Union[I
108108
AggregatorActionEntry(value=AggregatorActionEntryValue(aggregator=aggregator_value, ip_address=ip_address))
109109
)
110110

111-
def associate_as_path_action(self, prepend_action: List[int], exclude_action: List[int]):
111+
def associate_as_path_action(self, prepend_action: List[Union[str, int]], exclude_action: List[int]):
112112
self._insert_action_in_set(
113113
AsPathActionEntry(value=AsPathActionEntryValue(prepend=prepend_action, exclude=exclude_action))
114114
)

catalystwan/models/policy/policy_definition.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
SequenceIpType,
2828
ServiceChainNumber,
2929
ServiceType,
30+
SpaceSeparatedAsNumList,
31+
SpaceSeparatedAsPrependList,
3032
SpaceSeparatedInterfaceStr,
3133
SpaceSeparatedIPv4,
3234
SpaceSeparatedIPv6,
@@ -406,8 +408,8 @@ class AsPathListMatchEntry(BaseModel):
406408

407409

408410
class AsPathActionEntryValue(BaseModel):
409-
prepend: Optional[SpaceSeparatedNonNegativeIntList] = None
410-
exclude: Optional[SpaceSeparatedNonNegativeIntList] = None
411+
prepend: Optional[SpaceSeparatedAsPrependList] = None
412+
exclude: Optional[SpaceSeparatedAsNumList] = None
411413

412414

413415
class AsPathActionEntry(BaseModel):

0 commit comments

Comments
 (0)