Example #1
0
def handle_net_cfg(deque):
    """
    Handle the initial net_cfg for a (new) member node. Required format
    derived from async wrapper funcs.  Context is netstate runner and
    bootstrap_mbr_node.
    :param deque: netobj queue
    :return: tuple of formatted cfg fragments
    """
    with deque.transact():
        ipnet = deque.popleft()
        deque.append(ipnet)

    netcfg = ipnet_get_netcfg(ipnet)
    gw_ip = find_ipv4_iface(netcfg.gateway[0])
    src_ip = find_ipv4_iface(netcfg.host[0])

    ip_range = [{
        'ipRangeStart': '{}'.format(gw_ip),
        'ipRangeEnd': '{}'.format(src_ip)
    }]

    src_addr = {'ipAssignments': netcfg.host, 'authorized': True}

    gw_addr = {'ipAssignments': netcfg.gateway, 'authorized': True}

    net_defs = {'routes': netcfg.net_routes, 'ipAssignmentPools': ip_range}

    mbr_ip = AttrDict.from_nested_dict(src_addr)
    gw_ip = AttrDict.from_nested_dict(gw_addr)
    ip_net = AttrDict.from_nested_dict(net_defs)

    return ip_net, mbr_ip, gw_ip
Example #2
0
def get_net_status(cache):
    """
    Get user node status data for 'network' endpoint from cache, return
    list of dictionaries.
    NOTE: Not valid for networks under the 'controller' endpoint.
    """
    networks = []  # list of network objects
    key_list, values = get_endpoint_data(cache, 'net')
    if key_list:
        for key, data in zip(key_list, values):
            # we need to check for missing route list here
            if data.routes:
                for addr in data.assignedAddresses:
                    if find_ipv4_iface(addr, False):
                        zt_addr = find_ipv4_iface(addr)
                        break
                netStatus = {
                    'identity': data.id,
                    'status': data.status,
                    'mac': data.mac,
                    'ztdevice': data.portDeviceName,
                    'ztaddress': zt_addr,
                    'gateway': data.routes[1]['via']
                }
            else:
                netStatus = {
                    'identity': data.id,
                    'status': data.status,
                    'mac': data.mac,
                    'ztdevice': data.portDeviceName
                }
            networks.append(netStatus)
        logger.debug('netStatus list: {}'.format(networks))
    return networks
Example #3
0
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
Example #4
0
def netcfg_get_ipnet(addr, cidr='/30'):
    """
    Process member host or gateway addr string into the (python)
    network object it belongs to.  We also assume/require the CIDR
    prefix for `addr` == /30 to be compatible with gen_netobj_queue().
    :param addr: IPv4 address string without mask
    :param cidr: network prefix
    :return: <netobj> network object for host_addr
    :raises: AddressValueError
    """
    import ipaddress as ip
    from node_tools.helper_funcs import find_ipv4_iface

    if find_ipv4_iface(addr + cidr, strip=False):
        netobj = ip.ip_network(addr + cidr, strict=False)
        return netobj
    else:
        raise ip.AddressValueError
Example #5
0
 def test_bogus(self):
     """Return False if IPv4 addr is not valid"""
     bogus_addr = find_ipv4_iface('192.168.1.300/24', False)
     self.assertFalse(bogus_addr)
Example #6
0
 def test_nostrip(self):
     """Return True if IPv4 addr is valid"""
     nostrip = find_ipv4_iface('172.16.0.241' + '/30', False)
     self.assertTrue(nostrip)
Example #7
0
 def test_strip(self):
     """Return IPv4 addr without prefix"""
     strip = find_ipv4_iface('192.168.1.1/24')
     self.assertEqual(strip, '192.168.1.1')