[22c88c9] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import re |
---|
| 5 | import os |
---|
| 6 | |
---|
| 7 | from optparse import OptionParser |
---|
| 8 | from federation.remote_service import service_caller |
---|
| 9 | from federation import topdl |
---|
| 10 | |
---|
| 11 | class constraint: |
---|
| 12 | def __init__(self, required=False, accepts=None, provides=None): |
---|
| 13 | self.required = required |
---|
| 14 | self.accepts = accepts or [] |
---|
| 15 | self.provides = provides or [] |
---|
| 16 | |
---|
| 17 | def __str__(self): |
---|
| 18 | return "%s:%s:%s" % (self.required, ",".join(self.provides), |
---|
| 19 | ",".join(self.accepts)) |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | def add_to_map(exp, mark): |
---|
| 23 | valid_marks = ('stub', 'transit') |
---|
| 24 | |
---|
| 25 | nn, r, p, a = exp.split(":") |
---|
| 26 | p = p.split(",") |
---|
| 27 | a = a.split(",") |
---|
| 28 | mark[nn] = constraint(r == 'required', p, a) |
---|
| 29 | |
---|
| 30 | def annotate_topo(top, mark): |
---|
| 31 | def make_new_name(names): |
---|
| 32 | i = 0 |
---|
| 33 | n = "mark%d" % i |
---|
| 34 | while n in names: |
---|
| 35 | i += 1 |
---|
| 36 | n = "mark%d" % i |
---|
| 37 | names.add(n) |
---|
| 38 | return n |
---|
| 39 | |
---|
| 40 | snames = set([ s.name for s in top.substrates ]) |
---|
| 41 | |
---|
| 42 | for e in top.elements: |
---|
| 43 | for n in e.name: |
---|
| 44 | if n in mark: |
---|
| 45 | if all([ not i.get_attribute('export') for i in e.interface]): |
---|
| 46 | con = mark[n] |
---|
| 47 | inames = set([x.name for x in e.interface]) |
---|
| 48 | sn = make_new_name(snames) |
---|
| 49 | ii = make_new_name(inames) |
---|
| 50 | s = topdl.Substrate(name=sn) |
---|
| 51 | i = topdl.Interface(substrate=sn, name=ii, element=e, |
---|
| 52 | attribute=[ |
---|
| 53 | topdl.Attribute(attribute='composition_point', |
---|
| 54 | value='True'), |
---|
| 55 | topdl.Attribute(attribute='required', |
---|
| 56 | value = "%s" % con.required), |
---|
| 57 | topdl.Attribute(attribute='provides', |
---|
| 58 | value=",".join(con.provides)), |
---|
| 59 | topdl.Attribute(attribute='accepts', |
---|
| 60 | value=",".join(con.accepts)), |
---|
| 61 | ] |
---|
| 62 | ) |
---|
| 63 | e.interface.append(i) |
---|
| 64 | top.substrates.append(s) |
---|
| 65 | |
---|
| 66 | top.incorporate_elements() |
---|
| 67 | |
---|
| 68 | def remote_ns2topdl(uri, desc, cert): |
---|
| 69 | |
---|
| 70 | req = { 'description' : { 'ns2description': desc }, } |
---|
| 71 | |
---|
| 72 | try: |
---|
| 73 | r = service_caller('Ns2Topdl')(uri, req, cert) |
---|
| 74 | except: |
---|
| 75 | return None |
---|
| 76 | |
---|
| 77 | if r.has_key('Ns2TopdlResponseBody'): |
---|
| 78 | r = r['Ns2TopdlResponseBody'] |
---|
| 79 | ed = r.get('experimentdescription', None) |
---|
| 80 | if 'topdldescription' in ed: |
---|
| 81 | return topdl.Topology(**ed['topdldescription']) |
---|
| 82 | else: |
---|
| 83 | return None |
---|
| 84 | else: |
---|
| 85 | return None |
---|
| 86 | |
---|
| 87 | const_re = re.compile("\s*#\s*COMPOSITION:\s*([^:]+:[^:]+:.*)") |
---|
| 88 | |
---|
| 89 | parser = OptionParser() |
---|
| 90 | parser.add_option('--url', dest='url', default="http://localhost:13235", |
---|
| 91 | help='url of ns2 to topdl service') |
---|
| 92 | parser.add_option('--certfile', dest='cert', default=None, |
---|
| 93 | help='Certificate to use as identity') |
---|
| 94 | |
---|
| 95 | opts, args = parser.parse_args() |
---|
| 96 | |
---|
| 97 | if opts.cert: |
---|
| 98 | cert = opts.cert |
---|
| 99 | elif os.access(os.path.expanduser("~/.ssl/emulab.pem"), os.R_OK): |
---|
| 100 | cert = os.path.expanduser("~/.ssl/emulab.pem") |
---|
| 101 | else: |
---|
| 102 | cert = None |
---|
| 103 | |
---|
| 104 | for fn in args: |
---|
| 105 | marks = { } |
---|
| 106 | contents = "" |
---|
| 107 | try: |
---|
| 108 | f = open(fn, "r") |
---|
| 109 | for l in f: |
---|
| 110 | contents += l |
---|
| 111 | m = const_re.search(l) |
---|
| 112 | if m: |
---|
| 113 | add_to_map(re.sub('\s', '', m.group(1)), marks) |
---|
| 114 | except EnvironmentError, e: |
---|
| 115 | print >>sys.stderr, "Error on %S: %s" % (fn, e) |
---|
| 116 | |
---|
| 117 | print marks |
---|
| 118 | |
---|
| 119 | top = remote_ns2topdl(opts.url, contents, cert) |
---|
| 120 | |
---|
| 121 | if top: |
---|
| 122 | annotate_topo(top, marks) |
---|
| 123 | print topdl.topology_to_xml(top) |
---|
| 124 | else: |
---|
| 125 | sys.exit("Topology conversion failed on %s" % fn) |
---|