def augment_lan_link(link, addr_pools, ndict, defaults={}): if 'prefix' in link: pfx_list = addressing.parse_prefix(link.prefix) else: pfx_list = addressing.get(addr_pools, [link.get('role'), 'lan']) link.prefix = {af: str(pfx_list[af]) for af in pfx_list} interfaces = {} for (node, value) in link.items(): if node in ndict: ifaddr = Box({}, default_box=True) if value is None: value = Box({}, default_box=True) for af, pfx in pfx_list.items(): ip = netaddr.IPNetwork(pfx[ndict[node].id]) ip.prefixlen = pfx.prefixlen value[af] = str(ip) ifaddr[af] = value[af] link[node] = value ifaddr_add_module(ifaddr, link, defaults.get('module')) if link.type != "stub": n_list = filter(lambda n: n in ndict and n != node, link.keys()) ifaddr.name = link.get("name") or ( node + " -> [" + ",".join(list(n_list)) + "]") interfaces[node] = interface_data(link=link, link_attr=link_attr_base, ifdata=ifaddr) add_node_interface(ndict[node], interfaces[node], defaults) for node in interfaces.keys(): interfaces[node].neighbors = {} for remote in interfaces.keys(): if remote != node: interfaces[node].neighbors[remote] = { \ 'ifname': interfaces[remote]['ifname'] } for af in ('ipv4', 'ipv6'): if af in interfaces[remote]: interfaces[node].neighbors[remote][af] = interfaces[ remote][af] ifindex = len(ndict[node].links) - 1 ndict[node].links[ifindex] = interfaces[node]
def augment_nodes(topology, defaults, pools): id = 0 ndict = {} for n in topology['nodes']: id = id + 1 if 'id' in n: common.error( "ERROR: static node IDs are not supported, overwriting with %d: %s" % (id, str(n))) n['id'] = id if not n.get('name'): common.error("ERROR: node does not have a name %s" % str(n)) continue if 'loopback' in pools: n['loopback'] = {} prefix_list = addressing.get(pools, ['loopback']) for af in prefix_list: if af == 'ipv6': n['loopback'][af] = get_addr_mask(prefix_list[af], 1) else: n['loopback'][af] = str(prefix_list[af]) if not 'device' in n: n['device'] = defaults.get('device') device_data = common.get_value( \ data=defaults, path=['devices',n['device']]) if not device_data: common.error("ERROR: Unsupported device type %s: %s" % (n['device'], n)) continue augment_mgmt_if(n, device_data, topology['addressing'].get('mgmt')) if 'box' in device_data: n['box'] = device_data['box'] ndict[n['name']] = n topology['nodes_map'] = ndict return ndict
def augment_lan_link(link, addr_pools, ndict, defaults={}): if 'prefix' in link: pfx_list = addressing.parse_prefix(link['prefix']) else: pfx_list = addressing.get(addr_pools, ['lan']) link['prefix'] = {af: str(pfx_list[af]) for af in pfx_list} interfaces = {} link_attr = ['bridge', 'type'] link_attr.extend(defaults.get('link_attr', [])) for (node, value) in link.items(): if node in ndict: ifaddr = {} if value is None: value = {} for af, pfx in pfx_list.items(): ip = netaddr.IPNetwork(pfx[ndict[node]['id']]) ip.prefixlen = pfx.prefixlen value[af] = str(ip) ifaddr[af] = value[af] link[node] = value ifdata = interface_data(link=link, link_attr=link_attr, ifdata=ifaddr) interfaces[node] = add_node_interface(ndict[node], ifdata, defaults) for node in interfaces.keys(): interfaces[node]['neighbors'] = {} for remote in interfaces.keys(): if remote != node: interfaces[node]['neighbors'][remote] = { \ 'ifname': interfaces[remote]['ifname'] } for af in ('ipv4', 'ipv6'): if af in interfaces[remote]: interfaces[node]['neighbors'][remote][af] = interfaces[ remote][af]
def transform(topology, defaults, pools): augment_node_images(topology) id = 0 ndict = {} for n in topology['nodes']: id = id + 1 if n.id: common.error( "ERROR: static node IDs are not supported, overwriting with %d: %s" % (id, str(n))) n.id = id if not n.name: common.error("ERROR: node does not have a name %s" % str(n)) continue if pools.loopback: prefix_list = addressing.get(pools, ['loopback']) for af in prefix_list: if af == 'ipv6': n.loopback[af] = addressing.get_addr_mask( prefix_list[af], 1) else: n.loopback[af] = str(prefix_list[af]) device_data = defaults.devices[n.device] if not device_data: common.error("ERROR: Unsupported device type %s: %s" % (n.device, n)) continue augment_mgmt_if(n, device_data, topology.addressing.mgmt) ndict[n.name] = n topology.nodes_map = ndict return ndict
def augment_p2p_link(link, addr_pools, ndict, defaults={}): if 'prefix' in link: pfx_list = addressing.parse_prefix(link['prefix']) else: pfx_list = addressing.get(addr_pools, ['p2p', 'lan']) link['prefix'] = {af: str(pfx_list[af]) for af in pfx_list} end_names = ['left', 'right'] nodes = [] interfaces = [] for (node, value) in link.items(): if node in ndict: ecount = len(nodes) ifaddr = {} if value is None: value = {} for af, pfx in pfx_list.items(): ip = netaddr.IPNetwork(pfx[ecount + 1]) ip.prefixlen = pfx.prefixlen value[af] = str(ip) ifaddr[af] = value[af] link[node] = value nodes.append({'name': node, 'link': value, 'ifaddr': ifaddr}) if len(nodes) > len(end_names): print("Too many nodes specified on a P2P link") return link_attr = ['bridge', 'type'] link_attr.extend(defaults.get('link_attr', [])) for i in range(0, len(nodes)): node = nodes[i]['name'] ifdata = interface_data(link=link, link_attr=link_attr, ifdata=nodes[i]['ifaddr']) interfaces.append(add_node_interface(ndict[node], ifdata, defaults)) for i in range(0, 2): if 'bridge' in link: interfaces[i]['bridge'] = link['bridge'] else: interfaces[i]['remote_id'] = ndict[nodes[1 - i]['name']]['id'] interfaces[i]['remote_ifindex'] = interfaces[1 - i]['ifindex'] link[end_names[i]] = { 'node': nodes[i]['name'], 'ifname': interfaces[i].get('ifname') } remote = nodes[1 - i]['name'] interfaces[i]['neighbors'] = { remote : { \ 'ifname' : interfaces[1-i]['ifname'] }} for af in ('ipv4', 'ipv6'): if af in interfaces[1 - i]: interfaces[i]['neighbors'][remote][af] = interfaces[1 - i][af] if af in interfaces[i]: link[end_names[i]][af] = interfaces[i][af] return link