def build_topo_from_json(tgen, topo): """ Reads configuration from JSON file. Adds routers, creates interface names dynamically and link routers as defined in JSON to create topology. Assigns IPs dynamically to all interfaces of each router. * `tgen`: Topogen object * `topo`: json file data """ ROUTER_LIST = sorted(topo["routers"].keys(), key=lambda x: int(re_search("\d+", x).group(0))) SWITCH_LIST = [] if "switches" in topo: SWITCH_LIST = sorted(topo["switches"].keys(), key=lambda x: int(re_search("\d+", x).group(0))) listRouters = ROUTER_LIST[:] listSwitches = SWITCH_LIST[:] listAllRouters = deepcopy(listRouters) dictSwitches = {} for routerN in ROUTER_LIST: logger.info("Topo: Add router {}".format(routerN)) tgen.add_router(routerN) listRouters.append(routerN) for switchN in SWITCH_LIST: logger.info("Topo: Add switch {}".format(switchN)) dictSwitches[switchN] = tgen.add_switch(switchN) listSwitches.append(switchN) if "ipv4base" in topo: ipv4Next = ipaddress.IPv4Address(topo["link_ip_start"]["ipv4"]) ipv4Step = 2**(32 - topo["link_ip_start"]["v4mask"]) if topo["link_ip_start"]["v4mask"] < 32: ipv4Next += 1 if "ipv6base" in topo: ipv6Next = ipaddress.IPv6Address(topo["link_ip_start"]["ipv6"]) ipv6Step = 2**(128 - topo["link_ip_start"]["v6mask"]) if topo["link_ip_start"]["v6mask"] < 127: ipv6Next += 1 for router in listRouters: topo["routers"][router]["nextIfname"] = 0 while listRouters != []: curRouter = listRouters.pop(0) # Physical Interfaces if "links" in topo["routers"][curRouter]: def link_sort(x): if x == "lo": return 0 elif "link" in x: return int(x.split("-link")[1]) else: return int(re_search("\d+", x).group(0)) for destRouterLink, data in sorted( topo["routers"][curRouter]["links"].items(), key=lambda x: link_sort(x[0]), ): currRouter_lo_json = topo["routers"][curRouter]["links"][ destRouterLink] # Loopback interfaces if "type" in data and data["type"] == "loopback": if ("ipv4" in currRouter_lo_json and currRouter_lo_json["ipv4"] == "auto"): currRouter_lo_json["ipv4"] = "{}{}.{}/{}".format( topo["lo_prefix"]["ipv4"], number_to_row(curRouter), number_to_column(curRouter), topo["lo_prefix"]["v4mask"], ) if ("ipv6" in currRouter_lo_json and currRouter_lo_json["ipv6"] == "auto"): currRouter_lo_json["ipv6"] = "{}{}:{}/{}".format( topo["lo_prefix"]["ipv6"], number_to_row(curRouter), number_to_column(curRouter), topo["lo_prefix"]["v6mask"], ) if "-" in destRouterLink: # Spliting and storing destRouterLink data in tempList tempList = destRouterLink.split("-") # destRouter destRouter = tempList.pop(0) # Current Router Link tempList.insert(0, curRouter) curRouterLink = "-".join(tempList) else: destRouter = destRouterLink curRouterLink = curRouter if destRouter in listRouters: currRouter_link_json = topo["routers"][curRouter]["links"][ destRouterLink] destRouter_link_json = topo["routers"][destRouter][ "links"][curRouterLink] # Assigning name to interfaces currRouter_link_json["interface"] = "{}-{}-eth{}".format( curRouter, destRouter, topo["routers"][curRouter]["nextIfname"]) destRouter_link_json["interface"] = "{}-{}-eth{}".format( destRouter, curRouter, topo["routers"][destRouter]["nextIfname"]) topo["routers"][curRouter]["nextIfname"] += 1 topo["routers"][destRouter]["nextIfname"] += 1 # Linking routers to each other as defined in JSON file tgen.gears[curRouter].add_link( tgen.gears[destRouter], topo["routers"][curRouter]["links"][destRouterLink] ["interface"], topo["routers"][destRouter]["links"][curRouterLink] ["interface"], ) # IPv4 if "ipv4" in currRouter_link_json: if currRouter_link_json["ipv4"] == "auto": currRouter_link_json["ipv4"] = "{}/{}".format( ipv4Next, topo["link_ip_start"]["v4mask"]) destRouter_link_json["ipv4"] = "{}/{}".format( ipv4Next + 1, topo["link_ip_start"]["v4mask"]) ipv4Next += ipv4Step # IPv6 if "ipv6" in currRouter_link_json: if currRouter_link_json["ipv6"] == "auto": currRouter_link_json["ipv6"] = "{}/{}".format( ipv6Next, topo["link_ip_start"]["v6mask"]) destRouter_link_json["ipv6"] = "{}/{}".format( ipv6Next + 1, topo["link_ip_start"]["v6mask"]) ipv6Next = ipaddress.IPv6Address( int(ipv6Next) + ipv6Step) logger.debug( "Generated link data for router: %s\n%s", curRouter, json_dumps(topo["routers"][curRouter]["links"], indent=4, sort_keys=True), ) switch_count = 0 add_switch_to_topo = [] while listSwitches != []: curSwitch = listSwitches.pop(0) # Physical Interfaces if "links" in topo['switches'][curSwitch]: for destRouterLink, data in sorted( topo['switches'][curSwitch]['links'].iteritems()): # Loopback interfaces if "dst_node" in data: destRouter = data['dst_node'] elif "-" in destRouterLink: # Spliting and storing destRouterLink data in tempList tempList = destRouterLink.split("-") # destRouter destRouter = tempList.pop(0) else: destRouter = destRouterLink if destRouter in listAllRouters: topo['routers'][destRouter]['links'][curSwitch] = \ deepcopy(topo['switches'][curSwitch]['links'][destRouterLink]) # Assigning name to interfaces topo['routers'][destRouter]['links'][curSwitch]['interface'] = \ '{}-{}-eth{}'.format(destRouter, curSwitch, topo['routers'] \ [destRouter]['nextIfname']) topo['switches'][curSwitch]['links'][destRouter]['interface'] = \ '{}-{}-eth{}'.format(curSwitch, destRouter, topo['routers'] \ [destRouter]['nextIfname']) topo['routers'][destRouter]['nextIfname'] += 1 # Add links dictSwitches[curSwitch].add_link(tgen.gears[destRouter], \ topo['switches'][curSwitch]['links'][destRouter]['interface'], topo['routers'][destRouter]['links'][curSwitch]['interface'], ) # IPv4 if 'ipv4' in topo['routers'][destRouter]['links'][ curSwitch]: if topo['routers'][destRouter]['links'][curSwitch][ 'ipv4'] == 'auto': topo['routers'][destRouter]['links'][curSwitch]['ipv4'] = \ '{}/{}'.format(ipv4Next, topo['link_ip_start'][ \ 'v4mask']) ipv4Next += 1 # IPv6 if 'ipv6' in topo['routers'][destRouter]['links'][ curSwitch]: if topo['routers'][destRouter]['links'][curSwitch][ 'ipv6'] == 'auto': topo['routers'][destRouter]['links'][curSwitch]['ipv6'] = \ '{}/{}'.format(ipv6Next, topo['link_ip_start'][ \ 'v6mask']) ipv6Next = ipaddr.IPv6Address( int(ipv6Next) + ipv6Step) logger.debug( "Generated link data for router: %s\n%s", curRouter, json_dumps(topo["routers"][curRouter]["links"], indent=4, sort_keys=True), )
def build_topo_from_json(tgen, topo): """ Reads configuration from JSON file. Adds routers, creates interface names dynamically and link routers as defined in JSON to create topology. Assigns IPs dynamically to all interfaces of each router. * `tgen`: Topogen object * `topo`: json file data """ ROUTER_LIST = sorted(topo['routers'].keys(), key=lambda x: int(re_search('\d+', x).group(0))) listRouters = ROUTER_LIST[:] for routerN in ROUTER_LIST: logger.info('Topo: Add router {}'.format(routerN)) tgen.add_router(routerN) listRouters.append(routerN) if 'ipv4base' in topo: ipv4Next = ipaddr.IPv4Address(topo['link_ip_start']['ipv4']) ipv4Step = 2**(32 - topo['link_ip_start']['v4mask']) if topo['link_ip_start']['v4mask'] < 32: ipv4Next += 1 if 'ipv6base' in topo: ipv6Next = ipaddr.IPv6Address(topo['link_ip_start']['ipv6']) ipv6Step = 2**(128 - topo['link_ip_start']['v6mask']) if topo['link_ip_start']['v6mask'] < 127: ipv6Next += 1 for router in listRouters: topo['routers'][router]['nextIfname'] = 0 while listRouters != []: curRouter = listRouters.pop(0) # Physical Interfaces if 'links' in topo['routers'][curRouter]: def link_sort(x): if x == 'lo': return 0 elif 'link' in x: return int(x.split('-link')[1]) else: return int(re_search('\d+', x).group(0)) for destRouterLink, data in sorted(topo['routers'][curRouter]['links']. \ iteritems(), key=lambda x: link_sort(x[0])): currRouter_lo_json = \ topo['routers'][curRouter]['links'][destRouterLink] # Loopback interfaces if 'type' in data and data['type'] == 'loopback': if 'ipv4' in currRouter_lo_json and \ currRouter_lo_json['ipv4'] == 'auto': currRouter_lo_json['ipv4'] = '{}{}.{}/{}'. \ format(topo['lo_prefix']['ipv4'], number_to_row(curRouter), \ number_to_column(curRouter), topo['lo_prefix']['v4mask']) if 'ipv6' in currRouter_lo_json and \ currRouter_lo_json['ipv6'] == 'auto': currRouter_lo_json['ipv6'] = '{}{}:{}/{}'. \ format(topo['lo_prefix']['ipv6'], number_to_row(curRouter), \ number_to_column(curRouter), topo['lo_prefix']['v6mask']) if "-" in destRouterLink: # Spliting and storing destRouterLink data in tempList tempList = destRouterLink.split("-") # destRouter destRouter = tempList.pop(0) # Current Router Link tempList.insert(0, curRouter) curRouterLink = "-".join(tempList) else: destRouter = destRouterLink curRouterLink = curRouter if destRouter in listRouters: currRouter_link_json = \ topo['routers'][curRouter]['links'][destRouterLink] destRouter_link_json = \ topo['routers'][destRouter]['links'][curRouterLink] # Assigning name to interfaces currRouter_link_json['interface'] = \ '{}-{}-eth{}'.format(curRouter, destRouter, topo['routers'] \ [curRouter]['nextIfname']) destRouter_link_json['interface'] = \ '{}-{}-eth{}'.format(destRouter, curRouter, topo['routers'] \ [destRouter]['nextIfname']) topo['routers'][curRouter]['nextIfname'] += 1 topo['routers'][destRouter]['nextIfname'] += 1 # Linking routers to each other as defined in JSON file tgen.gears[curRouter].add_link(tgen.gears[destRouter], topo['routers'][curRouter]['links'][destRouterLink] \ ['interface'], topo['routers'][destRouter]['links'] \ [curRouterLink]['interface']) # IPv4 if 'ipv4' in currRouter_link_json: if currRouter_link_json['ipv4'] == 'auto': currRouter_link_json['ipv4'] = \ '{}/{}'.format(ipv4Next, topo['link_ip_start'][ \ 'v4mask']) destRouter_link_json['ipv4'] = \ '{}/{}'.format(ipv4Next + 1, topo['link_ip_start'][ \ 'v4mask']) ipv4Next += ipv4Step # IPv6 if 'ipv6' in currRouter_link_json: if currRouter_link_json['ipv6'] == 'auto': currRouter_link_json['ipv6'] = \ '{}/{}'.format(ipv6Next, topo['link_ip_start'][ \ 'v6mask']) destRouter_link_json['ipv6'] = \ '{}/{}'.format(ipv6Next + 1, topo['link_ip_start'][ \ 'v6mask']) ipv6Next = ipaddr.IPv6Address( int(ipv6Next) + ipv6Step) logger.debug( "Generated link data for router: %s\n%s", curRouter, json_dumps(topo["routers"][curRouter]["links"], indent=4, sort_keys=True))