3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # This code is under preliminary development. I am going to clean it up
6 # once it is tested to work.
7 # Generates a WSDL for geniwrapper
13 import xml.dom.minidom
19 from geni.util.auth import Auth
20 from geni.util.parameter import Parameter,Mixed
27 class SoapError(Exception):
28 def __init__(self, value):
31 return repr(self.value)
38 def fold_complex_type_names(acc, arg):
43 python_is_braindead = acc.doc
44 acc = [python_is_braindead,name]
48 def fold_complex_type(acc, arg):
50 name = name_complex_type(arg)
51 complex_types[arg]=name
55 python_is_braindead = name_complex_type(acc)
56 acc = [python_is_braindead,name]
59 def name_complex_type(arg):
63 #if (complex_types.has_key(arg)):
64 # return complex_types[arg]
66 types_section = types.getElementsByTagName("xsd:schema")[0]
68 if (isinstance(arg, Mixed)):
69 inner_types = reduce(fold_complex_type, arg)
70 inner_names = reduce(fold_complex_type_names, arg)
71 if (inner_types[-1]=="none"):
72 inner_types=inner_types[:-1]
78 type_name = "Type%d"%num_types
79 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
80 complex_type.setAttribute("name", type_name)
82 choice = complex_type.appendChild(types.createElement("xsd:choice"))
83 for n,t in zip(inner_names,inner_types):
84 element = choice.appendChild(types.createElement("element"))
85 element.setAttribute("name", n)
86 element.setAttribute("type", "%s"%t)
87 element.setAttribute("minOccurs","%d"%min_args)
88 return "xsdl:%s"%type_name
89 elif (isinstance(arg, Parameter)):
90 return (name_simple_type(arg.type))
91 elif type(arg) == ListType or type(arg) == TupleType:
92 inner_type = name_complex_type(arg[0])
94 type_name = "Type%d"%num_types
95 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
96 complex_type.setAttribute("name", type_name)
97 complex_content = complex_type.appendChild(types.createElement("xsd:complexContent"))
98 restriction = complex_content.appendChild(types.createElement("restriction"))
99 restriction.setAttribute("base","soapenc:Array")
100 attribute = restriction.appendChild(types.createElement("attribute"))
101 attribute.setAttribute("ref","soapenc:arrayType")
102 attribute.setAttribute("wsdl:arrayType",inner_type+"[]")
103 return "xsdl:%s"%type_name
105 elif type(arg) == DictType or arg == DictType or issubclass(arg, dict):
106 num_types=num_types+1
107 type_name = "Type%d"%num_types
108 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
109 complex_type.setAttribute("name", type_name)
110 complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
113 inner_type = name_complex_type(arg.fields[k])
114 element=complex_content.appendChild(types.createElement("xsd:element"))
115 element.setAttribute("name",k)
116 element.setAttribute("type",inner_type)
118 return "xsdl:%s"%type_name
121 return (name_simple_type(arg))
123 def name_simple_type(arg_type):
126 if arg_type == DictType:
128 elif arg_type == IntType or arg_type == LongType:
130 elif arg_type == bool:
132 elif arg_type == FloatType:
134 elif arg_type in StringTypes:
138 raise SoapError, "Cannot handle %s objects" % arg_type
141 return (name_complex_type(arg))
143 def add_wsdl_ports_and_bindings (wsdl):
144 for method in apistub.methods:
145 # Skip system. methods
146 if "system." in method:
149 function = apistub.callable(method) # Commented documentation
150 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
151 #print "\n".join(lines)
155 in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
156 in_el.setAttribute("name", method + "_in")
158 for service_name in function.interfaces:
159 if (services.has_key(service_name)):
160 if (not method in services[service_name]):
161 services[service_name].append(method)
163 services[service_name]=[method]
167 if (function.accepts):
168 (min_args, max_args, defaults) = function.args()
169 for (argname,argtype) in zip(max_args,function.accepts):
170 arg_part = in_el.appendChild(wsdl.createElement("part"))
171 arg_part.setAttribute("name", argname)
172 arg_part.setAttribute("type", param_type(argtype))
175 return_type = function.returns
176 out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
177 out_el.setAttribute("name", method + "_out")
178 ret_part = out_el.appendChild(wsdl.createElement("part"))
179 ret_part.setAttribute("name", "returnvalue")
180 ret_part.setAttribute("type", param_type(return_type))
182 # Port connecting arguments with return type
184 port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
185 port_el.setAttribute("name", method + "_port")
187 op_el = port_el.appendChild(wsdl.createElement("operation"))
188 op_el.setAttribute("name", method)
189 inp_el=wsdl.createElement("input")
190 inp_el.setAttribute("message","tns:" + method + "_in")
191 inp_el.setAttribute("name",method+"_request")
192 op_el.appendChild(inp_el)
193 out_el = wsdl.createElement("output")
194 out_el.setAttribute("message","tns:" + method + "_out")
195 out_el.setAttribute("name",method+"_response")
196 op_el.appendChild(out_el)
200 bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
201 bind_el.setAttribute("name", method + "_binding")
202 bind_el.setAttribute("type", "tns:" + method + "_port")
204 soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
205 soap_bind.setAttribute("style", "rpc")
206 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
209 wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
210 wsdl_op.setAttribute("name", method)
211 wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
215 wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
216 input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
217 input_soap_body.setAttribute("use", "encoded")
218 input_soap_body.setAttribute("namespace", "urn:" + method)
219 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
222 wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
223 output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
224 output_soap_body.setAttribute("use", "encoded")
225 output_soap_body.setAttribute("namespace", "urn:" + method)
226 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
229 def add_wsdl_service(wsdl):
230 for service in services.keys():
231 service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
232 service_el.setAttribute("name", service)
234 for method in services[service]:
236 servport_el = service_el.appendChild(wsdl.createElement("port"))
237 servport_el.setAttribute("name", name + "_port")
238 servport_el.setAttribute("binding", "tns:" + name + "_binding")
240 soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
241 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
244 def get_wsdl_definitions():
245 wsdl_text_header = """
247 name="geniwrapper_autogenerated"
248 targetNamespace="%s/2009/06/sfa.wsdl"
249 xmlns="http://schemas.xmlsoap.org/wsdl/"
250 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
251 xmlns:xsdl="http://%s/2009/06/schema"
252 xmlns:tns="%s/2009/06/sfa.wsdl"
253 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
254 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
255 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
256 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
258 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
262 def get_wsdl_definitions_and_types():
263 wsdl_text_header = """
265 name="geniwrapper_autogenerated"
266 targetNamespace="%s/2009/06/sfa.wsdl"
267 xmlns="http://schemas.xmlsoap.org/wsdl/"
268 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
269 xmlns:xsdl="http://%s/2009/06/schema"
270 xmlns:tns="%s/2009/06/sfa.wsdl"
271 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
272 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
273 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
275 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/06/schema"/>
277 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
278 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
282 types = get_wsdl_definitions_and_types()
284 wsdl = get_wsdl_definitions()
285 add_wsdl_ports_and_bindings(wsdl)
286 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
287 wsdl.firstChild.appendChild(wsdl_types)
288 add_wsdl_service(wsdl)
290 xml.dom.ext.PrettyPrint(wsdl)