| Conditions | 12 |
| Total Lines | 99 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 42 |
| CRAP Score | 12 |
| 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_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.""" |
||
| 45 | 1 | def _build_int_source_flows( |
|
| 46 | self, |
||
| 47 | uni_src_key: Literal["uni_a", "uni_z"], |
||
| 48 | evc: dict, |
||
| 49 | stored_flows: dict[int, list[dict]], |
||
| 50 | ) -> list[dict]: |
||
| 51 | """Build INT source flows. |
||
| 52 | |||
| 53 | At the INT source, one flow becomes 3: one for UDP on table 0, |
||
| 54 | one for TCP on table 0, and one on table X (2 for evpl and 3 for epl by default) |
||
| 55 | On table 0, we use just new instructions: push_int and goto_table |
||
| 56 | On table X, we add add_int_metadata before the original actions |
||
| 57 | INT flows will have higher priority. |
||
| 58 | """ |
||
| 59 | 1 | new_flows = [] |
|
| 60 | 1 | new_int_flow_tbl_0_tcp = {} |
|
| 61 | 1 | src_uni = evc[uni_src_key] |
|
| 62 | |||
| 63 | # Get the original flows |
||
| 64 | 1 | dpid = src_uni["switch"] |
|
| 65 | 1 | for flow in stored_flows[ |
|
| 66 | utils.get_cookie(evc["id"], settings.MEF_COOKIE_PREFIX) |
||
| 67 | ]: |
||
| 68 | 1 | if ( |
|
| 69 | flow["switch"] == dpid |
||
| 70 | and flow["flow"]["match"]["in_port"] == src_uni["port_number"] |
||
| 71 | ): |
||
| 72 | 1 | new_int_flow_tbl_0_tcp = copy.deepcopy(flow) |
|
| 73 | 1 | break |
|
| 74 | |||
| 75 | 1 | if not new_int_flow_tbl_0_tcp: |
|
| 76 | 1 | if not evc["active"]: |
|
| 77 | 1 | return [] |
|
| 78 | 1 | raise FlowsNotFound(evc["id"]) |
|
| 79 | |||
| 80 | 1 | utils.set_instructions_from_actions(new_int_flow_tbl_0_tcp) |
|
| 81 | 1 | utils.set_new_cookie(new_int_flow_tbl_0_tcp) |
|
| 82 | 1 | utils.set_owner(new_int_flow_tbl_0_tcp) |
|
| 83 | |||
| 84 | # Deepcopy to use for table X (2 or 3 by default for EVPL or EPL respectively) |
||
| 85 | 1 | new_int_flow_tbl_x = copy.deepcopy(new_int_flow_tbl_0_tcp) |
|
| 86 | |||
| 87 | # Prepare TCP Flow for Table 0 |
||
| 88 | 1 | new_int_flow_tbl_0_tcp["flow"]["match"]["dl_type"] = settings.IPv4 |
|
| 89 | 1 | new_int_flow_tbl_0_tcp["flow"]["match"]["nw_proto"] = settings.TCP |
|
| 90 | 1 | utils.set_priority(new_int_flow_tbl_0_tcp) |
|
| 91 | |||
| 92 | # The flow_manager has two outputs: instructions and actions. |
||
| 93 | 1 | table_group = self.table_group |
|
| 94 | 1 | new_table_id = table_group[new_int_flow_tbl_x["flow"]["table_group"]] |
|
| 95 | 1 | instructions = [ |
|
| 96 | { |
||
| 97 | "instruction_type": "apply_actions", |
||
| 98 | "actions": [{"action_type": "push_int"}], |
||
| 99 | }, |
||
| 100 | {"instruction_type": "goto_table", "table_id": new_table_id}, |
||
| 101 | ] |
||
| 102 | 1 | new_int_flow_tbl_0_tcp["flow"]["instructions"] = instructions |
|
| 103 | |||
| 104 | # Prepare UDP Flow for Table 0. Everything the same as TCP except the nw_proto |
||
| 105 | 1 | new_int_flow_tbl_0_udp = copy.deepcopy(new_int_flow_tbl_0_tcp) |
|
| 106 | 1 | new_int_flow_tbl_0_udp["flow"]["match"]["nw_proto"] = settings.UDP |
|
| 107 | |||
| 108 | # Prepare Flows for Table X - No TCP or UDP specifics |
||
| 109 | 1 | new_int_flow_tbl_x["flow"]["table_id"] = new_table_id |
|
| 110 | |||
| 111 | # if intra-switch EVC, then output port should be the dst UNI's source port |
||
| 112 | 1 | if utils.is_intra_switch_evc(evc): |
|
| 113 | 1 | dst_uni = evc["uni_z" if uni_src_key == "uni_a" else "uni_a"] |
|
| 114 | 1 | proxy_port = dst_uni["proxy_port"] |
|
| 115 | 1 | for instruction in new_int_flow_tbl_x["flow"]["instructions"]: |
|
| 116 | 1 | if instruction["instruction_type"] == "apply_actions": |
|
| 117 | 1 | for action in instruction["actions"]: |
|
| 118 | 1 | if action["action_type"] == "output": |
|
| 119 | # Since this is the INT Source, we use source |
||
| 120 | # to avoid worrying about single or multi |
||
| 121 | # home physical loops. |
||
| 122 | # The choice for destination is at the INT Sink. |
||
| 123 | 1 | action["port"] = proxy_port.source.port_number |
|
| 124 | |||
| 125 | # remove set_vlan action if it exists, this is for |
||
| 126 | # avoding a redundant set_vlan since it'll be set in the egress sink |
||
| 127 | 1 | instruction["actions"] = utils.modify_actions( |
|
| 128 | instruction["actions"], ["set_vlan"], remove=True |
||
| 129 | ) |
||
| 130 | |||
| 131 | 1 | instructions = utils.add_to_apply_actions( |
|
| 132 | new_int_flow_tbl_x["flow"]["instructions"], |
||
| 133 | new_instruction={"action_type": "add_int_metadata"}, |
||
| 134 | position=0, |
||
| 135 | ) |
||
| 136 | |||
| 137 | 1 | new_int_flow_tbl_x["flow"]["instructions"] = instructions |
|
| 138 | |||
| 139 | 1 | new_flows.append(new_int_flow_tbl_0_tcp) |
|
| 140 | 1 | new_flows.append(new_int_flow_tbl_0_udp) |
|
| 141 | 1 | new_flows.append(new_int_flow_tbl_x) |
|
| 142 | |||
| 143 | 1 | return new_flows |
|
| 144 | |||
| 300 |