Changes between Version 18 and Version 19 of TopdlLibrary


Ignore:
Timestamp:
Oct 8, 2012 2:27:10 PM (12 years ago)
Author:
faber
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TopdlLibrary

    v18 v19  
    547547The next few examples will build on one another to show a more complex change to a topology.  We will take a topology, find the degrees of nodes, assign operating systems to them based on that degree, and output DETER-ready ns2.
    548548
    549 To try this we need a more complex topology.  The program below creates a topology with {{{nrouters}}} completely connected routers, each of which has {{{nleaves}}} leaves, except for one central router that has none.  Here is an image of the topology when {{{nrouters}}}=4 and {{{nleaves}}}=4:
     549To try this we need a more complex topology.  The program below creates a topology  with {{{nrouters}}} completely connected routers, each of which has {{{nleaves}}} leaves, except for one central router that has none.  Here is an image of the topology ([attachment:topo.xml in topdl]) when {{{nrouters}}}=4 and {{{nleaves}}}=4:
    550550
    551551[[Image(topo.png)]]
    552552
    553 We leave one router without leaves for aecthetics.
     553(One router is leafless because it makes the image more aesthetic).
    554554
    555555Here is the program that generates that topology:
     
    620620
    621621The code takes a few more shortcuts in construction,  e.g.,  most elements and substrates are appended directly to the lists rather than being assigned to variables, but is not fundamentally different from the start topology generator.
     622
     623== Annotating A Topology ==
     624
     625The first step in out transformation is to take a topology and add attributes that will be used by other applications.  In our example, the other applications will be further refinements in the same program.
     626
     627Here is a function that walks a topology, determines the degree of each computer, and adds an [TopdlLibrary#AttributeClass attribute] to each computer with that attribute in it.  It demostrates editing a topology using the [TopdlLibrary#TopdlClassFeatures set_attribute] function  common to topdl classes.
     628
     629{{{
     630def annotate_degree(top):
     631    '''
     632    Add an attribute (node_degree) to each Computer in the topology (top) that
     633    lists its degree
     634    '''
     635    for e in top.elements:
     636        # Skip elements that are not computers
     637        if not isinstance(e, topdl.Computer): continue
     638        # degree is the number of interfaces
     639        deg = len(e.interface)
     640        # Save a string-valued attribute
     641        e.set_attribute('node_degree', '%s' % deg)
     642    return top
     643}}}
     644
     645== Adding Other Classes ==
     646
     647This function takes a topology that has been through the {{{annotate_degree}}} function above and adds an [TopdlLibrary#OperatingSystemClass OperatingSystem nested object] to each Computer in the topology, based on the {{{node_degree}}} attribute.  This demonstrates adding a nested object directly, rather rthan through [TopdlLibrary#TopdlClassFeatures set_attribute].
     648
     649{{{
     650def add_operating_system(top):
     651    '''
     652    Add an OperatingSystem class to each computer in the topology (top).  If
     653    the node is a leaf (degree 1) make it Ubuntu Linux, otherwise make it
     654    FreeBSD.  annotate_degree() must have been called on the topology as this
     655    routine uses the node_degree attribute to make decisions.
     656    '''
     657
     658    for e in top.elements:
     659        # Skip non-Computers
     660        if not isinstance(e, topdl.Computer): continue
     661        a = e.get_attribute('node_degree')
     662        # if there is a node_degree attribute, assign an OS
     663        if a:
     664            # Covnert the string attribute into an integer
     665            try:
     666                deg = int(a)
     667            except ValueError, e:
     668                sys.exit('%s has a non-integer degree %s!?' % (e.name, a))
     669            # Assign the os - includes a distribution for Linux
     670            if deg == 1:
     671                e.os.append(topdl.OperatingSystem(name='Linux',
     672                    distribution='Ubuntu'))
     673            else:
     674                e.os.append(topdl.OperatingSystem(name='FreeBSD'))
     675    return top
     676
     677}}}