def get_neighbor_ids(trie, node_id): """ Given the node ID, get the payloads from the net trie and return the attached network and neighbor node IDs. :param trie: net data trie :param node_id: node ID to lookup :return: tuple of net and node IDs """ from node_tools.ctlr_funcs import ipnet_get_netcfg from node_tools.ctlr_funcs import is_exit_node from node_tools.ctlr_funcs import netcfg_get_ipnet from node_tools.helper_funcs import find_ipv4_iface node_list = [] key_list = [] src_net = None exit_net = None src_node = None exit_node = None for key in trie: if node_id in key: key_list.append(key[0:16]) node_list.append(trie[key]) if len(key_list) != 2 and (len(key_list) == 1 and not is_exit_node(node_id)): raise AssertionError('Node {} keys {} are invalid!'.format( node_id, key_list)) else: for key, data in zip(key_list, node_list): node_ip = data['ipAssignments'][0] ipnet = netcfg_get_ipnet(node_ip) netcfg = ipnet_get_netcfg(ipnet) gw_ip = find_ipv4_iface(netcfg.gateway[0]) if node_ip == gw_ip: src_net = key for node in trie.suffixes(src_net)[1:]: if node_id != node: src_node = node else: exit_net = key for node in trie.suffixes(exit_net)[1:]: if node_id != node: exit_node = node return src_net, exit_net, src_node, exit_node
def get_dangling_net_data(trie, net_id): """ Given the net ID, get the payload from the net data trie and return the target network cfg from the `routes` list. :notes: The return format matches the cfg_dict payload used by config_network_object() :param trie: net data trie :param net_id: network ID to retrive :return: Attrdict <netcfg> or None """ from node_tools.ctlr_funcs import ipnet_get_netcfg from node_tools.ctlr_funcs import netcfg_get_ipnet payload = trie[net_id] # logger.debug('TRIE: net {} has payload {}'.format(net_id, payload)) netcfg = None for route in payload['routes']: if route['via'] is not None: tgt_route = route['via'] netobj = netcfg_get_ipnet(tgt_route) netcfg = ipnet_get_netcfg(netobj) return netcfg
def test_get_invalid_cfg(self): """Raise IPv4Network ValueError""" with self.assertRaises(ValueError): res = ipnet_get_netcfg('172.16.0.0/30')
def test_get_valid_cfg(self): netobj = ipaddress.ip_network('172.16.0.0/30') res = ipnet_get_netcfg(netobj) self.assertIsInstance(res, AttrDict) self.assertEqual(res.host, ['172.16.0.2/30'])