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