| Conditions | 10 |
| Total Lines | 96 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| 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._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.""" |
||
| 169 | def _build_int_sink_flows( |
||
| 170 | uni_dst_key: Literal["uni_a", "uni_z"], |
||
| 171 | evc: dict, |
||
| 172 | stored_flows: dict[int, list[dict]], |
||
| 173 | ) -> list[dict]: |
||
| 174 | """ |
||
| 175 | Build INT sink flows. |
||
| 176 | |||
| 177 | At the INT sink, one flow becomes many: |
||
| 178 | 1. Before the proxy, we do add_int_metadata as an INT hop. |
||
| 179 | We need to keep the set_queue |
||
| 180 | 2. After the proxy, we do send_report and pop_int and output |
||
| 181 | We only use table 0 for #1. |
||
| 182 | We use table 2 for #2. for pop_int and output |
||
| 183 | """ |
||
| 184 | new_flows = [] |
||
| 185 | dst_uni = evc[uni_dst_key] |
||
| 186 | proxy_port = dst_uni["proxy_port"] |
||
| 187 | dpid = dst_uni["switch"] |
||
| 188 | |||
| 189 | for flow in stored_flows[utils.get_cookie(evc["id"], settings.MEF_COOKIE_PREFIX)]: |
||
| 190 | # Only consider this sink's dpid flows |
||
| 191 | if flow["switch"] != dpid: |
||
| 192 | continue |
||
| 193 | # Only consider flows coming from NNI interfaces |
||
| 194 | if flow["flow"]["match"]["in_port"] == dst_uni["port_number"]: |
||
| 195 | continue |
||
| 196 | |||
| 197 | new_int_flow_tbl_0_tcp = copy.deepcopy(flow) |
||
| 198 | |||
| 199 | if not new_int_flow_tbl_0_tcp: |
||
| 200 | raise FlowsNotFound(evc["id"]) |
||
| 201 | |||
| 202 | utils.set_new_cookie(new_int_flow_tbl_0_tcp) |
||
| 203 | utils.set_owner(new_int_flow_tbl_0_tcp) |
||
| 204 | utils.set_instructions_from_actions(new_int_flow_tbl_0_tcp) |
||
| 205 | |||
| 206 | # Save for pos-proxy flows |
||
| 207 | new_int_flow_tbl_0_pos = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
| 208 | new_int_flow_tbl_2_pos = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
| 209 | |||
| 210 | # Prepare TCP Flow for Table 0 PRE proxy |
||
| 211 | if not utils.is_intra_switch_evc(evc): |
||
| 212 | new_int_flow_tbl_0_tcp["flow"]["match"]["dl_type"] = settings.IPv4 |
||
| 213 | new_int_flow_tbl_0_tcp["flow"]["match"]["nw_proto"] = settings.TCP |
||
| 214 | utils.set_priority(new_int_flow_tbl_0_tcp) |
||
| 215 | |||
| 216 | # Add telemetry, keep set_queue, output to the proxy port. |
||
| 217 | output_port_no = proxy_port.source.port_number |
||
| 218 | for instruction in new_int_flow_tbl_0_tcp["flow"]["instructions"]: |
||
| 219 | if instruction["instruction_type"] == "apply_actions": |
||
| 220 | # Keep set_queue |
||
| 221 | actions = utils.modify_actions( |
||
| 222 | instruction["actions"], |
||
| 223 | ["pop_vlan", "push_vlan", "set_vlan", "output"], |
||
| 224 | remove=True, |
||
| 225 | ) |
||
| 226 | actions.insert(0, {"action_type": "add_int_metadata"}) |
||
| 227 | actions.append({"action_type": "output", "port": output_port_no}) |
||
| 228 | instruction["actions"] = actions |
||
| 229 | |||
| 230 | # Prepare UDP Flow for Table 0 |
||
| 231 | new_int_flow_tbl_0_udp = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
| 232 | new_int_flow_tbl_0_udp["flow"]["match"]["nw_proto"] = settings.UDP |
||
| 233 | |||
| 234 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_tcp)) |
||
| 235 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_udp)) |
||
| 236 | |||
| 237 | # Prepare Flows for Table 0 AFTER proxy. No difference between TCP or UDP |
||
| 238 | in_port_no = proxy_port.destination.port_number |
||
| 239 | |||
| 240 | new_int_flow_tbl_0_pos["flow"]["match"]["in_port"] = in_port_no |
||
| 241 | utils.set_priority(new_int_flow_tbl_0_tcp) |
||
| 242 | |||
| 243 | instructions = [ |
||
| 244 | { |
||
| 245 | "instruction_type": "apply_actions", |
||
| 246 | "actions": [{"action_type": "send_report"}], |
||
| 247 | }, |
||
| 248 | {"instruction_type": "goto_table", "table_id": settings.INT_TABLE}, |
||
| 249 | ] |
||
| 250 | new_int_flow_tbl_0_pos["flow"]["instructions"] = instructions |
||
| 251 | |||
| 252 | # Prepare Flows for Table 2 POS proxy |
||
| 253 | new_int_flow_tbl_2_pos["flow"]["match"]["in_port"] = in_port_no |
||
| 254 | new_int_flow_tbl_2_pos["flow"]["table_id"] = settings.INT_TABLE |
||
| 255 | utils.set_table_group(new_int_flow_tbl_2_pos) |
||
| 256 | |||
| 257 | for instruction in new_int_flow_tbl_2_pos["flow"]["instructions"]: |
||
| 258 | if instruction["instruction_type"] == "apply_actions": |
||
| 259 | instruction["actions"].insert(0, {"action_type": "pop_int"}) |
||
| 260 | |||
| 261 | new_flows.append(copy.deepcopy(new_int_flow_tbl_0_pos)) |
||
| 262 | new_flows.append(copy.deepcopy(new_int_flow_tbl_2_pos)) |
||
| 263 | |||
| 264 | return new_flows |
||
| 265 |