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