Conditions | 11 |
Total Lines | 93 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 122.943 |
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 | 1 | 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 | |||
54 | # Get the original flows |
||
55 | dpid = src_uni["switch"] |
||
56 | for flow in stored_flows[utils.get_cookie(evc["id"], settings.MEF_COOKIE_PREFIX)]: |
||
57 | if ( |
||
58 | flow["switch"] == dpid |
||
59 | and flow["flow"]["match"]["in_port"] == src_uni["port_number"] |
||
60 | ): |
||
61 | new_int_flow_tbl_0_tcp = copy.deepcopy(flow) |
||
62 | break |
||
63 | |||
64 | if not new_int_flow_tbl_0_tcp: |
||
65 | raise FlowsNotFound(evc["id"]) |
||
66 | |||
67 | utils.set_instructions_from_actions(new_int_flow_tbl_0_tcp) |
||
68 | utils.set_new_cookie(new_int_flow_tbl_0_tcp) |
||
69 | utils.set_owner(new_int_flow_tbl_0_tcp) |
||
70 | |||
71 | # Deepcopy to use for table 2 later |
||
72 | new_int_flow_tbl_2 = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
73 | |||
74 | # Prepare TCP Flow for Table 0 |
||
75 | new_int_flow_tbl_0_tcp["flow"]["match"]["dl_type"] = settings.IPv4 |
||
76 | new_int_flow_tbl_0_tcp["flow"]["match"]["nw_proto"] = settings.TCP |
||
77 | utils.set_priority(new_int_flow_tbl_0_tcp) |
||
78 | |||
79 | # The flow_manager has two outputs: instructions and actions. |
||
80 | instructions = [ |
||
81 | { |
||
82 | "instruction_type": "apply_actions", |
||
83 | "actions": [{"action_type": "push_int"}], |
||
84 | }, |
||
85 | {"instruction_type": "goto_table", "table_id": settings.INT_TABLE}, |
||
86 | ] |
||
87 | new_int_flow_tbl_0_tcp["flow"]["instructions"] = instructions |
||
88 | |||
89 | # Prepare UDP Flow for Table 0. Everything the same as TCP except the nw_proto |
||
90 | new_int_flow_tbl_0_udp = copy.deepcopy(new_int_flow_tbl_0_tcp) |
||
91 | new_int_flow_tbl_0_udp["flow"]["match"]["nw_proto"] = settings.UDP |
||
92 | |||
93 | # Prepare Flows for Table 2 - No TCP or UDP specifics |
||
94 | new_int_flow_tbl_2["flow"]["table_id"] = settings.INT_TABLE |
||
95 | utils.set_table_group(new_int_flow_tbl_2) |
||
96 | |||
97 | # if intra-switch EVC, then output port should be the dst UNI's source port |
||
98 | if utils.is_intra_switch_evc(evc): |
||
99 | dst_uni = evc["uni_z" if uni_src_key == "uni_a" else "uni_a"] |
||
100 | proxy_port = dst_uni["proxy_port"] |
||
101 | for instruction in new_int_flow_tbl_2["flow"]["instructions"]: |
||
102 | if instruction["instruction_type"] == "apply_actions": |
||
103 | for action in instruction["actions"]: |
||
104 | if action["action_type"] == "output": |
||
105 | # Since this is the INT Source, we use source |
||
106 | # to avoid worrying about single or multi |
||
107 | # home physical loops. |
||
108 | # The choice for destination is at the INT Sink. |
||
109 | action["port"] = proxy_port.source.port_number |
||
110 | |||
111 | # remove set_vlan action if it exists, this is for |
||
112 | # avoding a redundant set_vlan since it'll be set in the egress sink |
||
113 | instruction["actions"] = utils.modify_actions( |
||
114 | instruction["actions"], ["set_vlan"], remove=True |
||
115 | ) |
||
116 | |||
117 | instructions = utils.add_to_apply_actions( |
||
118 | new_int_flow_tbl_2["flow"]["instructions"], |
||
119 | new_instruction={"action_type": "add_int_metadata"}, |
||
120 | position=0, |
||
121 | ) |
||
122 | |||
123 | new_int_flow_tbl_2["flow"]["instructions"] = instructions |
||
124 | |||
125 | new_flows.append(new_int_flow_tbl_0_tcp) |
||
126 | new_flows.append(new_int_flow_tbl_0_udp) |
||
127 | new_flows.append(new_int_flow_tbl_2) |
||
128 | |||
129 | return new_flows |
||
130 | |||
272 |