minor fix to sfa2wsdl
[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 and \
46                   self.interface_options.slicemgr and \
47                   self.interface_options.registry:
48               return "complete"
49          if self.interface_options.aggregate: return "aggregate"
50          elif self.interface_options.slicemgr: return "slicemgr"
51          elif self.interface_options.registry: return "registry"
52          elif self.interface_options.component: return "component"
53          else: return "unknown"
54
55     def filter_argname(self,argname):
56         if (not self.interface_options.lite or (argname!="cred")):
57             if (argname.find('(') != -1):
58                 # The name has documentation in it :-/
59                 brackright = argname.split('(')[1]
60                 if (brackright.find(')') == -1):
61                         raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
62                 inbrack = brackright.split(')')[0]
63                 argname = inbrack
64         return argname
65
66     def fold_complex_type_names(self,acc, arg):
67         name = arg.doc
68         if (type(acc)==list):
69             acc.append(name)
70         else:
71             p_i_b = acc.doc
72             acc = [p_i_b,name]
73         return acc
74
75     def fold_complex_type(self,acc, arg):
76         name = self.name_complex_type(arg)
77         self.complex_types[arg]=name
78         if (type(acc)==list):
79             acc.append(name)
80         else:
81             p_i_b = self.name_complex_type(acc)
82             acc = [p_i_b,name]
83         return acc
84
85     def name_complex_type(self,arg):
86
87         types_section = self.types.getElementsByTagName("xsd:schema")[0]
88
89         #pdb.set_trace()
90         if (isinstance(arg, Mixed)):
91             inner_types = reduce(self.fold_complex_type, arg)
92             inner_names = reduce(self.fold_complex_type_names, 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=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) == ListType or type(arg) == 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 arg_type in StringTypes:
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 (self.services.has_key(service_name)):
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" % (globals.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(),globals.plc_ns,globals.plc_ns,globals.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(),globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.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             xml.dom.ext.PrettyPrint(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("-s", "--slice-manager",
337                               action="store_true", dest="slicemgr", 
338                               help="Generate sm.wsdl")
339     parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
340                               help="Generate am.wsdl")
341     parser.add_option("-c", "--component", action="store_true", dest="component",
342                               help="Generate cm.wsdl")
343     parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
344                       help="Generate gm.wsdl")
345     parser.add_option("-l", "--lite", action="store_true", dest="lite",
346                               help="Generate LITE version of the interface, in which calls exclude credentials")
347     (interface_options, args) = parser.parse_args()
348
349     gen = WSDLGen(interface_options)
350     gen.generate_wsdl()
351     gen.pretty_print()
352     
353
354 if __name__ == "__main__":
355         main()