3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for geniwrapper
11 import xml.dom.minidom
17 from optparse import OptionParser
19 from sfa.trust.auth import Auth
20 from sfa.util.parameter import Parameter,Mixed
29 class SoapError(Exception):
30 def __init__(self, value):
33 return repr(self.value)
40 def filter_argname(argname):
41 global interface_options
42 if (not interface_options.lite or (argname!="cred")):
43 if (argname.find('(') != -1):
44 # The name has documentation in it :-/
45 brackright = argname.split('(')[1]
46 if (brackright.find(')') == -1):
47 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
48 inbrack = brackright.split(')')[0]
52 def fold_complex_type_names(acc, arg):
61 def fold_complex_type(acc, arg):
63 name = name_complex_type(arg)
64 complex_types[arg]=name
68 p_i_b = name_complex_type(acc)
72 def name_complex_type(arg):
76 types_section = types.getElementsByTagName("xsd:schema")[0]
79 if (isinstance(arg, Mixed)):
80 inner_types = reduce(fold_complex_type, arg)
81 inner_names = reduce(fold_complex_type_names, arg)
82 if (inner_types[-1]=="none"):
83 inner_types=inner_types[:-1]
89 type_name = "Type%d"%num_types
90 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
91 complex_type.setAttribute("name", type_name)
93 choice = complex_type.appendChild(types.createElement("xsd:choice"))
94 for n,t in zip(inner_names,inner_types):
95 element = choice.appendChild(types.createElement("element"))
97 element.setAttribute("name", n)
98 element.setAttribute("type", "%s"%t)
99 element.setAttribute("minOccurs","%d"%min_args)
100 return "xsdl:%s"%type_name
101 elif (isinstance(arg, Parameter)):
102 return (name_simple_type(arg.type))
103 elif type(arg) == ListType or type(arg) == TupleType:
104 inner_type = name_complex_type(arg[0])
105 num_types=num_types+1
106 type_name = "Type%d"%num_types
107 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
108 type_name = filter_argname(type_name)
109 complex_type.setAttribute("name", type_name)
110 complex_content = complex_type.appendChild(types.createElement("xsd:complexContent"))
111 restriction = complex_content.appendChild(types.createElement("xsd:restriction"))
112 restriction.setAttribute("base","soapenc:Array")
113 attribute = restriction.appendChild(types.createElement("xsd:attribute"))
114 attribute.setAttribute("ref","soapenc:arrayType")
115 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
117 return "xsdl:%s"%type_name
119 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
120 num_types=num_types+1
121 type_name = "Type%d"%num_types
122 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
123 type_name = filter_argname(type_name)
124 complex_type.setAttribute("name", type_name)
125 complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
128 inner_type = name_complex_type(arg.fields[k])
129 element=complex_content.appendChild(types.createElement("xsd:element"))
130 element.setAttribute("name",k)
131 element.setAttribute("type",inner_type)
133 return "xsdl:%s"%type_name
135 return (name_simple_type(arg))
137 def name_simple_type(arg_type):
138 # A Parameter is reported as an instance, even though it is serialized as a type <>
139 if type(arg_type) == InstanceType:
140 return (name_simple_type(arg_type.type))
143 if arg_type == DictType:
145 elif arg_type == IntType or arg_type == LongType:
147 elif arg_type == bool:
149 elif arg_type == FloatType:
151 elif arg_type in StringTypes:
155 raise SoapError, "Cannot handle %s objects" % arg_type
158 return (name_complex_type(arg))
160 def add_wsdl_ports_and_bindings (wsdl):
161 for method in apistub.methods:
163 # Skip system. methods
164 if "system." in method:
167 function = apistub.callable(method) # Commented documentation
168 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
169 #print "\n".join(lines)
173 in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
174 in_el.setAttribute("name", method + "_in")
176 for service_name in function.interfaces:
177 if (services.has_key(service_name)):
178 if (not method in services[service_name]):
179 services[service_name].append(method)
181 services[service_name]=[method]
185 if (function.accepts):
186 (min_args, max_args, defaults) = function.args()
187 for (argname,argtype) in zip(max_args,function.accepts):
188 argname = filter_argname(argname)
189 arg_part = in_el.appendChild(wsdl.createElement("part"))
190 arg_part.setAttribute("name", argname)
191 arg_part.setAttribute("type", param_type(argtype))
194 return_type = function.returns
195 out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
196 out_el.setAttribute("name", method + "_out")
197 ret_part = out_el.appendChild(wsdl.createElement("part"))
198 ret_part.setAttribute("name", "Result")
199 ret_part.setAttribute("type", param_type(return_type))
201 # Port connecting arguments with return type
203 port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
204 port_el.setAttribute("name", method + "_port")
206 op_el = port_el.appendChild(wsdl.createElement("operation"))
207 op_el.setAttribute("name", method)
208 inp_el=wsdl.createElement("input")
209 inp_el.setAttribute("message","tns:" + method + "_in")
210 inp_el.setAttribute("name",method+"_request")
211 op_el.appendChild(inp_el)
212 out_el = wsdl.createElement("output")
213 out_el.setAttribute("message","tns:" + method + "_out")
214 out_el.setAttribute("name",method+"_response")
215 op_el.appendChild(out_el)
219 bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
220 bind_el.setAttribute("name", method + "_binding")
221 bind_el.setAttribute("type", "tns:" + method + "_port")
223 soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
224 soap_bind.setAttribute("style", "rpc")
225 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
228 wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
229 wsdl_op.setAttribute("name", method)
230 wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
234 wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
235 input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
236 input_soap_body.setAttribute("use", "encoded")
237 input_soap_body.setAttribute("namespace", "urn:" + method)
238 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
241 wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
242 output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
243 output_soap_body.setAttribute("use", "encoded")
244 output_soap_body.setAttribute("namespace", "urn:" + method)
245 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
248 def add_wsdl_service(wsdl):
249 for service in services.keys():
250 global interface_options
251 if (getattr(interface_options,service)):
252 service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
253 service_el.setAttribute("name", service)
255 for method in services[service]:
257 servport_el = service_el.appendChild(wsdl.createElement("port"))
258 servport_el.setAttribute("name", name + "_port")
259 servport_el.setAttribute("binding", "tns:" + name + "_binding")
261 soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
262 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
265 def get_wsdl_definitions():
266 wsdl_text_header = """
268 name="geniwrapper_autogenerated"
269 targetNamespace="%s/2009/07/sfa.wsdl"
270 xmlns="http://schemas.xmlsoap.org/wsdl/"
271 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
272 xmlns:xsdl="%s/2009/07/schema"
273 xmlns:tns="%s/2009/07/sfa.wsdl"
274 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
275 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
276 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
277 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
279 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
283 def get_wsdl_definitions_and_types():
284 wsdl_text_header = """
286 name="geniwrapper_autogenerated"
287 targetNamespace="%s/2009/07/sfa.wsdl"
288 xmlns="http://schemas.xmlsoap.org/wsdl/"
289 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
290 xmlns:xsdl="%s/2009/07/schema"
291 xmlns:tns="%s/2009/07/sfa.wsdl"
292 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
293 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
294 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
296 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
298 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
299 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
304 global interface_options
306 parser = OptionParser()
307 parser.add_option("-r", "--registry", dest="registry", action="store_true",
308 help="Generate registry.wsdl", metavar="FILE")
309 parser.add_option("-s", "--slice-manager",
310 action="store_true", dest="slicemgr",
311 help="Generate sm.wsdl")
312 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
313 help="Generate am.wsdl")
314 parser.add_option("-l", "--lite", action="store_true", dest="lite",
315 help="Generate LITE version of the interface, in which calls exclude credentials")
316 (interface_options, args) = parser.parse_args()
318 types = get_wsdl_definitions_and_types()
319 wsdl = get_wsdl_definitions()
320 add_wsdl_ports_and_bindings(wsdl)
321 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
322 wsdl.firstChild.appendChild(wsdl_types)
323 add_wsdl_service(wsdl)
325 xml.dom.ext.PrettyPrint(wsdl)
327 if __name__ == "__main__":