TopdlLibrary: topo.py

File topo.py, 1.8 KB (added by faber, 12 years ago)

Program to create more complex topology

Line 
1#!/usr/bin/env python
2
3import sys
4from deter import topdl
5
6# Parse the user-supplied routers and leaves arguments
7try:
8    if len(sys.argv) > 1: arg = sys.argv[1]
9    else: arg = 4
10    nrouters = int(arg)
11
12    if len(sys.argv) > 2: arg = sys.argv[2]
13    else: arg = 3
14    nleaves = int(arg)
15
16except ValueError, e:
17    print >>sys.stderr, 'Usage %s routers leaves' % sys.argv[0]
18    sys.exit('Cannot convert %s to an int' % arg)
19
20elems = []      # Elements in the topology
21subs = []       # Substrates in the topology
22
23# Make nrouters routers
24r = []
25for i in range(0,nrouters):
26    r.append(topdl.Computer(name='r-%d' % i))
27
28elems.extend(r)
29# Interconnect them
30n = 0
31for i in range(0,nrouters):
32    for j in range(i+1, nrouters):
33        # Connect r[i] to r[j]
34        # Pick a substrate name
35        link_name = 'rlink%i' % n
36        # Add interfaces to the two routers
37        r[i].interface.append(topdl.Interface(substrate=[link_name]))
38        r[j].interface.append(topdl.Interface(substrate=[link_name]))
39        # Put a substrate into the substrate list with the name we picked
40        subs.append(topdl.Substrate(name=link_name))
41        n += 1
42
43# Put nleaves leaves on each router, except the center one
44for i in range(0, (nrouters-1) * nleaves):
45    # Pick a link name
46    link_name = 'llink%i' % i
47
48    # add a computer with an interface on the new substrate to the element list
49    elems.append(topdl.Computer(name='leaf%d' % i, interface=[
50        topdl.Interface(substrate=[link_name])]))
51
52    # Add an interface to a router on the new substrate
53    r[i/nleaves].interface.append(topdl.Interface(substrate=[link_name]))
54
55    # Put a substrate into the substrate list with the name we picked
56    subs.append(topdl.Substrate(name=link_name))
57
58# Make the topology and print it
59top = topdl.Topology(elements=elems, substrates=subs)
60print topdl.topology_to_xml(top, top='experiment')