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