3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for sfa
11 import xml.dom.minidom
16 from optparse import OptionParser
18 from sfa.storage.parameter import Parameter, Mixed
20 from sfa.util.py23 import StringType
22 plc_ns="http://www.planet-lab.org/sfa"
24 class SoapError(Exception):
25 def __init__(self, value):
28 return repr(self.value)
42 def __init__(self, interface_options):
43 self.interface_options = interface_options
45 def interface_name (self):
46 if (self.interface_options.aggregate and
47 self.interface_options.registry):
49 if self.interface_options.aggregate: return "aggregate"
50 elif self.interface_options.registry: return "registry"
51 else: return "unknown"
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]
64 # def fold_complex_type_names(self,acc, arg):
66 # if (type(acc)==list):
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):
79 # p_i_b = self.name_complex_type(acc)
83 def name_complex_type(self,arg):
85 types_section = self.types.getElementsByTagName("xsd:schema")[0]
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]
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)
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)
128 return "xsdl:%s"%type_name
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"))
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)
144 return "xsdl:%s"%type_name
146 return (self.name_simple_type(arg))
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))
154 if arg_type == DictType:
156 if arg_type in (ListType, TupleType):
157 return "xsd:arrayType"
158 elif arg_type == IntType or arg_type == LongType:
160 elif arg_type == bool:
162 elif arg_type == FloatType:
164 elif issubclass(arg_type, StringType):
168 raise SoapError("Cannot handle %s objects" % arg_type)
170 def param_type(self, arg):
171 return (self.name_complex_type(arg))
173 def add_wsdl_ports_and_bindings (self):
174 for method in apistub.methods:
176 # Skip system. methods
177 if "system." in method:
180 function = apistub.callable(method) # Commented documentation
181 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
182 #print "\n".join(lines)
186 in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
187 in_el.setAttribute("name", method + "_in")
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)
194 self.services[service_name]=[method]
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))
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))
214 # Port connecting arguments with return type
216 port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
217 port_el.setAttribute("name", method + "_port")
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)
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")
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")
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",
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/")
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/")
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)
267 for method in self.services[service]:
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")
273 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
274 soapaddress.setAttribute("location", "%s/%s" % (plc_ns,service))
277 def compute_wsdl_definitions(self):
278 wsdl_text_header = """
279 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
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)
292 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
295 def compute_wsdl_definitions_and_types(self):
296 wsdl_text_header = """
297 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
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/">
309 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
311 </wsdl:definitions> """ % (self.interface_name(),plc_ns, plc_ns, plc_ns, plc_ns)
312 self.types = xml.dom.minidom.parseString(wsdl_text_header)
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)
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()
326 def pretty_print(self):
328 print xml.dom.minidom.Document.toprettyxml(self.wsdl)
330 raise Exception("Empty WSDL")
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()
344 gen = WSDLGen(interface_options)
349 if __name__ == "__main__":