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