Changeset db6b092 for fedd/federation/topdl.py
- Timestamp:
- Aug 5, 2009 11:12:21 AM (15 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-2.00, version-3.01, version-3.02
- Children:
- c9b3f49
- Parents:
- ab37086
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/federation/topdl.py
rab37086 rdb6b092 23 23 def get_attribute(self, key): 24 24 rv = None 25 attrs = getattr(self, 'attribute s', None)25 attrs = getattr(self, 'attribute', None) 26 26 if attrs: 27 27 for a in attrs: … … 40 40 self.value = value 41 41 42 def clone(self): 43 return Attribute(attribute=self.attribute, value=self.value) 44 42 45 def to_dict(self): 43 46 return { 'attribute': self.attribute, 'value': self.value } … … 48 51 self.kind = kind 49 52 53 def clone(self): 54 return Capacity(rate=self.rate, kind=self.kind) 55 50 56 def to_dict(self): 51 57 return { 'rate': self.rate, 'kind': self.kind } … … 55 61 self.time = time 56 62 self.kind = kind 63 64 def clone(self): 65 return Latency(time=self.time, kind=self.kind) 57 66 58 67 def to_dict(self): … … 68 77 self.interfaces = [ ] 69 78 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 70 91 def to_dict(self): 71 92 rv = { 'name': self.name } … … 83 104 self.attribute = [ self.init_class(Attribute, a) for a in \ 84 105 self.make_list(attribute) ] 106 107 def clone(self): 108 return CPU(type=self.type, 109 attribute = [a.clone() for a in self.attribute]) 85 110 86 111 def to_dict(self): … … 100 125 for a in self.make_list(attribute) ] 101 126 127 def clone(self): 128 return Storage(amount=self.amount, persistence=self.persistence, 129 attribute = [a.clone() for a in self.attribute]) 130 102 131 def to_dict(self): 103 132 rv = { 'amount': float(self.amount), 'persistence': self.persistence } … … 116 145 for a in self.make_list(attribute) ] 117 146 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 118 154 def to_dict(self): 119 155 rv = { } … … 132 168 self.attribute = [ self.init_class(Attribute, a)\ 133 169 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]) 134 174 135 175 def to_dict(self): … … 150 190 self.element = element 151 191 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]) 152 203 153 204 def to_dict(self): … … 181 232 map(assign_element, self.interface) 182 233 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 183 243 def to_dict(self): 184 244 rv = { } 245 if self.name: 246 rv['name'] = self.name 185 247 if self.cpu: 186 248 rv['cpu'] = [ c.to_dict() for c in self.cpu ] … … 203 265 self.attribute = [ self.init_class(Attribute, c) \ 204 266 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]) 205 271 206 272 def to_dict(self): … … 225 291 } 226 292 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 230 299 231 300 def __init__(self, substrates=[], elements=[]): … … 234 303 self.elements = [ self.init_element(e) \ 235 304 for e in self.make_list(elements) ] 305 self.incorporate_elements() 306 307 def incorporate_elements(self): 308 236 309 # Could to this init in one gulp, but we want to look for duplicate 237 310 # substrate names 238 311 substrate_map = { } 239 312 for s in self.substrates: 313 s.interfaces = [ ] 240 314 if not substrate_map.has_key(s.name): 241 315 substrate_map[s.name] = s … … 243 317 raise ConsistencyError("Duplicate substrate name %s" % s.name) 244 318 245 substrate_map = dict([ (s.name, s) for s in self.substrates ])246 319 for e in self.elements: 247 320 for i in e.interface: 321 i.element = e 322 i.subs = [ ] 248 323 for sn in i.substrate: 249 324 # NB, interfaces have substrate names in their substrate … … 256 331 raise ConsistencyError("No such substrate for %s" % sn) 257 332 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 258 342 def to_dict(self): 259 343 rv = { } … … 264 348 return rv 265 349 266 def topology_from_xml(string=None, file=None, top="topology"):350 def topology_from_xml(string=None, file=None, filename=None, top="topology"): 267 351 import xml.parsers.expat 268 352 … … 318 402 xp.CharacterDataHandler = p.char_data 319 403 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) 322 413 elif file: 323 f = open(file, "r") 324 xp.ParseFile(f) 414 xp.ParseFile(file) 325 415 elif string: 326 416 xp.Parse(string, isfinal=True) … … 360 450 361 451 362 452 def 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.