[eec716b] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
[6c57fe9] | 3 | import re |
---|
| 4 | |
---|
[eec716b] | 5 | class base: |
---|
| 6 | @staticmethod |
---|
| 7 | def init_class(c, arg): |
---|
| 8 | if isinstance(arg, dict): |
---|
[df783c1] | 9 | try: |
---|
| 10 | return c(**arg) |
---|
| 11 | except: |
---|
| 12 | print "%s" % arg |
---|
| 13 | raise |
---|
[eec716b] | 14 | elif isinstance(arg, c): |
---|
| 15 | return arg |
---|
| 16 | else: |
---|
| 17 | return None |
---|
| 18 | |
---|
| 19 | @staticmethod |
---|
| 20 | def make_list(a): |
---|
| 21 | if isinstance(a, basestring) or isinstance(a, dict): return [ a ] |
---|
| 22 | elif getattr(a, '__iter__', None): return a |
---|
| 23 | else: return [ a ] |
---|
| 24 | |
---|
[69692a9] | 25 | def remove_attribute(self, key): |
---|
| 26 | to_del = None |
---|
| 27 | attrs = getattr(self, 'attribute', []) |
---|
| 28 | for i, a in enumerate(attrs): |
---|
| 29 | if a.attribute == key: |
---|
| 30 | to_del = i |
---|
| 31 | break |
---|
| 32 | |
---|
| 33 | if to_del: del attrs[i] |
---|
| 34 | |
---|
[df783c1] | 35 | def get_attribute(self, key): |
---|
| 36 | rv = None |
---|
[db6b092] | 37 | attrs = getattr(self, 'attribute', None) |
---|
[df783c1] | 38 | if attrs: |
---|
| 39 | for a in attrs: |
---|
| 40 | if a.attribute == key: |
---|
| 41 | rv = a.value |
---|
| 42 | break |
---|
| 43 | return rv |
---|
| 44 | |
---|
[6c57fe9] | 45 | def set_attribute(self, key, value): |
---|
| 46 | attrs = getattr(self, 'attribute', None) |
---|
| 47 | if attrs is None: |
---|
| 48 | return |
---|
| 49 | for a in attrs: |
---|
| 50 | if a.attribute == key: |
---|
| 51 | a.value = value |
---|
| 52 | break |
---|
| 53 | else: |
---|
| 54 | attrs.append(Attribute(key, value)) |
---|
[eec716b] | 55 | |
---|
| 56 | class ConsistencyError(RuntimeError): pass |
---|
| 57 | |
---|
| 58 | class Attribute(base): |
---|
| 59 | def __init__(self, attribute, value): |
---|
| 60 | self.attribute = attribute |
---|
| 61 | self.value = value |
---|
| 62 | |
---|
[db6b092] | 63 | def clone(self): |
---|
| 64 | return Attribute(attribute=self.attribute, value=self.value) |
---|
| 65 | |
---|
[eec716b] | 66 | def to_dict(self): |
---|
| 67 | return { 'attribute': self.attribute, 'value': self.value } |
---|
| 68 | |
---|
| 69 | class Capacity(base): |
---|
| 70 | def __init__(self, rate, kind): |
---|
[cc8d8e9] | 71 | self.rate = float(rate) |
---|
[eec716b] | 72 | self.kind = kind |
---|
| 73 | |
---|
[db6b092] | 74 | def clone(self): |
---|
| 75 | return Capacity(rate=self.rate, kind=self.kind) |
---|
| 76 | |
---|
[eec716b] | 77 | def to_dict(self): |
---|
[cc8d8e9] | 78 | return { 'rate': float(self.rate), 'kind': self.kind } |
---|
[eec716b] | 79 | |
---|
| 80 | class Latency(base): |
---|
| 81 | def __init__(self, time, kind): |
---|
[cc8d8e9] | 82 | self.time = float(time) |
---|
[eec716b] | 83 | self.kind = kind |
---|
| 84 | |
---|
[db6b092] | 85 | def clone(self): |
---|
| 86 | return Latency(time=self.time, kind=self.kind) |
---|
| 87 | |
---|
[eec716b] | 88 | def to_dict(self): |
---|
[cc8d8e9] | 89 | return { 'time': float(self.time), 'kind': self.kind } |
---|
[eec716b] | 90 | |
---|
| 91 | class Substrate(base): |
---|
| 92 | def __init__(self, name, capacity=None, latency=None, attribute=[]): |
---|
| 93 | self.name = name |
---|
| 94 | self.capacity = self.init_class(Capacity, capacity) |
---|
| 95 | self.latency = self.init_class(Latency, latency) |
---|
| 96 | self.attribute = [ self.init_class(Attribute, a) \ |
---|
| 97 | for a in self.make_list(attribute) ] |
---|
| 98 | self.interfaces = [ ] |
---|
| 99 | |
---|
[db6b092] | 100 | def clone(self): |
---|
| 101 | if self.capacity: c = self.capacity.clone() |
---|
| 102 | else: c = None |
---|
| 103 | |
---|
| 104 | if self.latency: l = self.latency.clone() |
---|
| 105 | else: l = None |
---|
| 106 | |
---|
| 107 | return Substrate(name=self.name, |
---|
| 108 | capacity=c, |
---|
| 109 | latency=l, |
---|
| 110 | attribute = [a.clone() for a in self.attribute]) |
---|
| 111 | |
---|
[eec716b] | 112 | def to_dict(self): |
---|
| 113 | rv = { 'name': self.name } |
---|
| 114 | if self.capacity: |
---|
| 115 | rv['capacity'] = self.capacity.to_dict() |
---|
| 116 | if self.latency: |
---|
| 117 | rv['latency'] = self.latency.to_dict() |
---|
| 118 | if self.attribute: |
---|
| 119 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 120 | return rv |
---|
| 121 | |
---|
| 122 | class CPU(base): |
---|
| 123 | def __init__(self, type, attribute=[]): |
---|
| 124 | self.type = type |
---|
| 125 | self.attribute = [ self.init_class(Attribute, a) for a in \ |
---|
| 126 | self.make_list(attribute) ] |
---|
| 127 | |
---|
[db6b092] | 128 | def clone(self): |
---|
| 129 | return CPU(type=self.type, |
---|
| 130 | attribute = [a.clone() for a in self.attribute]) |
---|
| 131 | |
---|
[eec716b] | 132 | def to_dict(self): |
---|
| 133 | rv = { 'type': self.type} |
---|
| 134 | if self.attribute: |
---|
| 135 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 136 | return rv |
---|
| 137 | |
---|
| 138 | class Storage(base): |
---|
| 139 | def __init__(self, amount, persistence, attribute=[]): |
---|
[cc8d8e9] | 140 | self.amount = float(amount) |
---|
[eec716b] | 141 | self.presistence = persistence |
---|
| 142 | self.attribute = [ self.init_class(Attribute, a) \ |
---|
| 143 | for a in self.make_list(attribute) ] |
---|
| 144 | |
---|
[db6b092] | 145 | def clone(self): |
---|
| 146 | return Storage(amount=self.amount, persistence=self.persistence, |
---|
| 147 | attribute = [a.clone() for a in self.attribute]) |
---|
| 148 | |
---|
[eec716b] | 149 | def to_dict(self): |
---|
| 150 | rv = { 'amount': float(self.amount), 'persistence': self.persistence } |
---|
| 151 | if self.attribute: |
---|
| 152 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 153 | return rv |
---|
| 154 | |
---|
| 155 | class OperatingSystem(base): |
---|
[df783c1] | 156 | def __init__(self, name=None, version=None, distribution=None, |
---|
[eec716b] | 157 | distributionversion=None, attribute=[]): |
---|
| 158 | self.name = name |
---|
| 159 | self.version = version |
---|
| 160 | self.distribution = distribution |
---|
| 161 | self.distributionversion = distributionversion |
---|
| 162 | self.attribute = [ self.init_class(Attribute, a) \ |
---|
| 163 | for a in self.make_list(attribute) ] |
---|
| 164 | |
---|
[db6b092] | 165 | def clone(self): |
---|
| 166 | return OperatingSystem(name=self.name, |
---|
| 167 | version=self.version, |
---|
| 168 | distribution=self.distribution, |
---|
| 169 | distributionversion=self.distributionversion, |
---|
| 170 | attribute = [ a.clone() for a in self.attribute]) |
---|
| 171 | |
---|
[eec716b] | 172 | def to_dict(self): |
---|
[df783c1] | 173 | rv = { } |
---|
| 174 | if self.name: rv['name'] = self.name |
---|
[eec716b] | 175 | if self.version: rv['version'] = self.version |
---|
| 176 | if self.distribution: rv['version'] = self.distribution |
---|
| 177 | if self.distributionversion: rv['version'] = self.distributionversion |
---|
| 178 | if self.attribute: |
---|
| 179 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 180 | return rv |
---|
| 181 | |
---|
| 182 | class Software(base): |
---|
| 183 | def __init__(self, location, install=None, attribute=[]): |
---|
| 184 | self.location = location |
---|
| 185 | self.install = install |
---|
| 186 | self.attribute = [ self.init_class(Attribute, a)\ |
---|
| 187 | for a in self.make_list(attribute) ] |
---|
| 188 | |
---|
[db6b092] | 189 | def clone(self): |
---|
| 190 | return Software(location=self.location, install=self.install, |
---|
| 191 | attribute=[a.clone() for a in self.attribute]) |
---|
| 192 | |
---|
[eec716b] | 193 | def to_dict(self): |
---|
| 194 | rv = { 'location': self.location } |
---|
| 195 | if self.install: rv['install'] = self.install |
---|
| 196 | if self.attribute: |
---|
| 197 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 198 | return rv |
---|
| 199 | |
---|
| 200 | class Interface(base): |
---|
| 201 | def __init__(self, substrate, capacity=None, latency=None, attribute=[], |
---|
| 202 | element=None): |
---|
[cdb62d9] | 203 | self.substrate = self.make_list(substrate) |
---|
[eec716b] | 204 | self.capacity = self.init_class(Capacity, capacity) |
---|
| 205 | self.latency = self.init_class(Latency, latency) |
---|
| 206 | self.attribute = [ self.init_class(Attribute, a) \ |
---|
| 207 | for a in self.make_list(attribute) ] |
---|
| 208 | self.element = element |
---|
| 209 | self.subs = [ ] |
---|
| 210 | |
---|
[db6b092] | 211 | def clone(self): |
---|
| 212 | if self.capacity: c = self.capacity.clone() |
---|
| 213 | else: c = None |
---|
| 214 | |
---|
| 215 | if self.latency: l = self.latency.clone() |
---|
| 216 | else: l = None |
---|
| 217 | |
---|
| 218 | return Interface(substrate=self.substrate, |
---|
| 219 | capacity=c, latency=l, |
---|
| 220 | attribute = [ a.clone() for a in self.attribute]) |
---|
| 221 | |
---|
[eec716b] | 222 | def to_dict(self): |
---|
| 223 | rv = { 'substrate': self.substrate } |
---|
| 224 | if self.capacity: |
---|
| 225 | rv['capacity'] = self.capacity.to_dict() |
---|
| 226 | if self.latency: |
---|
| 227 | rv['latency'] = self.latency.to_dict() |
---|
| 228 | if self.attribute: |
---|
| 229 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 230 | return rv |
---|
| 231 | |
---|
[6c57fe9] | 232 | class ID(base): |
---|
| 233 | def __init__(self, fedid=None, uuid=None, uri=None, localname=None, |
---|
| 234 | kerberosUsername=None): |
---|
| 235 | self.fedid=fedid |
---|
| 236 | self.uuid = uuid |
---|
| 237 | self.uri = uri |
---|
| 238 | self.localname = localname |
---|
| 239 | self.kerberosUsername = kerberosUsername |
---|
| 240 | |
---|
| 241 | def clone(self): |
---|
| 242 | return ID(self.fedid, self.uuid, self.uri, self.localname, |
---|
[ecca6eb] | 243 | self.kerberosUsername) |
---|
[6c57fe9] | 244 | |
---|
| 245 | def to_dict(self): |
---|
| 246 | rv = { } |
---|
| 247 | if self.fedid: rv['fedid'] = self.fedid |
---|
| 248 | if self.uuid: rv['uuid'] = self.uuid |
---|
| 249 | if self.uri: rv['uri'] = self.uri |
---|
| 250 | if self.localname: rv['localname'] = self.localname |
---|
| 251 | if self.kerberosUsername: rv['kerberosUsername'] = self.kerberosUsername |
---|
| 252 | return rv |
---|
| 253 | |
---|
[eec716b] | 254 | class Computer(base): |
---|
[cdb62d9] | 255 | def __init__(self, name=[], cpu=[], os=[], software=[], storage=[], |
---|
| 256 | interface=[], attribute=[]): |
---|
[eec716b] | 257 | def assign_element(i): |
---|
| 258 | i.element = self |
---|
| 259 | |
---|
[cdb62d9] | 260 | self.name = self.make_list(name) |
---|
[eec716b] | 261 | self.cpu = [ self.init_class(CPU, c) for c in self.make_list(cpu) ] |
---|
| 262 | self.os = [ self.init_class(OperatingSystem, c) \ |
---|
| 263 | for c in self.make_list(os) ] |
---|
| 264 | self.software = [ self.init_class(Software, c) \ |
---|
| 265 | for c in self.make_list(software) ] |
---|
| 266 | self.storage = [ self.init_class(Storage, c) \ |
---|
| 267 | for c in self.make_list(storage) ] |
---|
| 268 | self.interface = [ self.init_class(Interface, c) \ |
---|
| 269 | for c in self.make_list(interface) ] |
---|
| 270 | self.attribute = [ self.init_class(Attribute, a) \ |
---|
| 271 | for a in self.make_list(attribute) ] |
---|
| 272 | map(assign_element, self.interface) |
---|
| 273 | |
---|
[db6b092] | 274 | def clone(self): |
---|
| 275 | return Computer(name=self.name, |
---|
| 276 | cpu=[x.clone() for x in self.cpu], |
---|
| 277 | os=[x.clone() for x in self.os], |
---|
| 278 | software=[x.clone() for x in self.software], |
---|
| 279 | storage=[x.clone() for x in self.storage], |
---|
| 280 | interface=[x.clone() for x in self.interface], |
---|
| 281 | attribute=[x.clone() for x in self.attribute]) |
---|
| 282 | |
---|
[eec716b] | 283 | def to_dict(self): |
---|
| 284 | rv = { } |
---|
[db6b092] | 285 | if self.name: |
---|
| 286 | rv['name'] = self.name |
---|
[eec716b] | 287 | if self.cpu: |
---|
| 288 | rv['cpu'] = [ c.to_dict() for c in self.cpu ] |
---|
| 289 | if self.os: |
---|
| 290 | rv['os'] = [ o.to_dict() for o in self.os ] |
---|
| 291 | if self.software: |
---|
| 292 | rv['software'] = [ s.to_dict() for s in self.software ] |
---|
| 293 | if self.storage: |
---|
| 294 | rv['storage'] = [ s.to_dict for s in self.storage ] |
---|
| 295 | if self.interface: |
---|
| 296 | rv['interface'] = [ i.to_dict() for i in self.interface ] |
---|
| 297 | if self.attribute: |
---|
| 298 | rv['attribute'] = [ i.to_dict() for i in self.attribute ] |
---|
[cdb62d9] | 299 | return { 'computer': rv } |
---|
[eec716b] | 300 | |
---|
[6c57fe9] | 301 | |
---|
| 302 | class Testbed(base): |
---|
| 303 | def __init__(self, uri, type, interface=[], attribute=[]): |
---|
| 304 | self.uri = uri |
---|
| 305 | self.type = type |
---|
| 306 | self.interface = [ self.init_class(Interface, c) \ |
---|
| 307 | for c in self.make_list(interface) ] |
---|
| 308 | self.attribute = [ self.init_class(Attribute, c) \ |
---|
| 309 | for c in self.make_list(attribute) ] |
---|
| 310 | |
---|
| 311 | def clone(self): |
---|
| 312 | return Testbed(self.uri, self.type, |
---|
| 313 | interface=[i.clone() for i in self.interface], |
---|
| 314 | attribute=[a.cone() for a in self.attribute]) |
---|
| 315 | |
---|
| 316 | def to_dict(self): |
---|
| 317 | rv = { } |
---|
| 318 | if self.uri: rv['uri'] = self.uri |
---|
| 319 | if self.type: rv['type'] = self.type |
---|
| 320 | if self.interface: |
---|
| 321 | rv['interface'] = [ i.to_dict() for i in self.interface] |
---|
| 322 | if self.attribute: |
---|
| 323 | rv['attribute'] = [ a.to_dict() for a in self.attribute] |
---|
| 324 | return { 'testbed': rv } |
---|
| 325 | |
---|
| 326 | class Segment(base): |
---|
| 327 | def __init__(self, id, type, uri, interface=[], attribute=[]): |
---|
| 328 | self.id = self.init_class(ID, id) |
---|
| 329 | self.type = type |
---|
| 330 | self.uri = uri |
---|
| 331 | self.interface = [ self.init_class(Interface, c) \ |
---|
| 332 | for c in self.make_list(interface) ] |
---|
| 333 | self.attribute = [ self.init_class(Attribute, c) \ |
---|
| 334 | for c in self.make_list(attribute) ] |
---|
| 335 | |
---|
| 336 | def clone(self): |
---|
| 337 | return Segment(self.id.clone(), self.type, self.uri, |
---|
| 338 | interface=[i.clone() for i in self.interface], |
---|
[ecca6eb] | 339 | attribute=[a.clone() for a in self.attribute]) |
---|
[6c57fe9] | 340 | |
---|
| 341 | def to_dict(self): |
---|
| 342 | rv = { } |
---|
| 343 | if self.id: rv['id'] = self.id.to_dict() |
---|
| 344 | if self.type: rv['type'] = self.type |
---|
| 345 | if self.uri: rv['uri'] = self.uri |
---|
| 346 | if self.interface: |
---|
| 347 | rv['interface'] = [ i.to_dict() for i in self.interface ] |
---|
| 348 | if self.attribute: |
---|
| 349 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
| 350 | return { 'segment': rv } |
---|
| 351 | |
---|
| 352 | |
---|
[eec716b] | 353 | class Other(base): |
---|
| 354 | def __init__(self, interface=[], attribute=[]): |
---|
| 355 | self.interface = [ self.init_class(Interface, c) \ |
---|
| 356 | for c in self.make_list(interface) ] |
---|
| 357 | self.attribute = [ self.init_class(Attribute, c) \ |
---|
| 358 | for c in self.make_list(attribute) ] |
---|
| 359 | |
---|
[db6b092] | 360 | def clone(self): |
---|
| 361 | return Other(interface=[i.clone() for i in self.interface], |
---|
| 362 | attribute=[a.clone() for a in attribute]) |
---|
| 363 | |
---|
[eec716b] | 364 | def to_dict(self): |
---|
[6c57fe9] | 365 | rv = {} |
---|
[eec716b] | 366 | if self.interface: |
---|
| 367 | rv['interface'] = [ i.to_dict() for i in self.interface ] |
---|
| 368 | if self.attribute: |
---|
| 369 | rv['attribute'] = [ a.to_dict() for a in self.attribute ] |
---|
[6c57fe9] | 370 | return {'other': rv } |
---|
[eec716b] | 371 | |
---|
| 372 | |
---|
| 373 | class Topology(base): |
---|
| 374 | @staticmethod |
---|
| 375 | def init_element(e): |
---|
| 376 | """ |
---|
| 377 | e should be of the form { typename: args } where args is a dict full of |
---|
| 378 | the right parameters to initialize the element. e should have only one |
---|
| 379 | key, but we walk e's keys in an arbitrary order and instantiate the |
---|
| 380 | first key we know how to. |
---|
| 381 | """ |
---|
| 382 | classmap = { |
---|
| 383 | 'computer': Computer, |
---|
[6c57fe9] | 384 | 'testbed': Testbed, |
---|
| 385 | 'segment': Segment, |
---|
[eec716b] | 386 | 'other': Other, |
---|
| 387 | } |
---|
| 388 | |
---|
[db6b092] | 389 | if isinstance(e, dict): |
---|
| 390 | for k in e.keys(): |
---|
| 391 | cl = classmap.get(k, None) |
---|
| 392 | if cl: return cl(**e[k]) |
---|
| 393 | else: |
---|
| 394 | return e |
---|
[eec716b] | 395 | |
---|
[69692a9] | 396 | def __init__(self, substrates=[], elements=[], attribute=[]): |
---|
[eec716b] | 397 | self.substrates = [ self.init_class(Substrate, s) \ |
---|
| 398 | for s in self.make_list(substrates) ] |
---|
| 399 | self.elements = [ self.init_element(e) \ |
---|
| 400 | for e in self.make_list(elements) ] |
---|
[69692a9] | 401 | self.attribute = [ self.init_class(Attribute, c) \ |
---|
| 402 | for c in self.make_list(attribute) ] |
---|
[db6b092] | 403 | self.incorporate_elements() |
---|
| 404 | |
---|
| 405 | def incorporate_elements(self): |
---|
| 406 | |
---|
[eec716b] | 407 | # Could to this init in one gulp, but we want to look for duplicate |
---|
| 408 | # substrate names |
---|
| 409 | substrate_map = { } |
---|
| 410 | for s in self.substrates: |
---|
[db6b092] | 411 | s.interfaces = [ ] |
---|
[eec716b] | 412 | if not substrate_map.has_key(s.name): |
---|
| 413 | substrate_map[s.name] = s |
---|
| 414 | else: |
---|
| 415 | raise ConsistencyError("Duplicate substrate name %s" % s.name) |
---|
| 416 | |
---|
| 417 | for e in self.elements: |
---|
| 418 | for i in e.interface: |
---|
[db6b092] | 419 | i.element = e |
---|
| 420 | i.subs = [ ] |
---|
[eec716b] | 421 | for sn in i.substrate: |
---|
| 422 | # NB, interfaces have substrate names in their substrate |
---|
| 423 | # attribute. |
---|
| 424 | if substrate_map.has_key(sn): |
---|
| 425 | sub = substrate_map[sn] |
---|
| 426 | i.subs.append(sub) |
---|
| 427 | sub.interfaces.append(i) |
---|
| 428 | else: |
---|
| 429 | raise ConsistencyError("No such substrate for %s" % sn) |
---|
| 430 | |
---|
[db6b092] | 431 | def clone(self): |
---|
| 432 | return Topology(substrates=[s.clone() for s in self.substrates], |
---|
[69692a9] | 433 | elements=[e.clone() for e in self.elements], |
---|
| 434 | attribute=[a.clone() for a in self.attribute]) |
---|
[db6b092] | 435 | |
---|
| 436 | |
---|
| 437 | def make_indices(self): |
---|
| 438 | sub_index = dict([(s.name, s) for s in self.substrates]) |
---|
| 439 | elem_index = dict([(n, e) for e in self.elements for n in e.name]) |
---|
| 440 | |
---|
[eec716b] | 441 | def to_dict(self): |
---|
| 442 | rv = { } |
---|
| 443 | if self.substrates: |
---|
| 444 | rv['substrates'] = [ s.to_dict() for s in self.substrates ] |
---|
| 445 | if self.elements: |
---|
| 446 | rv['elements'] = [ s.to_dict() for s in self.elements ] |
---|
[69692a9] | 447 | if self.attribute: |
---|
| 448 | rv['attribute'] = [ s.to_dict() for s in self.attribute] |
---|
[eec716b] | 449 | return rv |
---|
| 450 | |
---|
[db6b092] | 451 | def topology_from_xml(string=None, file=None, filename=None, top="topology"): |
---|
[eec716b] | 452 | import xml.parsers.expat |
---|
| 453 | |
---|
| 454 | class parser: |
---|
| 455 | def __init__(self): |
---|
| 456 | self.stack = [ ] |
---|
| 457 | self.chars = "" |
---|
| 458 | self.key = "" |
---|
| 459 | self.have_chars = False |
---|
| 460 | self.current = { } |
---|
| 461 | self.in_cdata = False |
---|
| 462 | |
---|
| 463 | def start_element(self, name, attrs): |
---|
| 464 | self.chars = "" |
---|
| 465 | self.have_chars = False |
---|
| 466 | self.key = str(name) |
---|
| 467 | self.stack.append((self.current, self.key)) |
---|
| 468 | self.current = { } |
---|
| 469 | |
---|
| 470 | def end_element(self, name): |
---|
| 471 | if self.have_chars: |
---|
| 472 | self.chars = self.chars.strip() |
---|
| 473 | if len(self.chars) >0: |
---|
| 474 | addit = self.chars |
---|
| 475 | else: |
---|
| 476 | addit = self.current |
---|
| 477 | else: |
---|
| 478 | addit = self.current |
---|
| 479 | |
---|
| 480 | parent, key = self.stack.pop() |
---|
| 481 | if parent.has_key(key): |
---|
| 482 | if isinstance(parent[key], list): |
---|
| 483 | parent[key].append(addit) |
---|
| 484 | else: |
---|
| 485 | parent[key] = [parent[key], addit] |
---|
| 486 | else: |
---|
| 487 | parent[key] = addit |
---|
| 488 | self.current = parent |
---|
| 489 | self.key = key |
---|
| 490 | |
---|
| 491 | self.chars = "" |
---|
| 492 | self.have_chars = False |
---|
| 493 | |
---|
| 494 | def char_data(self, data): |
---|
| 495 | self.have_chars = True |
---|
| 496 | self.chars += data |
---|
| 497 | |
---|
| 498 | p = parser() |
---|
| 499 | xp = xml.parsers.expat.ParserCreate() |
---|
| 500 | |
---|
| 501 | xp.StartElementHandler = p.start_element |
---|
| 502 | xp.EndElementHandler = p.end_element |
---|
| 503 | xp.CharacterDataHandler = p.char_data |
---|
| 504 | |
---|
[db6b092] | 505 | num_set = len([ x for x in (string, filename, file)\ |
---|
| 506 | if x is not None ]) |
---|
| 507 | |
---|
| 508 | if num_set != 1: |
---|
| 509 | raise RuntimeError("Exactly one one of file, filename and string " + \ |
---|
| 510 | "must be set") |
---|
| 511 | elif filename: |
---|
| 512 | f = open(filename, "r") |
---|
[df783c1] | 513 | xp.ParseFile(f) |
---|
[cc8d8e9] | 514 | f.close() |
---|
[db6b092] | 515 | elif file: |
---|
| 516 | xp.ParseFile(file) |
---|
[df783c1] | 517 | elif string: |
---|
| 518 | xp.Parse(string, isfinal=True) |
---|
| 519 | else: |
---|
| 520 | return None |
---|
[eec716b] | 521 | |
---|
| 522 | return Topology(**p.current[top]) |
---|
| 523 | |
---|
| 524 | def topology_to_xml(t, top=None): |
---|
| 525 | """ |
---|
| 526 | Print the topology as XML. This is quick and dirty, but should work for |
---|
| 527 | many purposes. Convert the topology to a dict and print it recursively. |
---|
| 528 | """ |
---|
| 529 | from xml.sax.saxutils import escape |
---|
| 530 | |
---|
| 531 | def dict_to_xml(e, top=None): |
---|
| 532 | if top: rv = "<%s>" % top |
---|
| 533 | else: rv = "" |
---|
| 534 | |
---|
| 535 | for k in e.keys(): |
---|
[69692a9] | 536 | if isinstance(e[k], basestring): |
---|
[eec716b] | 537 | rv += "<%s>%s</%s>" % (k, escape(e[k]), k) |
---|
[69692a9] | 538 | elif isinstance(e[k], (int, float, long)): |
---|
| 539 | rv += "<%s>%d</%s>" % (k, e[k], k) |
---|
[eec716b] | 540 | elif isinstance(e[k], dict): |
---|
| 541 | rv += "<%s>%s</%s>" % (k, dict_to_xml(e[k]), k) |
---|
| 542 | elif getattr(e[k], '__iter__', None): |
---|
| 543 | for ee in e[k]: |
---|
| 544 | if isinstance(ee, dict): |
---|
| 545 | rv += "<%s>%s</%s>" % (k, dict_to_xml(ee), k) |
---|
| 546 | else: |
---|
| 547 | rv += "<%s>%s</%s>" % (k, escape(ee), k) |
---|
| 548 | else: |
---|
[69692a9] | 549 | try: |
---|
| 550 | rv += "<%s>%s</%s>" % (k, e[k], k) |
---|
| 551 | except Exception: |
---|
| 552 | raise ConsistencyError("What is this?? %s %s" % (k, e[k])) |
---|
[eec716b] | 553 | if top: rv += "</%s>" % top |
---|
| 554 | return rv |
---|
| 555 | |
---|
| 556 | return dict_to_xml(t.to_dict(), top) |
---|
| 557 | |
---|
| 558 | |
---|
[db6b092] | 559 | def topology_to_vtopo(t): |
---|
| 560 | nodes = [ ] |
---|
| 561 | lans = [ ] |
---|
[eec716b] | 562 | |
---|
[db6b092] | 563 | for eidx, e in enumerate(t.elements): |
---|
| 564 | if e.name: name = e.name[0] |
---|
| 565 | else: name = "unnamed_node%d" % eidx |
---|
| 566 | |
---|
| 567 | ips = [ ] |
---|
[cc8d8e9] | 568 | for idx, i in enumerate(e.interface): |
---|
[db6b092] | 569 | ip = i.get_attribute('ip4_address') |
---|
| 570 | ips.append(ip) |
---|
| 571 | port = "%s:%d" % (name, idx) |
---|
| 572 | for idx, s in enumerate(i.subs): |
---|
| 573 | bw = 100000 |
---|
| 574 | delay = 0.0 |
---|
| 575 | if s.capacity: |
---|
| 576 | bw = s.capacity.rate |
---|
| 577 | if i.capacity: |
---|
| 578 | bw = i.capacity.rate |
---|
| 579 | |
---|
| 580 | if s.latency: |
---|
| 581 | delay = s.latency.time |
---|
| 582 | if i.latency: |
---|
| 583 | bw = i.latency.time |
---|
| 584 | |
---|
| 585 | lans.append({ |
---|
| 586 | 'member': port, |
---|
| 587 | 'vname': s.name, |
---|
| 588 | 'ip': ip, |
---|
| 589 | 'vnode': name, |
---|
| 590 | 'delay': delay, |
---|
| 591 | 'bandwidth': bw, |
---|
| 592 | }) |
---|
| 593 | nodes.append({ |
---|
| 594 | 'ips': ":".join(ips), |
---|
| 595 | 'vname': name, |
---|
| 596 | }) |
---|
| 597 | |
---|
[cc8d8e9] | 598 | return { 'node': nodes, 'lan': lans } |
---|
| 599 | |
---|
[6c57fe9] | 600 | def to_tcl_name(n): |
---|
[ecca6eb] | 601 | t = re.sub('-(\d+)', '(\1)', n) |
---|
[6c57fe9] | 602 | return t |
---|
| 603 | |
---|
[69692a9] | 604 | def generate_portal_command_filter(cmd, add_filter=None): |
---|
[ecca6eb] | 605 | def rv(e): |
---|
| 606 | s ="" |
---|
| 607 | if isinstance(e, Computer): |
---|
| 608 | gw = e.get_attribute('portal') |
---|
[69692a9] | 609 | if add_filter and callable(add_filter): |
---|
| 610 | add = add_filter(e) |
---|
| 611 | else: |
---|
| 612 | add = True |
---|
| 613 | if gw and add: |
---|
[ecca6eb] | 614 | s = "%s $%s\n" % (cmd, to_tcl_name(e.name[0])) |
---|
| 615 | return s |
---|
| 616 | return rv |
---|
| 617 | |
---|
[6c57fe9] | 618 | def generate_portal_image_filter(image): |
---|
| 619 | def rv(e): |
---|
| 620 | s ="" |
---|
| 621 | if isinstance(e, Computer): |
---|
| 622 | gw = e.get_attribute('portal') |
---|
| 623 | if gw: |
---|
[ecca6eb] | 624 | s = "tb-set-node-os $%s %s\n" % (to_tcl_name(e.name[0]), image) |
---|
[6c57fe9] | 625 | return s |
---|
| 626 | return rv |
---|
| 627 | |
---|
[ecca6eb] | 628 | def generate_portal_hardware_filter(type): |
---|
[6c57fe9] | 629 | def rv(e): |
---|
| 630 | s ="" |
---|
| 631 | if isinstance(e, Computer): |
---|
| 632 | gw = e.get_attribute('portal') |
---|
| 633 | if gw: |
---|
[ecca6eb] | 634 | s = "tb-set-hardware $%s %s\n" % (to_tcl_name(e.name[0]), type) |
---|
[6c57fe9] | 635 | return s |
---|
| 636 | return rv |
---|
| 637 | |
---|
| 638 | |
---|
[1da6a23] | 639 | def topology_to_ns2(t, filters=[], routing="Session"): |
---|
[cc8d8e9] | 640 | out = """ |
---|
| 641 | set ns [new Simulator] |
---|
| 642 | source tb_compat.tcl |
---|
| 643 | |
---|
| 644 | """ |
---|
[6c57fe9] | 645 | |
---|
[cc8d8e9] | 646 | for e in t.elements: |
---|
| 647 | rpms = "" |
---|
| 648 | tarfiles = "" |
---|
| 649 | if isinstance(e, Computer): |
---|
[6c57fe9] | 650 | name = to_tcl_name(e.name[0]) |
---|
[cc8d8e9] | 651 | out += "set %s [$ns node]\n" % name |
---|
| 652 | if e.os and len(e.os) == 1: |
---|
| 653 | osid = e.os[0].get_attribute('osid') |
---|
| 654 | if osid: |
---|
| 655 | out += "tb-set-node-os $%s %s\n" % (name, osid) |
---|
[ecca6eb] | 656 | hw = e.get_attribute('type') |
---|
| 657 | if hw: |
---|
| 658 | out += "tb-set-hardware $%s %s\n" % (name, hw) |
---|
[cc8d8e9] | 659 | for s in e.software: |
---|
| 660 | if s.install: |
---|
| 661 | tarfiles += "%s %s " % (s.install, s.location) |
---|
| 662 | else: |
---|
| 663 | rpms += "%s " % s.location |
---|
| 664 | if rpms: |
---|
| 665 | out += "tb-set-node-rpms $%s %s\n" % (name, rpms) |
---|
| 666 | if tarfiles: |
---|
| 667 | out += "tb-set-node-tarfiles $%s %s\n" % (name, tarfiles) |
---|
| 668 | startcmd = e.get_attribute('startup') |
---|
| 669 | if startcmd: |
---|
| 670 | out+= 'tb-set-node-startcmd $%s "%s"\n' % (name, startcmd) |
---|
[6c57fe9] | 671 | for f in filters: |
---|
| 672 | out += f(e) |
---|
[cc8d8e9] | 673 | out+= "\n" |
---|
| 674 | |
---|
| 675 | for idx, s in enumerate(t.substrates): |
---|
| 676 | loss = s.get_attribute('loss') |
---|
| 677 | if s.latency: |
---|
| 678 | delay = s.latency.time |
---|
| 679 | else: |
---|
| 680 | delay = 0 |
---|
[6c57fe9] | 681 | name = to_tcl_name(s.name or "sub%d" % idx) |
---|
[cc8d8e9] | 682 | |
---|
| 683 | if len(s.interfaces) > 2: |
---|
| 684 | # Lan |
---|
[6c57fe9] | 685 | members = [ to_tcl_name("$%s") % i.element.name[0] \ |
---|
| 686 | for i in s.interfaces] |
---|
| 687 | out += 'set %s [$ns make-lan "%s" %fkb %fms ]\n' % \ |
---|
[cc8d8e9] | 688 | (name, " ".join(members), s.capacity.rate, delay) |
---|
| 689 | if loss: |
---|
| 690 | "tb-set-lan-loss $%s %f\n" % (name, float(loss)) |
---|
| 691 | |
---|
| 692 | for i in s.interfaces: |
---|
| 693 | e = i.element |
---|
[1da6a23] | 694 | ip = i.get_attribute("ip4_address") |
---|
[cc8d8e9] | 695 | if ip: |
---|
[1da6a23] | 696 | out += "tb-set-ip-lan $%s $%s %s\n" % \ |
---|
[430e98d] | 697 | (to_tcl_name(e.name[0]), name, ip) |
---|
[cc8d8e9] | 698 | if i.capacity and i.capacity.rate != s.capacity.rate: |
---|
[6c57fe9] | 699 | out += "tb-set-node-lan-bandwidth $%s $%s %fkb\n" % \ |
---|
| 700 | (to_tcl_name(e.name[0]), name, i.capacity.rate) |
---|
[cc8d8e9] | 701 | if i.latency and i.latency.time != delay: |
---|
| 702 | out += "tb-set-node-lan-delay $%s $%s %fms\n" % \ |
---|
[6c57fe9] | 703 | (to_tcl_name(e.name[0]), name, i.latency.time) |
---|
[cc8d8e9] | 704 | iloss = i.get_attribute('loss') |
---|
| 705 | if loss and iloss != loss : |
---|
[1da6a23] | 706 | out += "tb-set-node-lan-loss $%s $%s %f\n" % \ |
---|
[6c57fe9] | 707 | (to_tcl_name(e.name[0]), name, float(loss)) |
---|
[cc8d8e9] | 708 | out+= "\n" |
---|
| 709 | elif len(s.interfaces) == 2: |
---|
| 710 | f = s.interfaces[0] |
---|
| 711 | t = s.interfaces[1] |
---|
| 712 | |
---|
[6c57fe9] | 713 | out += "set %s [$ns duplex-link $%s $%s %fkb %fms DropTail]\n" %\ |
---|
| 714 | (name, to_tcl_name(f.element.name[0]), |
---|
| 715 | to_tcl_name(t.element.name[0]), |
---|
[cc8d8e9] | 716 | s.capacity.rate, delay) |
---|
| 717 | if loss: |
---|
| 718 | out += "tb-set-link-loss $%s %f\n" % (name, float(loss)) |
---|
| 719 | |
---|
| 720 | for i in s.interfaces: |
---|
| 721 | lloss = i.get_attribute("loss") |
---|
| 722 | cap_override = i.capacity and \ |
---|
| 723 | i.capacity.rate != s.capacity.rate |
---|
| 724 | delay_override = i.latency and \ |
---|
| 725 | i.latency.time != delay |
---|
| 726 | loss_override = lloss and lloss != loss |
---|
| 727 | if cap_override or delay_override or loss_override: |
---|
| 728 | if i.capacity: cap = i.capacity.rate |
---|
| 729 | else: cap = s.capacity.rate |
---|
| 730 | |
---|
| 731 | if i.latency: delay = i.latency.time |
---|
| 732 | |
---|
| 733 | if lloss: loss = lloss |
---|
| 734 | else: loss = loss or 0.0 |
---|
| 735 | |
---|
[6c57fe9] | 736 | out += "tb-set-link-simplex-params $%s $%s %fms %fkb %f\n"\ |
---|
| 737 | % (name, to_tcl_name(i.element.name[0]), |
---|
| 738 | delay, cap, loss) |
---|
[1da6a23] | 739 | ip = i.get_attribute('ip4_address') |
---|
| 740 | if ip: |
---|
| 741 | out += "tb-set-ip-link $%s $%s %s\n" % \ |
---|
| 742 | (to_tcl_name(i.element.name[0]), name, ip) |
---|
[cc8d8e9] | 743 | out+= "\n" |
---|
[6c57fe9] | 744 | for f in filters: |
---|
| 745 | out+= f(s) |
---|
[1da6a23] | 746 | out+="$ns rtproto %s" % routing |
---|
[cc8d8e9] | 747 | out+=""" |
---|
| 748 | $ns run |
---|
| 749 | """ |
---|
| 750 | return out |
---|