Conditions | 42 |
Total Lines | 83 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 1739.1257 |
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.match.match10() 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 | """Switch match.""" |
||
37 | 1 | def match10(flow_dict, args): |
|
38 | """Match a packet against this flow (OF1.0).""" |
||
39 | match_fields = _get_match_fields(flow_dict) |
||
40 | wildcards = match_fields.get('wildcards') |
||
41 | if not wildcards & FlowWildCards.OFPFW_IN_PORT: |
||
42 | if 'in_port' not in args: |
||
43 | return False |
||
44 | if match_fields.get('in_port') != int(args.get('in_port')): |
||
45 | return False |
||
46 | if not wildcards & FlowWildCards.OFPFW_DL_VLAN_PCP: |
||
47 | if 'vlan_pcp' not in args: |
||
48 | return False |
||
49 | if match_fields.get('vlan_pcp') != int(args.get('vlan_pcp')): |
||
50 | return False |
||
51 | if not wildcards & FlowWildCards.OFPFW_DL_VLAN: |
||
52 | if 'vlan_vid' not in args: |
||
53 | return False |
||
54 | if match_fields.get('vlan_vid') != args.get('vlan_vid')[-1]: |
||
55 | return False |
||
56 | if not wildcards & FlowWildCards.OFPFW_DL_SRC: |
||
57 | if 'eth_src' not in args: |
||
58 | return False |
||
59 | if match_fields.get('eth_src') != args.get('eth_src'): |
||
60 | return False |
||
61 | if not wildcards & FlowWildCards.OFPFW_DL_DST: |
||
62 | if 'eth_dst' not in args: |
||
63 | return False |
||
64 | if match_fields.get('eth_dst') != args.get('eth_dst'): |
||
65 | return False |
||
66 | if not wildcards & FlowWildCards.OFPFW_DL_TYPE: |
||
67 | if 'eth_type' not in args: |
||
68 | return False |
||
69 | if match_fields.get('eth_type') != int(args.get('eth_type')): |
||
70 | return False |
||
71 | if not match_fields['eth_type'] == IPV4_ETH_TYPE: |
||
72 | return False |
||
73 | flow_ip_int = int(ipaddress.IPv4Address(match_fields.get('ipv4_src'))) |
||
74 | if flow_ip_int != 0: |
||
75 | mask = (wildcards |
||
76 | & FlowWildCards.OFPFW_NW_SRC_MASK) >> \ |
||
77 | FlowWildCards.OFPFW_NW_SRC_SHIFT |
||
78 | if mask > 32: |
||
79 | mask = 32 |
||
80 | if mask != 32 and 'ipv4_src' not in args: |
||
81 | return False |
||
82 | mask = (0xffffffff << mask) & 0xffffffff |
||
83 | ip_int = int(ipaddress.IPv4Address(args.get('ipv4_src'))) |
||
84 | if ip_int & mask != flow_ip_int & mask: |
||
85 | return False |
||
86 | flow_ip_int = int(ipaddress.IPv4Address(match_fields['ipv4_dst'])) |
||
87 | if flow_ip_int != 0: |
||
88 | mask = (wildcards |
||
89 | & FlowWildCards.OFPFW_NW_DST_MASK) >> \ |
||
90 | FlowWildCards.OFPFW_NW_DST_SHIFT |
||
91 | if mask > 32: |
||
92 | mask = 32 |
||
93 | if mask != 32 and 'ipv4_dst' not in args: |
||
94 | return False |
||
95 | mask = (0xffffffff << mask) & 0xffffffff |
||
96 | ip_int = int(ipaddress.IPv4Address(args.get('ipv4_dst'))) |
||
97 | if ip_int & mask != flow_ip_int & mask: |
||
98 | return False |
||
99 | if not wildcards & FlowWildCards.OFPFW_NW_TOS: |
||
100 | if 'ip_tos' not in args: |
||
101 | return False |
||
102 | if match_fields.get('ip_tos') != int(args.get('ip_tos')): |
||
103 | return False |
||
104 | if not wildcards & FlowWildCards.OFPFW_NW_PROTO: |
||
105 | if 'ip_proto' not in args: |
||
106 | return False |
||
107 | if match_fields.get('ip_proto') != int(args.get('ip_proto')): |
||
108 | return False |
||
109 | if not wildcards & FlowWildCards.OFPFW_TP_SRC: |
||
110 | if 'tp_src' not in args: |
||
111 | return False |
||
112 | if match_fields.get('tcp_src') != int(args.get('tp_src')): |
||
113 | return False |
||
114 | if not wildcards & FlowWildCards.OFPFW_TP_DST: |
||
115 | if 'tp_dst' not in args: |
||
116 | return False |
||
117 | if match_fields.get('tcp_dst') != int(args.get('tp_dst')): |
||
118 | return False |
||
119 | return flow_dict |
||
120 | |||
166 |