| Conditions | 10 |
| Total Lines | 86 |
| Code Lines | 48 |
| 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_source_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.""" |
||
| 37 | def _build_int_source_flows( |
||
| 38 | uni_src_key: Literal["uni_a", "uni_z"], |
||
| 39 | evc: dict, |
||
| 40 | stored_flows: dict[int, list[dict]], |
||
| 41 | ) -> list[dict]: |
||
| 42 | """Build INT source flows. |
||
| 43 | |||
| 44 | At the INT source, one flow becomes 3: one for UDP on table 0, |
||
| 45 | one for TCP on table 0, and one on table 2 |
||
| 46 | On table 0, we use just new instructions: push_int and goto_table |
||
| 47 | On table 2, we add add_int_metadata before the original actions |
||
| 48 | INT flows will have higher priority. |
||
| 49 | """ |
||
| 50 | new_flows = [] |
||
| 51 | new_int_flow_tbl_0_tcp = {} |
||
| 52 | src_uni = evc[uni_src_key] |
||
| 53 | proxy_port = src_uni["proxy_port"] |
||
| 54 | |||
| 55 | # Get the original flows |
||
| 56 | dpid = src_uni["switch"] |
||
| 57 | for flow in stored_flows[utils.get_cookie(evc["id"], settings.MEF_COOKIE_PREFIX)]: |
||
| 58 | if ( |
||
| 59 | flow["switch"] == dpid |
||
| 60 | and flow["flow"]["match"]["in_port"] == src_uni["port_number"] |
||
| 61 | ): |
||
| 62 | new_int_flow_tbl_0_tcp = copy.deepcopy(flow) |
||
| 63 | break |
||
| 64 | |||
| 65 | if not new_int_flow_tbl_0_tcp: |
||
| 66 | raise FlowsNotFound(evc["id"]) |
||
| 67 | |||
| 68 | utils.set_instructions_from_actions(new_int_flow_tbl_0_tcp) |
||
| 69 | utils.set_new_cookie(new_int_flow_tbl_0_tcp) |
||
| 70 | utils.set_owner(new_int_flow_tbl_0_tcp) |
||
| 71 | |||
| 72 | # Deepcopy to use for table 2 later |
||
| 73 | new_int_flow_tbl_2 = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
| 74 | |||
| 75 | # Prepare TCP Flow for Table 0 |
||
| 76 | new_int_flow_tbl_0_tcp["flow"]["match"]["dl_type"] = settings.IPv4 |
||
| 77 | new_int_flow_tbl_0_tcp["flow"]["match"]["nw_proto"] = settings.TCP |
||
| 78 | utils.set_priority(new_int_flow_tbl_0_tcp) |
||
| 79 | |||
| 80 | # The flow_manager has two outputs: instructions and actions. |
||
| 81 | instructions = [ |
||
| 82 | { |
||
| 83 | "instruction_type": "apply_actions", |
||
| 84 | "actions": [{"action_type": "push_int"}], |
||
| 85 | }, |
||
| 86 | {"instruction_type": "goto_table", "table_id": settings.INT_TABLE}, |
||
| 87 | ] |
||
| 88 | new_int_flow_tbl_0_tcp["flow"]["instructions"] = instructions |
||
| 89 | |||
| 90 | # Prepare UDP Flow for Table 0. Everything the same as TCP except the nw_proto |
||
| 91 | new_int_flow_tbl_0_udp = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
| 92 | new_int_flow_tbl_0_udp["flow"]["match"]["nw_proto"] = settings.UDP |
||
| 93 | |||
| 94 | # Prepare Flows for Table 2 - No TCP or UDP specifics |
||
| 95 | new_int_flow_tbl_2["flow"]["table_id"] = settings.INT_TABLE |
||
| 96 | utils.set_table_group(new_int_flow_tbl_2) |
||
| 97 | |||
| 98 | # if intra-switch EVC, then output port should be the proxy |
||
| 99 | if utils.is_intra_switch_evc(evc): |
||
| 100 | for instruction in new_int_flow_tbl_2["flow"]["instructions"]: |
||
| 101 | if instruction["instruction_type"] == "apply_actions": |
||
| 102 | for action in instruction["actions"]: |
||
| 103 | if action["action_type"] == "output": |
||
| 104 | # Since this is the INT Source, we use source |
||
| 105 | # to avoid worrying about single or multi |
||
| 106 | # home physical loops. |
||
| 107 | # The choice for destination is at the INT Sink. |
||
| 108 | action["port"] = proxy_port.source.port_number |
||
| 109 | |||
| 110 | instructions = utils.add_to_apply_actions( |
||
| 111 | new_int_flow_tbl_2["flow"]["instructions"], |
||
| 112 | new_instruction={"action_type": "add_int_metadata"}, |
||
| 113 | position=0, |
||
| 114 | ) |
||
| 115 | |||
| 116 | new_int_flow_tbl_2["flow"]["instructions"] = instructions |
||
| 117 | |||
| 118 | new_flows.append(new_int_flow_tbl_0_tcp) |
||
| 119 | new_flows.append(new_int_flow_tbl_0_udp) |
||
| 120 | new_flows.append(new_int_flow_tbl_2) |
||
| 121 | |||
| 122 | return new_flows |
||
| 123 | |||
| 265 |