def buildTree(start_net, end_net=None, bits_in_matrix=0, add_missing_nets=False, forceBuild=False): """Builds a tree from start_net to (and included) end_net. Arguments: start_net: IPy.IP instance of the starting point forceBuild: Force tree build instead of using cached result Returns: A tree (using a hash map) This module was originally implemented for the prefix matrix which needed three additional options: end_net: The last net to be shown in the matrix bits_in_matrix: Number of bits to exclude from the end_net IP, defaults to 0 add_missing_nets: If a leaf nodes' parent does not have prefix length == (end_net - bits_in_matrix) such a parent will be added in between the leaf and the original parent. The extra parent will serve as a 'foreign key' later on when the prefix matrix splits the tree on all nodes with prefix length = end_net.prefixlen() - bits_in_matrix. defaults to False """ result = {start_net: {}} subnets = getSubnets(start_net) sorted_subnets = sort_nets_by_prefixlength(subnets) #TODO: Reimplement this to respect that the list is allready sorted, # that way we won't have to sort the list again. if add_missing_nets and bits_in_matrix > 0: mask = getMask(start_net.version(), end_net.prefixlen() - bits_in_matrix) for ip in sorted_subnets: if ip.prefixlen() <= mask.prefixlen(): continue supernet = andIpMask(ip, mask) if supernet not in sorted_subnets: sorted_subnets.append(supernet) sorted_subnets = sort_nets_by_prefixlength(sorted_subnets) #build the tree for ip in sorted_subnets: insertIntoTree(result, ip) return result
def buildTree(start_net, end_net=None, bits_in_matrix=0, add_missing_nets=False, forceBuild=False): """Builds a tree from start_net to (and included) end_net. Arguments: start_net: IPy.IP instance of the starting point forceBuild: Force tree build instead of using cached result Returns: A tree (using a hash map) This module was originally implemented for the prefix matrix which needed three additional options: end_net: The last net to be shown in the matrix bits_in_matrix: Number of bits to exclude from the end_net IP, defaults to 0 add_missing_nets: If a leaf nodes' parent does not have prefix length == (end_net - bits_in_matrix) such a parent will be added in between the leaf and the original parent. The extra parent will serve as a 'foreign key' later on when the prefix matrix splits the tree on all nodes with prefix length = end_net.prefixlen() - bits_in_matrix. defaults to False """ result = {start_net:{}} subnets = getSubnets(start_net) sorted_subnets = sort_nets_by_prefixlength(subnets) #TODO: Reimplement this to respect that the list is allready sorted, # that way we won't have to sort the list again. if add_missing_nets and bits_in_matrix > 0: mask = getMask(start_net.version(), end_net.prefixlen()-bits_in_matrix) for ip in sorted_subnets: if ip.prefixlen() <= mask.prefixlen(): continue supernet = andIpMask(ip, mask) if supernet not in sorted_subnets: sorted_subnets.append(supernet) sorted_subnets = sort_nets_by_prefixlength(sorted_subnets) #build the tree for ip in sorted_subnets: insertIntoTree(result, ip) return result
def test_ipv6_andIpMask(self): ip = IP('fe80:1234:5678:9012:a00:27ff:fe8e:df69') mask = IP('fe80:5678::/64') expected = IP('fe80:1230::/64') self.assertEqual(andIpMask(ip, mask), expected)