2to3 -f has_key
[sfa.git] / wsdl / sfa2wsdl.py
1 #!/usr/bin/python
2 #
3 # Sapan Bhatia <sapanb@cs.princeton.edu>
4 #
5 # Generates a WSDL for sfa
6
7
8 import os, sys
9 import time
10 import pdb
11 import xml.dom.minidom
12 import apistub
13 import inspect
14
15 from types import *
16 from optparse import OptionParser
17
18 from sfa.storage.parameter import Parameter, Mixed
19
20 from sfa.util.py23 import StringType
21
22 plc_ns="http://www.planet-lab.org/sfa"
23
24 class SoapError(Exception):
25      def __init__(self, value):
26          self.value = value
27      def __str__(self):
28          return repr(self.value)
29 try:
30     set
31 except NameError:
32     from sets import Set
33     set = Set
34
35 class WSDLGen:
36     complex_types = {}
37     services = {}
38     num_types = 0
39     wsdl = None
40     types = None
41
42     def __init__(self, interface_options):
43         self.interface_options = interface_options
44
45     def interface_name (self):
46          if self.interface_options.aggregate and \
47                   self.interface_options.slicemgr and \
48                   self.interface_options.registry:
49               return "complete"
50          if self.interface_options.aggregate: return "aggregate"
51          elif self.interface_options.slicemgr: return "slicemgr"
52          elif self.interface_options.registry: return "registry"
53          elif self.interface_options.component: return "component"
54          else: return "unknown"
55
56     def filter_argname(self,argname):
57         if (not self.interface_options.lite or (argname!="cred")):
58             if (argname.find('(') != -1):
59                 # The name has documentation in it :-/
60                 brackright = argname.split('(')[1]
61                 if (brackright.find(')') == -1):
62                         raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
63                 inbrack = brackright.split(')')[0]
64                 argname = inbrack
65         return argname
66
67 #    def fold_complex_type_names(self,acc, arg):
68 #        name = arg.doc
69 #        if (type(acc)==list):
70 #            acc.append(name)
71 #        else:
72 #            p_i_b = acc.doc
73 #            acc = [p_i_b,name]
74 #        return acc
75 #
76 #    def fold_complex_type(self,acc, arg):
77 #        name = self.name_complex_type(arg)
78 #        self.complex_types[arg]=name
79 #        if (type(acc)==list):
80 #            acc.append(name)
81 #        else:
82 #            p_i_b = self.name_complex_type(acc)
83 #            acc = [p_i_b,name]
84 #        return acc
85
86     def name_complex_type(self,arg):
87
88         types_section = self.types.getElementsByTagName("xsd:schema")[0]
89
90         #pdb.set_trace()
91         if (isinstance(arg, Mixed)):
92 #            inner_types = reduce(self.fold_complex_type, arg)
93 #            inner_names = reduce(self.fold_complex_type_names, arg)
94             inner_types = [ self.name_complex_type(x) for x in arg ]
95             inner_names = [ x.doc for x in arg ]
96             if (inner_types[-1]=="none"):
97                 inner_types=inner_types[:-1]
98                 min_args = 0
99             else:
100                 min_args = 1
101         
102             self.num_types += 1
103             type_name = "Type%d"%self.num_types
104             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
105             complex_type.setAttribute("name", type_name)
106
107             choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
108             for (n,t) in zip(inner_names,inner_types):
109                 element = choice.appendChild(self.types.createElement("element"))
110                 n = self.filter_argname(n)
111                 element.setAttribute("name", n)
112                 element.setAttribute("type", "%s"%t)
113                 element.setAttribute("minOccurs","%d"%min_args)
114             return "xsdl:%s"%type_name
115         elif (isinstance(arg, Parameter)):
116             return (self.name_simple_type(arg.type))
117         elif type(arg) in ( ListType , TupleType ):
118             inner_type = self.name_complex_type(arg[0]) 
119             self.num_types=self.num_types+1
120             type_name = "Type%d"%self.num_types
121             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
122             type_name = self.filter_argname(type_name)
123             complex_type.setAttribute("name", type_name)
124             complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
125             restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
126             restriction.setAttribute("base","soapenc:Array")
127             attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
128             attribute.setAttribute("ref","soapenc:arrayType")
129             attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
130
131             return "xsdl:%s"%type_name
132
133         elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
134             self.num_types=self.num_types+1
135             type_name = "Type%d"%self.num_types
136             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
137             type_name = self.filter_argname(type_name)
138             complex_type.setAttribute("name", type_name)
139             complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
140      
141             for k in arg.fields:
142                 inner_type = self.name_complex_type(arg.fields[k]) 
143                 element=complex_content.appendChild(self.types.createElement("xsd:element"))
144                 element.setAttribute("name",k)
145                 element.setAttribute("type",inner_type)
146
147             return "xsdl:%s"%type_name 
148         else:
149             return (self.name_simple_type(arg))
150
151     def name_simple_type(self,arg_type):
152         # A Parameter is reported as an instance, even though it is serialized as a type <>
153         if type(arg_type) == InstanceType:
154             return (self.name_simple_type(arg_type.type))
155         if arg_type == None:
156             return "none"
157         if arg_type == DictType:
158             return "xsd:anyType"
159         if arg_type in (ListType, TupleType):
160             return "xsd:arrayType"
161         elif arg_type == IntType or arg_type == LongType:
162             return "xsd:int"
163         elif arg_type == bool:
164             return "xsd:boolean"
165         elif arg_type == FloatType:
166             return "xsd:double"
167         elif issubclass(arg_type, StringType):
168             return "xsd:string"
169         else:
170            pdb.set_trace()
171            raise SoapError("Cannot handle %s objects" % arg_type)
172
173     def param_type(self, arg):
174         return (self.name_complex_type(arg))
175
176     def add_wsdl_ports_and_bindings (self):
177         for method in apistub.methods:
178
179             # Skip system. methods
180             if "system." in method:
181                 continue
182
183             function = apistub.callable(method) # Commented documentation
184             #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
185             #print "\n".join(lines)
186             #print
187
188             
189             in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
190             in_el.setAttribute("name", method + "_in")
191
192             for service_name in function.interfaces:
193                 if (service_name in self.services):
194                     if (not method in self.services[service_name]):
195                         self.services[service_name].append(method)
196                 else:
197                     self.services[service_name]=[method]
198
199             # Arguments
200
201             if (function.accepts):
202                 (min_args, max_args, defaults) = function.args()
203                 for (argname,argtype) in zip(max_args,function.accepts):
204                     argname = self.filter_argname(argname)
205                     arg_part = in_el.appendChild(self.wsdl.createElement("part"))
206                     arg_part.setAttribute("name", argname)
207                     arg_part.setAttribute("type", self.param_type(argtype))
208                     
209             # Return type            
210             return_type = function.returns
211             out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
212             out_el.setAttribute("name", method + "_out")
213             ret_part = out_el.appendChild(self.wsdl.createElement("part"))
214             ret_part.setAttribute("name", "Result")
215             ret_part.setAttribute("type", self.param_type(return_type))
216
217             # Port connecting arguments with return type
218
219             port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
220             port_el.setAttribute("name", method + "_port")
221             
222             op_el = port_el.appendChild(self.wsdl.createElement("operation"))
223             op_el.setAttribute("name", method)
224             inp_el=self.wsdl.createElement("input")
225             inp_el.setAttribute("message","tns:" + method + "_in")
226             inp_el.setAttribute("name",method+"_request")
227             op_el.appendChild(inp_el)
228             out_el = self.wsdl.createElement("output")
229             out_el.setAttribute("message","tns:" + method + "_out")
230             out_el.setAttribute("name",method+"_response")
231             op_el.appendChild(out_el)
232
233             # Bindings
234
235             bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
236             bind_el.setAttribute("name", method + "_binding")
237             bind_el.setAttribute("type", "tns:" + method + "_port")
238             
239             soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
240             soap_bind.setAttribute("style", "rpc")
241             soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
242
243             
244             wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
245             wsdl_op.setAttribute("name", method)
246             wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
247                     "urn:" + method)
248
249             
250             wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
251             input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
252             input_soap_body.setAttribute("use", "encoded")
253             input_soap_body.setAttribute("namespace", "urn:" + method)
254             input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
255
256             
257             wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
258             output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
259             output_soap_body.setAttribute("use", "encoded")
260             output_soap_body.setAttribute("namespace", "urn:" + method)
261             output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
262             
263
264     def add_wsdl_services(self):
265         for service in self.services.keys():
266             if (getattr(self.interface_options,service)):
267                 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
268                 service_el.setAttribute("name", service)
269
270                 for method in self.services[service]:
271                         name=method
272                         servport_el = service_el.appendChild(self.wsdl.createElement("port"))
273                         servport_el.setAttribute("name", name + "_port")
274                         servport_el.setAttribute("binding", "tns:" + name + "_binding")
275
276                         soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
277                         soapaddress.setAttribute("location", "%s/%s" % (plc_ns,service))
278
279
280     def compute_wsdl_definitions(self):
281         wsdl_text_header = """
282             <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
283             <wsdl:definitions
284             name="myplc-sfa-%s"
285             targetNamespace="%s/sfa.wsdl"
286             xmlns="http://schemas.xmlsoap.org/wsdl/"
287             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
288             xmlns:xsdl="%s/schema"
289             xmlns:tns="%s/sfa.wsdl"
290             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
291             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
292             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
293             """ % (self.interface_name(),plc_ns,plc_ns,plc_ns)
294             
295         self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
296         
297
298     def compute_wsdl_definitions_and_types(self):
299         wsdl_text_header = """
300         <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
301         <wsdl:definitions
302             name="myplc-sfa-%s"
303             targetNamespace="%s/sfa.wsdl"
304             xmlns="http://schemas.xmlsoap.org/wsdl/"
305             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
306             xmlns:xsdl="%s/schema"
307             xmlns:tns="%s/sfa.wsdl"
308             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
309             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
310             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
311             <types>
312                 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
313             </types>
314         </wsdl:definitions> """ % (self.interface_name(),plc_ns, plc_ns, plc_ns, plc_ns)
315         self.types = xml.dom.minidom.parseString(wsdl_text_header)
316         
317
318     def add_wsdl_types(self):
319         wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
320         self.wsdl.lastChild.appendChild(wsdl_types)
321
322     def generate_wsdl(self):
323         self.compute_wsdl_definitions_and_types()
324         self.compute_wsdl_definitions()
325         self.add_wsdl_ports_and_bindings()
326         self.add_wsdl_types()
327         self.add_wsdl_services()
328
329     def pretty_print(self):
330         if (self.wsdl):
331              print xml.dom.minidom.Document.toprettyxml(self.wsdl)
332         else:
333              raise Exception("Empty WSDL")
334
335 def main():
336     parser = OptionParser()
337     parser.add_option("-r", "--registry", dest="registry", action="store_true", 
338                               help="Generate registry.wsdl", metavar="FILE")
339     parser.add_option("-s", "--slice-manager",
340                               action="store_true", dest="slicemgr", 
341                               help="Generate sm.wsdl")
342     parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
343                               help="Generate am.wsdl")
344     parser.add_option("-c", "--component", action="store_true", dest="component",
345                               help="Generate cm.wsdl")
346     parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
347                       help="Generate gm.wsdl")
348     parser.add_option("-l", "--lite", action="store_true", dest="lite",
349                               help="Generate LITE version of the interface, in which calls exclude credentials")
350     (interface_options, args) = parser.parse_args()
351
352     gen = WSDLGen(interface_options)
353     gen.generate_wsdl()
354     gen.pretty_print()
355     
356
357 if __name__ == "__main__":
358         main()