move auth and hierarchy into sfa/trust
[sfa.git] / wsdl / gw2wsdl.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 globals
14 import apistub
15 from types import *
16
17 from sfa.trust.auth import Auth
18 from sfa.util.parameter import Parameter,Mixed
19
20 complex_types = {}
21 services = {}
22
23 num_types = 0
24
25 class SoapError(Exception):
26      def __init__(self, value):
27          self.value = value
28      def __str__(self):
29          return repr(self.value)
30 try:
31     set
32 except NameError:
33     from sets import Set
34     set = Set
35
36 def fold_complex_type_names(acc, arg):
37     name = arg.doc
38     if (type(acc)==list):
39         acc.append(name)
40     else:
41         python_is_braindead = acc.doc
42         acc = [python_is_braindead,name]
43     return acc
44
45 def fold_complex_type(acc, arg):
46     global complex_types
47     name = name_complex_type(arg)
48     complex_types[arg]=name
49     if (type(acc)==list):
50         acc.append(name)
51     else:
52         python_is_braindead = name_complex_type(acc)
53         acc = [python_is_braindead,name]
54     return acc
55
56 def name_complex_type(arg):
57     global num_types
58     global types
59
60     types_section = types.getElementsByTagName("xsd:schema")[0]
61
62     if (isinstance(arg, Mixed)):
63         inner_types = reduce(fold_complex_type, arg)
64         inner_names = reduce(fold_complex_type_names, arg)
65         if (inner_types[-1]=="none"):
66             inner_types=inner_types[:-1]
67             min_args = 0
68         else:
69             min_args = 1
70     
71         num_types=num_types+1
72         type_name = "Type%d"%num_types
73         complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
74         complex_type.setAttribute("name", type_name)
75
76         choice = complex_type.appendChild(types.createElement("xsd:choice"))
77         for n,t in zip(inner_names,inner_types):
78             element = choice.appendChild(types.createElement("element"))
79             element.setAttribute("name", n)
80             element.setAttribute("type", "%s"%t)
81             element.setAttribute("minOccurs","%d"%min_args)
82         return "xsdl:%s"%type_name
83     elif (isinstance(arg, Parameter)):
84         return (name_simple_type(arg.type))
85     elif type(arg) == ListType or type(arg) == TupleType:
86         inner_type = name_complex_type(arg[0]) 
87         num_types=num_types+1
88         type_name = "Type%d"%num_types
89         complex_type = types_section.appendChild(types.createElement("xsd:simpleType"))
90         complex_type.setAttribute("name", type_name)
91         complex_content = complex_type.appendChild(types.createElement("xsd:list"))
92         complex_content.setAttribute("itemType",inner_type)
93         return "xsdl:%s"%type_name
94         
95     elif type(arg) == DictType or arg == DictType or issubclass(arg, dict):
96         num_types=num_types+1
97         type_name = "Type%d"%num_types
98         complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
99         complex_type.setAttribute("name", type_name)
100         complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
101  
102         for k in arg.fields:
103             inner_type = name_complex_type(arg.fields[k]) 
104             element=complex_content.appendChild(types.createElement("xsd:element"))
105             element.setAttribute("name",k)
106             element.setAttribute("type",inner_type)
107
108         return "xsdl:%s"%type_name 
109
110     else:
111         return (name_simple_type(arg))
112
113 def name_simple_type(arg_type):
114     if arg_type == None:
115         return "none"
116     if arg_type == DictType:
117         return "xsd:anyType"
118     elif arg_type == IntType or arg_type == LongType:
119         return "xsd:int"
120     elif arg_type == bool:
121         return "xsd:boolean"
122     elif arg_type == FloatType:
123         return "xsd:double"
124     elif arg_type in StringTypes:
125         return "xsd:string"
126     else:
127        pdb.set_trace()
128        raise SoapError, "Cannot handle %s objects" % arg_type
129
130 def param_type(arg):
131     return (name_complex_type(arg))
132
133 def add_wsdl_ports_and_bindings (wsdl):
134     for method in apistub.methods:
135         # Skip system. methods
136         if "system." in method:
137             continue
138
139         function = apistub.callable(method) # Commented documentation
140         #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
141         #print "\n".join(lines)
142         #print
143
144         
145         in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
146         in_el.setAttribute("name", method + "_in")
147
148         for service_name in function.interfaces:
149             if (services.has_key(service_name)):
150                 if (not method in services[service_name]):
151                     services[service_name].append(method)
152             else:
153                 services[service_name]=[method]
154
155         # Arguments
156
157         if (function.accepts):
158             (min_args, max_args, defaults) = function.args()
159             for (argname,argtype) in zip(max_args,function.accepts):
160                 arg_part = in_el.appendChild(wsdl.createElement("part"))
161                 arg_part.setAttribute("name", argname)
162                 arg_part.setAttribute("type", param_type(argtype))
163                 
164         # Return type            
165         return_type = function.returns
166         out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
167         out_el.setAttribute("name", method + "_out")
168         ret_part = out_el.appendChild(wsdl.createElement("part"))
169         ret_part.setAttribute("name", "returnvalue")
170         ret_part.setAttribute("type", param_type(return_type))
171
172         # Port connecting arguments with return type
173
174         port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
175         port_el.setAttribute("name", method + "_port")
176         
177         op_el = port_el.appendChild(wsdl.createElement("operation"))
178         op_el.setAttribute("name", method)
179         inp_el=wsdl.createElement("input")
180         inp_el.setAttribute("message","tns:" + method + "_in")
181         inp_el.setAttribute("name",method+"_request")
182         op_el.appendChild(inp_el)
183         out_el = wsdl.createElement("output")
184         out_el.setAttribute("message","tns:" + method + "_out")
185         out_el.setAttribute("name",method+"_response")
186         op_el.appendChild(out_el)
187
188         # Bindings
189
190         bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
191         bind_el.setAttribute("name", method + "_binding")
192         bind_el.setAttribute("type", "tns:" + method + "_port")
193         
194         soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
195         soap_bind.setAttribute("style", "rpc")
196         soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
197
198         
199         wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
200         wsdl_op.setAttribute("name", method)
201         wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
202                 "urn:" + method)
203
204         
205         wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
206         input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
207         input_soap_body.setAttribute("use", "encoded")
208         input_soap_body.setAttribute("namespace", "urn:" + method)
209         input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
210
211         
212         wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
213         output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
214         output_soap_body.setAttribute("use", "encoded")
215         output_soap_body.setAttribute("namespace", "urn:" + method)
216         output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
217         
218
219 def add_wsdl_service(wsdl):
220     for service in services.keys():
221         service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
222         service_el.setAttribute("name", service)
223
224         for method in services[service]:
225             name=method
226             servport_el = service_el.appendChild(wsdl.createElement("port"))
227             servport_el.setAttribute("name", name + "_port")
228             servport_el.setAttribute("binding", "tns:" + name + "_binding")
229
230             soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
231             soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
232
233
234 def get_wsdl_definitions():
235     wsdl_text_header = """
236         <wsdl:definitions
237         name="geniwrapper_autogenerated"
238         targetNamespace="%s/2009/07/sfa.wsdl"
239         xmlns="http://schemas.xmlsoap.org/wsdl/"
240         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
241         xmlns:xsdl="%s/2009/07/schema"
242         xmlns:tns="%s/2009/07/sfa.wsdl"
243         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
244         xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
245         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
246         """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
247         
248     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
249     
250     return wsdl
251
252 def get_wsdl_definitions_and_types():
253     wsdl_text_header = """
254     <wsdl:definitions
255         name="geniwrapper_autogenerated"
256         targetNamespace="%s/2009/07/sfa.wsdl"
257         xmlns="http://schemas.xmlsoap.org/wsdl/"
258         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
259         xmlns:xsdl="%s/2009/07/schema"
260         xmlns:tns="%s/2009/07/sfa.wsdl"
261         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
262         xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
263         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
264         <types>
265             <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
266         </types>
267     </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
268     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
269     return wsdl
270
271     
272 types = get_wsdl_definitions_and_types()
273
274 wsdl = get_wsdl_definitions()
275 add_wsdl_ports_and_bindings(wsdl)
276 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
277 wsdl.firstChild.appendChild(wsdl_types)
278 add_wsdl_service(wsdl)
279
280 xml.dom.ext.PrettyPrint(wsdl)