Ignore:
Timestamp:
Aug 5, 2009 11:12:21 AM (15 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-2.00, version-3.01, version-3.02
Children:
c9b3f49
Parents:
ab37086
Message:

Checkpoint (and whitespace in experiment_control)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/federation/topdl.py

    rab37086 rdb6b092  
    2323    def get_attribute(self, key):
    2424        rv = None
    25         attrs = getattr(self, 'attributes', None)
     25        attrs = getattr(self, 'attribute', None)
    2626        if attrs:
    2727            for a in attrs:
     
    4040        self.value = value
    4141
     42    def clone(self):
     43        return Attribute(attribute=self.attribute, value=self.value)
     44
    4245    def to_dict(self):
    4346        return { 'attribute': self.attribute, 'value': self.value }
     
    4851        self.kind = kind
    4952
     53    def clone(self):
     54        return Capacity(rate=self.rate, kind=self.kind)
     55
    5056    def to_dict(self):
    5157        return { 'rate': self.rate, 'kind': self.kind }
     
    5561        self.time = time
    5662        self.kind = kind
     63
     64    def clone(self):
     65        return Latency(time=self.time, kind=self.kind)
    5766
    5867    def to_dict(self):
     
    6877        self.interfaces = [ ]
    6978
     79    def clone(self):
     80        if self.capacity: c = self.capacity.clone()
     81        else: c = None
     82
     83        if self.latency: l = self.latency.clone()
     84        else: l = None
     85
     86        return Substrate(name=self.name,
     87                capacity=c,
     88                latency=l,
     89                attribute = [a.clone() for a in self.attribute])
     90
    7091    def to_dict(self):
    7192        rv = { 'name': self.name }
     
    83104        self.attribute = [ self.init_class(Attribute, a) for a in \
    84105                self.make_list(attribute) ]
     106
     107    def clone(self):
     108        return CPU(type=self.type,
     109                attribute = [a.clone() for a in self.attribute])
    85110
    86111    def to_dict(self):
     
    100125                for a in self.make_list(attribute) ]
    101126
     127    def clone(self):
     128        return Storage(amount=self.amount, persistence=self.persistence,
     129                attribute = [a.clone() for a in self.attribute])
     130
    102131    def to_dict(self):
    103132        rv = { 'amount': float(self.amount), 'persistence': self.persistence }
     
    116145                for a in self.make_list(attribute) ]
    117146
     147    def clone(self):
     148        return OperatingSystem(name=self.name,
     149                version=self.version,
     150                distribution=self.distribution,
     151                distributionversion=self.distributionversion,
     152                attribute = [ a.clone() for a in self.attribute])
     153
    118154    def to_dict(self):
    119155        rv = { }
     
    132168        self.attribute = [ self.init_class(Attribute, a)\
    133169                for a in self.make_list(attribute) ]
     170
     171    def clone(self):
     172        return Software(location=self.location, install=self.install,
     173                attribute=[a.clone() for a in self.attribute])
    134174
    135175    def to_dict(self):
     
    150190        self.element = element
    151191        self.subs = [ ]
     192
     193    def clone(self):
     194        if self.capacity: c = self.capacity.clone()
     195        else: c = None
     196
     197        if self.latency: l = self.latency.clone()
     198        else: l = None
     199
     200        return Interface(substrate=self.substrate,
     201                capacity=c, latency=l,
     202                attribute = [ a.clone() for a in self.attribute])
    152203
    153204    def to_dict(self):
     
    181232        map(assign_element, self.interface)
    182233
     234    def clone(self):
     235        return Computer(name=self.name,
     236                cpu=[x.clone() for x in self.cpu],
     237                os=[x.clone() for x in self.os],
     238                software=[x.clone() for x in self.software],
     239                storage=[x.clone() for x in self.storage],
     240                interface=[x.clone() for x in self.interface],
     241                attribute=[x.clone() for x in self.attribute])
     242
    183243    def to_dict(self):
    184244        rv = { }
     245        if self.name:
     246            rv['name'] = self.name
    185247        if self.cpu:
    186248            rv['cpu'] = [ c.to_dict() for  c in self.cpu ]
     
    203265        self.attribute = [ self.init_class(Attribute, c) \
    204266                for c in self.make_list(attribute) ]
     267
     268    def clone(self):
     269        return Other(interface=[i.clone() for i in self.interface],
     270                attribute=[a.clone() for a in attribute])
    205271
    206272    def to_dict(self):
     
    225291            }
    226292
    227         for k in e.keys():
    228             cl = classmap.get(k, None)
    229             if cl: return cl(**e[k])
     293        if isinstance(e, dict):
     294            for k in e.keys():
     295                cl = classmap.get(k, None)
     296                if cl: return cl(**e[k])
     297        else:
     298            return e
    230299
    231300    def __init__(self, substrates=[], elements=[]):
     
    234303        self.elements = [ self.init_element(e) \
    235304                for e in self.make_list(elements) ]
     305        self.incorporate_elements()
     306
     307    def incorporate_elements(self):
     308
    236309        # Could to this init in one gulp, but we want to look for duplicate
    237310        # substrate names
    238311        substrate_map = { }
    239312        for s in self.substrates:
     313            s.interfaces = [ ]
    240314            if not substrate_map.has_key(s.name):
    241315                substrate_map[s.name] = s
     
    243317                raise ConsistencyError("Duplicate substrate name %s" % s.name)
    244318
    245         substrate_map = dict([ (s.name, s) for s in self.substrates ])
    246319        for e in self.elements:
    247320            for i in e.interface:
     321                i.element = e
     322                i.subs = [ ]
    248323                for sn in i.substrate:
    249324                    # NB, interfaces have substrate names in their substrate
     
    256331                        raise ConsistencyError("No such substrate for %s" % sn)
    257332
     333    def clone(self):
     334        return Topology(substrates=[s.clone() for s in self.substrates],
     335                elements=[e.clone() for e in self.elements])
     336
     337
     338    def make_indices(self):
     339        sub_index = dict([(s.name, s) for s in self.substrates])
     340        elem_index = dict([(n, e) for e in self.elements for n in e.name])
     341
    258342    def to_dict(self):
    259343        rv = { }
     
    264348        return rv
    265349
    266 def topology_from_xml(string=None, file=None, top="topology"):
     350def topology_from_xml(string=None, file=None, filename=None, top="topology"):
    267351    import xml.parsers.expat
    268352
     
    318402    xp.CharacterDataHandler = p.char_data
    319403
    320     if file and string:
    321         raise RuntimeError("Only one of file and string")
     404    num_set = len([ x for x in (string, filename, file)\
     405            if x is not None ])
     406
     407    if num_set != 1:
     408        raise RuntimeError("Exactly one one of file, filename and string " + \
     409                "must be set")
     410    elif filename:
     411        f = open(filename, "r")
     412        xp.ParseFile(f)
    322413    elif file:
    323         f = open(file, "r")
    324         xp.ParseFile(f)
     414        xp.ParseFile(file)
    325415    elif string:
    326416        xp.Parse(string, isfinal=True)
     
    360450
    361451
    362 
     452def topology_to_vtopo(t):
     453    nodes = [ ]
     454    lans = [ ]
     455
     456    for eidx, e in enumerate(t.elements):
     457        n = { }
     458        if e.name: name = e.name[0]
     459        else: name = "unnamed_node%d" % eidx
     460       
     461        ips = [ ]
     462        for i in e.interfaces:
     463            ip = i.get_attribute('ip4_address')
     464            ips.append(ip)
     465            port = "%s:%d" % (name, idx)
     466            for idx, s in enumerate(i.subs):
     467                bw = 100000
     468                delay = 0.0
     469                if s.capacity:
     470                    bw = s.capacity.rate
     471                if i.capacity:
     472                    bw = i.capacity.rate
     473
     474                if s.latency:
     475                    delay = s.latency.time
     476                if i.latency:
     477                    bw = i.latency.time
     478
     479                lans.append({
     480                    'member': port,
     481                    'vname': s.name,
     482                    'ip': ip,
     483                    'vnode': name,
     484                    'delay': delay,
     485                    'bandwidth': bw,
     486                    })
     487        nodes.append({
     488            'ips': ":".join(ips),
     489            'vname': name,
     490            })
     491
     492        nodes.append(n)
     493    return { 'vtopo': { 'node': node, 'lan': lan } }
Note: See TracChangeset for help on using the changeset viewer.