| Conditions | 11 |
| Total Lines | 107 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 47 |
| CRAP Score | 11.0261 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like build.managers.flow_builder.FlowBuilder._build_int_sink_flows() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """flow_builder module responsible for building and mapping flows.""" |
||
| 193 | 1 | def _build_int_sink_flows( |
|
| 194 | self, |
||
| 195 | uni_dst_key: Literal["uni_a", "uni_z"], |
||
| 196 | evc: dict, |
||
| 197 | stored_flows: dict[int, list[dict]], |
||
| 198 | ) -> list[dict]: |
||
| 199 | """ |
||
| 200 | Build INT sink flows. |
||
| 201 | |||
| 202 | At the INT sink, one flow becomes many: |
||
| 203 | 1. Before the proxy, we do add_int_metadata as an INT hop. |
||
| 204 | We need to keep the set_queue |
||
| 205 | 2. After the proxy, we do send_report and pop_int and output |
||
| 206 | We only use table 0 for #1. |
||
| 207 | We use table X (2 or 3) for #2. for pop_int and output |
||
| 208 | """ |
||
| 209 | 1 | new_flows = [] |
|
| 210 | 1 | dst_uni = evc[uni_dst_key] |
|
| 211 | 1 | proxy_port = dst_uni["proxy_port"] |
|
| 212 | 1 | dpid = dst_uni["switch"] |
|
| 213 | |||
| 214 | 1 | for flow in stored_flows[ |
|
| 215 | utils.get_cookie(evc["id"], settings.MEF_COOKIE_PREFIX) |
||
| 216 | ]: |
||
| 217 | # Only consider this sink's dpid flows |
||
| 218 | 1 | if flow["switch"] != dpid: |
|
| 219 | 1 | continue |
|
| 220 | # Only consider flows coming from NNI interfaces |
||
| 221 | 1 | if flow["flow"]["match"]["in_port"] == dst_uni["port_number"]: |
|
| 222 | 1 | continue |
|
| 223 | |||
| 224 | 1 | new_int_flow_tbl_0_tcp = copy.deepcopy(flow) |
|
| 225 | |||
| 226 | 1 | if not new_int_flow_tbl_0_tcp: |
|
| 227 | if not evc["active"]: |
||
| 228 | return [] |
||
| 229 | raise FlowsNotFound(evc["id"]) |
||
| 230 | |||
| 231 | 1 | utils.set_new_cookie(new_int_flow_tbl_0_tcp) |
|
| 232 | 1 | utils.set_owner(new_int_flow_tbl_0_tcp) |
|
| 233 | 1 | utils.set_instructions_from_actions(new_int_flow_tbl_0_tcp) |
|
| 234 | |||
| 235 | # Save for pos-proxy flows |
||
| 236 | 1 | new_int_flow_tbl_0_pos = copy.deepcopy(new_int_flow_tbl_0_tcp) |
|
| 237 | 1 | new_int_flow_tbl_x_pos = copy.deepcopy(new_int_flow_tbl_0_tcp) |
|
| 238 | |||
| 239 | # Prepare TCP Flow for Table 0 PRE proxy |
||
| 240 | 1 | if not utils.is_intra_switch_evc(evc): |
|
| 241 | 1 | new_int_flow_tbl_0_tcp["flow"]["match"]["dl_type"] = settings.IPv4 |
|
| 242 | 1 | new_int_flow_tbl_0_tcp["flow"]["match"]["nw_proto"] = settings.TCP |
|
| 243 | 1 | utils.set_priority(new_int_flow_tbl_0_tcp) |
|
| 244 | |||
| 245 | # Add telemetry, keep set_queue, output to the proxy port. |
||
| 246 | 1 | output_port_no = proxy_port.source.port_number |
|
| 247 | 1 | for instruction in new_int_flow_tbl_0_tcp["flow"]["instructions"]: |
|
| 248 | 1 | if instruction["instruction_type"] == "apply_actions": |
|
| 249 | # Keep set_queue |
||
| 250 | 1 | actions = utils.modify_actions( |
|
| 251 | instruction["actions"], |
||
| 252 | ["pop_vlan", "push_vlan", "set_vlan", "output"], |
||
| 253 | remove=True, |
||
| 254 | ) |
||
| 255 | 1 | actions.insert(0, {"action_type": "add_int_metadata"}) |
|
| 256 | 1 | actions.append( |
|
| 257 | {"action_type": "output", "port": output_port_no} |
||
| 258 | ) |
||
| 259 | 1 | instruction["actions"] = actions |
|
| 260 | |||
| 261 | # Prepare UDP Flow for Table 0 |
||
| 262 | 1 | new_int_flow_tbl_0_udp = copy.deepcopy(new_int_flow_tbl_0_tcp) |
|
| 263 | 1 | new_int_flow_tbl_0_udp["flow"]["match"]["nw_proto"] = settings.UDP |
|
| 264 | |||
| 265 | 1 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_tcp)) |
|
| 266 | 1 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_udp)) |
|
| 267 | |||
| 268 | # Prepare Flows for Table 0 AFTER proxy. No difference between TCP or UDP |
||
| 269 | 1 | in_port_no = proxy_port.destination.port_number |
|
| 270 | |||
| 271 | 1 | new_int_flow_tbl_0_pos["flow"]["match"]["in_port"] = in_port_no |
|
| 272 | 1 | utils.set_priority(new_int_flow_tbl_0_tcp) |
|
| 273 | |||
| 274 | 1 | table_group = self.table_group |
|
| 275 | 1 | new_table_id = table_group[new_int_flow_tbl_x_pos["flow"]["table_group"]] |
|
| 276 | 1 | instructions = [ |
|
| 277 | { |
||
| 278 | "instruction_type": "apply_actions", |
||
| 279 | "actions": [{"action_type": "send_report"}], |
||
| 280 | }, |
||
| 281 | { |
||
| 282 | "instruction_type": "goto_table", |
||
| 283 | "table_id": new_table_id, |
||
| 284 | }, |
||
| 285 | ] |
||
| 286 | 1 | new_int_flow_tbl_0_pos["flow"]["instructions"] = instructions |
|
| 287 | |||
| 288 | # Prepare Flows for Table X POS proxy |
||
| 289 | 1 | new_int_flow_tbl_x_pos["flow"]["match"]["in_port"] = in_port_no |
|
| 290 | 1 | new_int_flow_tbl_x_pos["flow"]["table_id"] = new_table_id |
|
| 291 | |||
| 292 | 1 | for instruction in new_int_flow_tbl_x_pos["flow"]["instructions"]: |
|
| 293 | 1 | if instruction["instruction_type"] == "apply_actions": |
|
| 294 | 1 | instruction["actions"].insert(0, {"action_type": "pop_int"}) |
|
| 295 | |||
| 296 | 1 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_pos)) |
|
| 297 | 1 | new_flows.append(copy.deepcopy(new_int_flow_tbl_x_pos)) |
|
| 298 | |||
| 299 | return new_flows |
||
| 300 |