1
- from typing import Any , List , Tuple , Dict
2
- from pydantic import BaseModel , validator
1
+ from typing import Any , List , Tuple , Dict , Annotated
2
+ from pydantic import BaseModel , field_validator , ValidationInfo , Field
3
3
from .utils import exceptions , constants as const
4
4
from .model .m_test_config import TestConfigModel
5
5
from .model .m_test_type_config import TestTypesConfiguration
11
11
12
12
13
13
class PluginModel2544 (BaseModel ): # Main Model
14
- test_configuration : TestConfigModel
14
+ test_configuration : Annotated [ TestConfigModel , Field ( validate_default = True )]
15
15
protocol_segments : List [ProtocolSegmentProfileConfig ]
16
- ports_configuration : PortConfType
16
+ ports_configuration : Annotated [ PortConfType , Field ( validate_default = True )]
17
17
test_types_configuration : TestTypesConfiguration
18
18
19
19
def set_ports_rx_tx_type (self ) -> None :
@@ -45,33 +45,33 @@ def __init__(self, **data: Dict[str, Any]) -> None:
45
45
self .check_port_groups_and_peers ()
46
46
self .set_profile ()
47
47
48
- @validator ("ports_configuration" , always = True )
49
- def check_ip_properties (cls , v : "PortConfType" , values ) -> "PortConfType" :
50
- pro_map = {v .id : v .protocol_version for v in values ['protocol_segments' ]}
51
- for i , port_config in enumerate (v ):
48
+
49
+ @field_validator ("ports_configuration" )
50
+ def check_ip_properties (cls , value : "PortConfType" , info : ValidationInfo ) -> "PortConfType" :
51
+ pro_map = {v .id : v .protocol_version for v in info .data ['protocol_segments' ]}
52
+ for i , port_config in enumerate (value ):
52
53
if port_config .protocol_segment_profile_id not in pro_map :
53
54
raise exceptions .PSPMissing ()
54
55
if (
55
56
pro_map [port_config .protocol_segment_profile_id ].is_l3
56
57
and (not port_config .ip_address or port_config .ip_address .address .is_empty )
57
58
):
58
59
raise exceptions .IPAddressMissing ()
59
- return v
60
+ return value
60
61
61
- @validator ("ports_configuration" , always = True )
62
- def check_port_count (
63
- cls , v : "PortConfType" , values : Dict [str , Any ]
64
- ) -> "PortConfType" :
62
+
63
+ @field_validator ("ports_configuration" )
64
+ def check_port_count (cls , value : "PortConfType" , info : ValidationInfo ) -> "PortConfType" :
65
65
require_ports = 2
66
- if "test_configuration" in values :
67
- topology : const .TestTopology = values [
66
+ if "test_configuration" in info . data :
67
+ topology : const .TestTopology = info . data [
68
68
"test_configuration"
69
69
].topology_config .topology
70
70
if topology .is_pair_topology :
71
71
require_ports = 1
72
- if len (v ) < require_ports :
72
+ if len (value ) < require_ports :
73
73
raise exceptions .PortConfigNotEnough (require_ports )
74
- return v
74
+ return value
75
75
76
76
def check_port_groups_and_peers (self ) -> None :
77
77
topology = self .test_configuration .topology_config .topology
@@ -89,37 +89,35 @@ def check_port_groups_and_peers(self) -> None:
89
89
if not i :
90
90
raise exceptions .PortGroupError (group )
91
91
92
- @validator ("ports_configuration" , always = True )
93
- def check_modifier_mode_and_segments (
94
- cls , v : "PortConfType" , values : Dict [str , Any ]
95
- ) -> "PortConfType" :
96
- if "test_configuration" in values :
97
- flow_creation_type = values [
92
+
93
+ @field_validator ("ports_configuration" )
94
+ def check_modifier_mode_and_segments (cls , value : PortConfType , info : ValidationInfo ) -> PortConfType :
95
+ if "test_configuration" in info .data :
96
+ flow_creation_type = info .data [
98
97
"test_configuration"
99
98
].test_execution_config .flow_creation_config .flow_creation_type
100
- for port_config in v :
99
+ for port_config in value :
101
100
if (
102
101
not flow_creation_type .is_stream_based
103
102
) and port_config .profile .protocol_version .is_l3 :
104
103
raise exceptions .ModifierBasedNotSupportL3 ()
105
- return v
104
+ return value
105
+
106
106
107
- @validator ("ports_configuration" , always = True )
108
- def check_port_group (
109
- cls , v : "PortConfiguration" , values : Dict [str , Any ]
110
- ) -> "PortConfiguration" :
111
- if "ports_configuration" in values and "test_configuration" in values :
112
- for k , p in values ["ports_configuration" ].items ():
107
+ @field_validator ("ports_configuration" )
108
+ def check_port_group (cls , value : PortConfiguration , info : ValidationInfo ) -> PortConfiguration :
109
+ if "ports_configuration" in info .data and "test_configuration" in info .data :
110
+ for k , p in info .data ["ports_configuration" ].items ():
113
111
if (
114
112
p .port_group == const .PortGroup .UNDEFINED
115
- and not values [
113
+ and not info . data [
116
114
"test_configuration"
117
115
].topology_config .topology .is_mesh_topology
118
116
):
119
117
raise exceptions .PortGroupNeeded ()
120
- return v
118
+ return value
121
119
122
- @validator ("test_types_configuration" , always = True )
120
+ @field_validator ("test_types_configuration" )
123
121
def check_test_type_enable (
124
122
cls , v : "TestTypesConfiguration"
125
123
) -> "TestTypesConfiguration" :
@@ -134,22 +132,21 @@ def check_test_type_enable(
134
132
raise exceptions .TestTypesError ()
135
133
return v
136
134
137
- @validator ("test_types_configuration" , always = True )
138
- def check_result_scope (
139
- cls , v : "TestTypesConfiguration" , values : Dict [str , Any ]
140
- ) -> "TestTypesConfiguration" :
141
- if "test_configuration" not in values :
142
- return v
135
+
136
+ @field_validator ("test_types_configuration" )
137
+ def check_result_scope (cls , value : "TestTypesConfiguration" , info : ValidationInfo ) -> "TestTypesConfiguration" :
138
+ if "test_configuration" not in info .data :
139
+ return value
143
140
if (
144
- v .throughput_test .enabled
145
- and v .throughput_test .rate_iteration_options .result_scope
141
+ value .throughput_test .enabled
142
+ and value .throughput_test .rate_iteration_options .result_scope
146
143
== const .RateResultScopeType .PER_SOURCE_PORT
147
- and not values [
144
+ and not info . data [
148
145
"test_configuration"
149
146
].test_execution_config .flow_creation_config .flow_creation_type .is_stream_based
150
147
):
151
148
raise exceptions .ModifierBasedNotSupportPerPortResult ()
152
- return v
149
+ return value
153
150
154
151
@staticmethod
155
152
def count_port_group (
0 commit comments