sfa-0.9-17 tag
[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 filter_argname(self,argname):
45         if (not self.interface_options.lite or (argname!="cred")):
46             if (argname.find('(') != -1):
47                 # The name has documentation in it :-/
48                 brackright = argname.split('(')[1]
49                 if (brackright.find(')') == -1):
50                         raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
51                 inbrack = brackright.split(')')[0]
52                 argname = inbrack
53         return argname
54
55     def fold_complex_type_names(self,acc, arg):
56         name = arg.doc
57         if (type(acc)==list):
58             acc.append(name)
59         else:
60             p_i_b = acc.doc
61             acc = [p_i_b,name]
62         return acc
63
64     def fold_complex_type(self,acc, arg):
65         name = self.name_complex_type(arg)
66         self.complex_types[arg]=name
67         if (type(acc)==list):
68             acc.append(name)
69         else:
70             p_i_b = self.name_complex_type(acc)
71             acc = [p_i_b,name]
72         return acc
73
74     def name_complex_type(self,arg):
75
76         types_section = self.types.getElementsByTagName("xsd:schema")[0]
77
78         #pdb.set_trace()
79         if (isinstance(arg, Mixed)):
80             inner_types = reduce(self.fold_complex_type, arg)
81             inner_names = reduce(self.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             self.num_types=self.num_types+1
89             type_name = "Type%d"%self.num_types
90             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
91             complex_type.setAttribute("name", type_name)
92
93             choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
94             for n,t in zip(inner_names,inner_types):
95                 element = choice.appendChild(self.types.createElement("element"))
96                 n = self.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 (self.name_simple_type(arg.type))
103         elif type(arg) == ListType or type(arg) == TupleType:
104             inner_type = self.name_complex_type(arg[0]) 
105             self.num_types=self.num_types+1
106             type_name = "Type%d"%self.num_types
107             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
108             type_name = self.filter_argname(type_name)
109             complex_type.setAttribute("name", type_name)
110             complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
111             restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
112             restriction.setAttribute("base","soapenc:Array")
113             attribute = restriction.appendChild(self.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             self.num_types=self.num_types+1
121             type_name = "Type%d"%self.num_types
122             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
123             type_name = self.filter_argname(type_name)
124             complex_type.setAttribute("name", type_name)
125             complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
126      
127             for k in arg.fields:
128                 inner_type = self.name_complex_type(arg.fields[k]) 
129                 element=complex_content.appendChild(self.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 (self.name_simple_type(arg))
136
137     def name_simple_type(self,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 (self.name_simple_type(arg_type.type))
141         if arg_type == None:
142             return "none"
143         if arg_type == DictType:
144             return "xsd:anyType"
145         if arg_type in (ListType, TupleType):
146             return "xsd:arrayType"
147         elif arg_type == IntType or arg_type == LongType:
148             return "xsd:int"
149         elif arg_type == bool:
150             return "xsd:boolean"
151         elif arg_type == FloatType:
152             return "xsd:double"
153         elif arg_type in StringTypes:
154             return "xsd:string"
155         else:
156            pdb.set_trace()
157            raise SoapError, "Cannot handle %s objects" % arg_type
158
159     def param_type(self, arg):
160         return (self.name_complex_type(arg))
161
162     def add_wsdl_ports_and_bindings (self):
163         for method in apistub.methods:
164
165             # Skip system. methods
166             if "system." in method:
167                 continue
168
169             function = apistub.callable(method) # Commented documentation
170             #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
171             #print "\n".join(lines)
172             #print
173
174             
175             in_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
176             in_el.setAttribute("name", method + "_in")
177
178             for service_name in function.interfaces:
179                 if (self.services.has_key(service_name)):
180                     if (not method in self.services[service_name]):
181                         self.services[service_name].append(method)
182                 else:
183                     self.services[service_name]=[method]
184
185             # Arguments
186
187             if (function.accepts):
188                 (min_args, max_args, defaults) = function.args()
189                 for (argname,argtype) in zip(max_args,function.accepts):
190                     argname = self.filter_argname(argname)
191                     arg_part = in_el.appendChild(self.wsdl.createElement("part"))
192                     arg_part.setAttribute("name", argname)
193                     arg_part.setAttribute("type", self.param_type(argtype))
194                     
195             # Return type            
196             return_type = function.returns
197             out_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
198             out_el.setAttribute("name", method + "_out")
199             ret_part = out_el.appendChild(self.wsdl.createElement("part"))
200             ret_part.setAttribute("name", "Result")
201             ret_part.setAttribute("type", self.param_type(return_type))
202
203             # Port connecting arguments with return type
204
205             port_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("portType"))
206             port_el.setAttribute("name", method + "_port")
207             
208             op_el = port_el.appendChild(self.wsdl.createElement("operation"))
209             op_el.setAttribute("name", method)
210             inp_el=self.wsdl.createElement("input")
211             inp_el.setAttribute("message","tns:" + method + "_in")
212             inp_el.setAttribute("name",method+"_request")
213             op_el.appendChild(inp_el)
214             out_el = self.wsdl.createElement("output")
215             out_el.setAttribute("message","tns:" + method + "_out")
216             out_el.setAttribute("name",method+"_response")
217             op_el.appendChild(out_el)
218
219             # Bindings
220
221             bind_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("binding"))
222             bind_el.setAttribute("name", method + "_binding")
223             bind_el.setAttribute("type", "tns:" + method + "_port")
224             
225             soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
226             soap_bind.setAttribute("style", "rpc")
227             soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
228
229             
230             wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
231             wsdl_op.setAttribute("name", method)
232             wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
233                     "urn:" + method)
234
235             
236             wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
237             input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
238             input_soap_body.setAttribute("use", "encoded")
239             input_soap_body.setAttribute("namespace", "urn:" + method)
240             input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
241
242             
243             wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
244             output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
245             output_soap_body.setAttribute("use", "encoded")
246             output_soap_body.setAttribute("namespace", "urn:" + method)
247             output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
248             
249
250     def add_wsdl_services(self):
251         for service in self.services.keys():
252             if (getattr(self.interface_options,service)):
253                 service_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("service"))
254                 service_el.setAttribute("name", service)
255
256                 for method in self.services[service]:
257                         name=method
258                         servport_el = service_el.appendChild(self.wsdl.createElement("port"))
259                         servport_el.setAttribute("name", name + "_port")
260                         servport_el.setAttribute("binding", "tns:" + name + "_binding")
261
262                         soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
263                         soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
264
265
266     def compute_wsdl_definitions(self):
267         wsdl_text_header = """
268             <wsdl:definitions
269             name="sfa_autogenerated"
270             targetNamespace="%s/2009/07/sfa.wsdl"
271             xmlns="http://schemas.xmlsoap.org/wsdl/"
272             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
273             xmlns:xsdl="%s/2009/07/schema"
274             xmlns:tns="%s/2009/07/sfa.wsdl"
275             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
276             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
277             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
278             """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
279             
280         self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
281         
282
283     def compute_wsdl_definitions_and_types(self):
284         wsdl_text_header = """
285         <wsdl:definitions
286             name="sfa_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         self.types = xml.dom.minidom.parseString(wsdl_text_header)
300         
301
302     def add_wsdl_types(self):
303         wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
304         self.wsdl.firstChild.appendChild(wsdl_types)
305
306     def generate_wsdl(self):
307         self.compute_wsdl_definitions_and_types()
308         self.compute_wsdl_definitions()
309         self.add_wsdl_ports_and_bindings()
310         self.add_wsdl_types()
311         self.add_wsdl_services()
312
313     def pretty_print(self):
314         if (self.wsdl):
315             xml.dom.ext.PrettyPrint(self.wsdl)
316         else:
317             raise Exception("Empty WSDL")
318
319 def main():
320     parser = OptionParser()
321     parser.add_option("-r", "--registry", dest="registry", action="store_true", 
322                               help="Generate registry.wsdl", metavar="FILE")
323     parser.add_option("-s", "--slice-manager",
324                               action="store_true", dest="slicemgr", 
325                               help="Generate sm.wsdl")
326     parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
327                               help="Generate am.wsdl")
328     parser.add_option("-c", "--component", action="store_true", dest="component",
329                               help="Generate cm.wsdl")
330     parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
331                       help="Generate gm.wsdl")
332     parser.add_option("-l", "--lite", action="store_true", dest="lite",
333                               help="Generate LITE version of the interface, in which calls exclude credentials")
334     (interface_options, args) = parser.parse_args()
335
336     gen = WSDLGen(interface_options)
337     gen.generate_wsdl()
338     gen.pretty_print()
339     
340
341 if __name__ == "__main__":
342         main()