source: fedd/federation/topdl.py @ d608a90

version-3.02
Last change on this file since d608a90 was d608a90, checked in by Ted Faber <faber@…>, 13 years ago

Import the better XML for topdl

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