A few small refactors, using get_by_name() more, casting boolean return values, etc
This commit is contained in:
		| @@ -24,9 +24,9 @@ def get_parent_by_name(yaml, ifname): | |||||||
|     """ Returns the sub-interface's parent, or None if the sub-int doesn't exist. """ |     """ Returns the sub-interface's parent, or None if the sub-int doesn't exist. """ | ||||||
|     if not '.' in ifname: |     if not '.' in ifname: | ||||||
|         return None |         return None | ||||||
|  |     try: | ||||||
|         ifname, subid = ifname.split('.') |         ifname, subid = ifname.split('.') | ||||||
|         subid = int(subid) |         subid = int(subid) | ||||||
|     try: |  | ||||||
|         iface = yaml['interfaces'][ifname] |         iface = yaml['interfaces'][ifname] | ||||||
|         return iface |         return iface | ||||||
|     except: |     except: | ||||||
| @@ -37,9 +37,9 @@ def get_parent_by_name(yaml, ifname): | |||||||
| def get_by_name(yaml, ifname): | def get_by_name(yaml, ifname): | ||||||
|     """ Returns the interface or sub-interface by a given name, or None if it does not exist """ |     """ Returns the interface or sub-interface by a given name, or None if it does not exist """ | ||||||
|     if '.' in ifname: |     if '.' in ifname: | ||||||
|  |         try: | ||||||
|             ifname, subid = ifname.split('.') |             ifname, subid = ifname.split('.') | ||||||
|             subid = int(subid) |             subid = int(subid) | ||||||
|         try: |  | ||||||
|             iface = yaml['interfaces'][ifname]['sub-interfaces'][subid] |             iface = yaml['interfaces'][ifname]['sub-interfaces'][subid] | ||||||
|             return iface |             return iface | ||||||
|         except: |         except: | ||||||
| @@ -53,6 +53,12 @@ def get_by_name(yaml, ifname): | |||||||
|     return None |     return None | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def is_sub(yaml, ifname): | ||||||
|  |     """ Returns True if this interface is a sub-interface """ | ||||||
|  |     parent_iface = get_parent_by_name(yaml, ifname) | ||||||
|  |     return isinstance(parent_iface, dict) | ||||||
|  |  | ||||||
|  |  | ||||||
| def has_sub(yaml, ifname): | def has_sub(yaml, ifname): | ||||||
|     """ Returns True if this interface has sub-interfaces """ |     """ Returns True if this interface has sub-interfaces """ | ||||||
|     if not 'interfaces' in yaml: |     if not 'interfaces' in yaml: | ||||||
| @@ -67,25 +73,11 @@ def has_sub(yaml, ifname): | |||||||
|  |  | ||||||
| def has_address(yaml, ifname): | def has_address(yaml, ifname): | ||||||
|     """ Returns True if this interface or sub-interface has one or more addresses""" |     """ Returns True if this interface or sub-interface has one or more addresses""" | ||||||
|     if not 'interfaces' in yaml: |  | ||||||
|         return False |  | ||||||
|  |  | ||||||
|     if '.' in ifname: |     iface = get_by_name(yaml, ifname) | ||||||
|         ifname, subid = ifname.split('.') |     if not iface: | ||||||
|         subid = int(subid) |  | ||||||
|         try: |  | ||||||
|             if len(yaml['interfaces'][ifname]['sub-interfaces'][subid]['addresses']) > 0: |  | ||||||
|                 return True |  | ||||||
|         except: |  | ||||||
|             pass |  | ||||||
|         return False |  | ||||||
|  |  | ||||||
|     try: |  | ||||||
|         if len(yaml['interfaces'][ifname]['addresses']) > 0: |  | ||||||
|             return True |  | ||||||
|     except: |  | ||||||
|         pass |  | ||||||
|         return False |         return False | ||||||
|  |     return 'addresses' in iface | ||||||
|  |  | ||||||
|  |  | ||||||
| def is_bond_member(yaml, ifname): | def is_bond_member(yaml, ifname): | ||||||
| @@ -118,19 +110,13 @@ def is_bridge_interface_unique(yaml, ifname): | |||||||
|     """ Returns True if this interface is referenced in bridgedomains zero or one times """ |     """ Returns True if this interface is referenced in bridgedomains zero or one times """ | ||||||
|  |  | ||||||
|     ifs = get_bridge_interfaces(yaml) |     ifs = get_bridge_interfaces(yaml) | ||||||
|     n = ifs.count(ifname) |     return ifs.count(ifname) < 2 | ||||||
|  |  | ||||||
|     if n == 0 or n == 1: |  | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def is_bridge_interface(yaml, ifname): | def is_bridge_interface(yaml, ifname): | ||||||
|     """ Returns True if this interface is a member of a BridgeDomain """ |     """ Returns True if this interface is a member of a BridgeDomain """ | ||||||
|  |  | ||||||
|     if ifname in get_bridge_interfaces(yaml): |     return ifname in get_bridge_interfaces(yaml) | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_l2xc_interfaces(yaml): | def get_l2xc_interfaces(yaml): | ||||||
| @@ -152,9 +138,7 @@ def get_l2xc_interfaces(yaml): | |||||||
| def is_l2xc_interface(yaml, ifname): | def is_l2xc_interface(yaml, ifname): | ||||||
|     """ Returns True if this interface has an L2 CrossConnect """ |     """ Returns True if this interface has an L2 CrossConnect """ | ||||||
|  |  | ||||||
|     if ifname in get_l2xc_interfaces(yaml): |     return ifname in get_l2xc_interfaces(yaml) | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_l2xc_target_interfaces(yaml): | def get_l2xc_target_interfaces(yaml): | ||||||
| @@ -175,43 +159,23 @@ def get_l2xc_target_interfaces(yaml): | |||||||
| def is_l2xc_target_interface(yaml, ifname): | def is_l2xc_target_interface(yaml, ifname): | ||||||
|     """ Returns True if this interface is the target of an L2 CrossConnect """ |     """ Returns True if this interface is the target of an L2 CrossConnect """ | ||||||
|  |  | ||||||
|     if ifname in get_l2xc_target_interfaces(yaml): |     return ifname in get_l2xc_target_interfaces(yaml) | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def is_l2xc_target_interface_unique(yaml, ifname): | def is_l2xc_target_interface_unique(yaml, ifname): | ||||||
|     """ Returns True if this interface is referenced as an l2xc target zero or one times """ |     """ Returns True if this interface is referenced as an l2xc target zero or one times """ | ||||||
|  |  | ||||||
|     ifs = get_l2xc_target_interfaces(yaml) |     ifs = get_l2xc_target_interfaces(yaml) | ||||||
|     n = ifs.count(ifname) |     return ifs.count(ifname) < 2 | ||||||
|  |  | ||||||
|     if n == 0 or n == 1: |  | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def has_lcp(yaml, ifname): | def has_lcp(yaml, ifname): | ||||||
|     """ Returns True if this interface or sub-interface has an LCP """ |     """ Returns True if this interface or sub-interface has an LCP """ | ||||||
|     if not 'interfaces' in yaml: |  | ||||||
|         return False |  | ||||||
|  |  | ||||||
|     if '.' in ifname: |     iface = get_by_name(yaml, ifname) | ||||||
|         ifname, subid = ifname.split('.') |     if not iface: | ||||||
|         subid = int(subid) |  | ||||||
|         try: |  | ||||||
|             if len(yaml['interfaces'][ifname]['sub-interfaces'][subid]['lcp']) > 0: |  | ||||||
|                 return True |  | ||||||
|         except: |  | ||||||
|             pass |  | ||||||
|         return False |  | ||||||
|  |  | ||||||
|     try: |  | ||||||
|         if len(yaml['interfaces'][ifname]['lcp']) > 0: |  | ||||||
|             return True |  | ||||||
|     except: |  | ||||||
|         pass |  | ||||||
|         return False |         return False | ||||||
|  |     return 'lcp' in iface | ||||||
|  |  | ||||||
|  |  | ||||||
| def unique_encapsulation(yaml, sub_ifname): | def unique_encapsulation(yaml, sub_ifname): | ||||||
| @@ -255,18 +219,11 @@ def unique_encapsulation(yaml, sub_ifname): | |||||||
|             ## print("%s overlaps with %s: [%d,%d,%d]" % (sub_ifname, sibling_ifname, dot1q, dot1ad, inner_dot1q)) |             ## print("%s overlaps with %s: [%d,%d,%d]" % (sub_ifname, sibling_ifname, dot1q, dot1ad, inner_dot1q)) | ||||||
|             ncount = ncount + 1 |             ncount = ncount + 1 | ||||||
|  |  | ||||||
|     if (ncount == 0): |     return ncount == 0 | ||||||
|         return True |  | ||||||
|     return False |  | ||||||
|  |  | ||||||
| def valid_encapsulation(yaml, sub_ifname): | def valid_encapsulation(yaml, sub_ifname): | ||||||
|     try: |     """ Returns True if the sub interface has a valid encapsulation """ | ||||||
|         ifname, subid = sub_ifname.split('.') |     sub_iface = get_by_name(yaml, sub_ifname) | ||||||
|         subid = int(subid) |  | ||||||
|         sub_iface = yaml['interfaces'][ifname]['sub-interfaces'][subid] |  | ||||||
|     except: |  | ||||||
|         return False |  | ||||||
|  |  | ||||||
|     if not 'encapsulation' in sub_iface: |     if not 'encapsulation' in sub_iface: | ||||||
|         return True |         return True | ||||||
|  |  | ||||||
|   | |||||||
| @@ -32,6 +32,4 @@ def is_unique(yaml, lcpname): | |||||||
|         for ifname, iface in yaml['bridgedomains'].items(): |         for ifname, iface in yaml['bridgedomains'].items(): | ||||||
|             if 'lcp' in iface and iface['lcp'] == lcpname: |             if 'lcp' in iface and iface['lcp'] == lcpname: | ||||||
|                 ncount = ncount + 1 |                 ncount = ncount + 1 | ||||||
|     if ncount > 1: |     return ncount < 2 | ||||||
|         return False |  | ||||||
|     return True |  | ||||||
|   | |||||||
| @@ -39,9 +39,7 @@ def vni_unique(yaml, vni): | |||||||
|         if iface['vni'] == vni: |         if iface['vni'] == vni: | ||||||
|             ncount = ncount + 1 |             ncount = ncount + 1 | ||||||
|  |  | ||||||
|     if ncount > 1: |     return ncount < 2 | ||||||
|         return False |  | ||||||
|     return True |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def validate_vxlan_tunnels(yaml): | def validate_vxlan_tunnels(yaml): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user