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