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