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