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