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 fold_complex_type_names(acc, arg):
45 python_is_braindead = acc.doc
46 acc = [python_is_braindead,name]
49 def fold_complex_type(acc, arg):
51 name = name_complex_type(arg)
52 complex_types[arg]=name
56 python_is_braindead = name_complex_type(acc)
57 acc = [python_is_braindead,name]
60 def name_complex_type(arg):
64 types_section = types.getElementsByTagName("xsd:schema")[0]
67 if (isinstance(arg, Mixed)):
68 inner_types = reduce(fold_complex_type, arg)
69 inner_names = reduce(fold_complex_type_names, arg)
70 if (inner_types[-1]=="none"):
71 inner_types=inner_types[:-1]
77 type_name = "Type%d"%num_types
78 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
79 complex_type.setAttribute("name", type_name)
81 choice = complex_type.appendChild(types.createElement("xsd:choice"))
82 for n,t in zip(inner_names,inner_types):
83 element = choice.appendChild(types.createElement("element"))
84 element.setAttribute("name", n)
85 element.setAttribute("type", "%s"%t)
86 element.setAttribute("minOccurs","%d"%min_args)
87 return "xsdl:%s"%type_name
88 elif (isinstance(arg, Parameter)):
89 return (name_simple_type(arg.type))
90 elif type(arg) == ListType or type(arg) == TupleType:
91 inner_type = name_complex_type(arg[0])
93 type_name = "Type%d"%num_types
94 complex_type = types_section.appendChild(types.createElement("xsd:simpleType"))
95 complex_type.setAttribute("name", type_name)
96 complex_content = complex_type.appendChild(types.createElement("xsd:list"))
97 complex_content.setAttribute("itemType",inner_type)
98 return "xsdl:%s"%type_name
99 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
100 num_types=num_types+1
101 type_name = "Type%d"%num_types
102 complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
103 complex_type.setAttribute("name", type_name)
104 complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
107 inner_type = name_complex_type(arg.fields[k])
108 element=complex_content.appendChild(types.createElement("xsd:element"))
109 element.setAttribute("name",k)
110 element.setAttribute("type",inner_type)
112 return "xsdl:%s"%type_name
114 return (name_simple_type(arg))
116 def name_simple_type(arg_type):
117 # A Parameter is reported as an instance, even though it is serialized as a type <>
118 if type(arg_type) == InstanceType:
119 return (name_simple_type(arg_type.type))
122 if arg_type == DictType:
124 elif arg_type == IntType or arg_type == LongType:
126 elif arg_type == bool:
128 elif arg_type == FloatType:
130 elif arg_type in StringTypes:
134 raise SoapError, "Cannot handle %s objects" % arg_type
137 return (name_complex_type(arg))
139 def add_wsdl_ports_and_bindings (wsdl):
140 for method in apistub.methods:
142 # Skip system. methods
143 if "system." in method:
146 function = apistub.callable(method) # Commented documentation
147 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
148 #print "\n".join(lines)
152 in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
153 in_el.setAttribute("name", method + "_in")
155 for service_name in function.interfaces:
156 if (services.has_key(service_name)):
157 if (not method in services[service_name]):
158 services[service_name].append(method)
160 services[service_name]=[method]
164 if (function.accepts):
165 (min_args, max_args, defaults) = function.args()
166 for (argname,argtype) in zip(max_args,function.accepts):
167 global interface_options
168 if (not interface_options.lite or (argname!="cred")):
169 arg_part = in_el.appendChild(wsdl.createElement("part"))
170 arg_part.setAttribute("name", argname)
171 arg_part.setAttribute("type", param_type(argtype))
174 return_type = function.returns
175 out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
176 out_el.setAttribute("name", method + "_out")
177 ret_part = out_el.appendChild(wsdl.createElement("part"))
178 ret_part.setAttribute("name", "returnvalue")
179 ret_part.setAttribute("type", param_type(return_type))
181 # Port connecting arguments with return type
183 port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
184 port_el.setAttribute("name", method + "_port")
186 op_el = port_el.appendChild(wsdl.createElement("operation"))
187 op_el.setAttribute("name", method)
188 inp_el=wsdl.createElement("input")
189 inp_el.setAttribute("message","tns:" + method + "_in")
190 inp_el.setAttribute("name",method+"_request")
191 op_el.appendChild(inp_el)
192 out_el = wsdl.createElement("output")
193 out_el.setAttribute("message","tns:" + method + "_out")
194 out_el.setAttribute("name",method+"_response")
195 op_el.appendChild(out_el)
199 bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
200 bind_el.setAttribute("name", method + "_binding")
201 bind_el.setAttribute("type", "tns:" + method + "_port")
203 soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
204 soap_bind.setAttribute("style", "rpc")
205 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
208 wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
209 wsdl_op.setAttribute("name", method)
210 wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
214 wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
215 input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
216 input_soap_body.setAttribute("use", "encoded")
217 input_soap_body.setAttribute("namespace", "urn:" + method)
218 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
221 wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
222 output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
223 output_soap_body.setAttribute("use", "encoded")
224 output_soap_body.setAttribute("namespace", "urn:" + method)
225 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
228 def add_wsdl_service(wsdl):
229 for service in services.keys():
230 global interface_options
231 if (getattr(interface_options,service)):
232 service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
233 service_el.setAttribute("name", service)
235 for method in services[service]:
237 servport_el = service_el.appendChild(wsdl.createElement("port"))
238 servport_el.setAttribute("name", name + "_port")
239 servport_el.setAttribute("binding", "tns:" + name + "_binding")
241 soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
242 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
245 def get_wsdl_definitions():
246 wsdl_text_header = """
248 name="geniwrapper_autogenerated"
249 targetNamespace="%s/2009/07/sfa.wsdl"
250 xmlns="http://schemas.xmlsoap.org/wsdl/"
251 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
252 xmlns:xsdl="%s/2009/07/schema"
253 xmlns:tns="%s/2009/07/sfa.wsdl"
254 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
255 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
256 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
257 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
259 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
263 def get_wsdl_definitions_and_types():
264 wsdl_text_header = """
266 name="geniwrapper_autogenerated"
267 targetNamespace="%s/2009/07/sfa.wsdl"
268 xmlns="http://schemas.xmlsoap.org/wsdl/"
269 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
270 xmlns:xsdl="%s/2009/07/schema"
271 xmlns:tns="%s/2009/07/sfa.wsdl"
272 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
273 xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
274 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
276 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
278 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
279 wsdl = xml.dom.minidom.parseString(wsdl_text_header)
284 global interface_options
286 parser = OptionParser()
287 parser.add_option("-r", "--registry", dest="registry", action="store_true",
288 help="Generate registry.wsdl", metavar="FILE")
289 parser.add_option("-s", "--slice-manager",
290 action="store_true", dest="slicemgr",
291 help="Generate sm.wsdl")
292 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
293 help="Generate am.wsdl")
294 parser.add_option("-l", "--lite", action="store_true", dest="lite",
295 help="Generate LITE version of the interface, in which calls exclude credentials")
296 (interface_options, args) = parser.parse_args()
298 types = get_wsdl_definitions_and_types()
299 wsdl = get_wsdl_definitions()
300 add_wsdl_ports_and_bindings(wsdl)
301 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
302 wsdl.firstChild.appendChild(wsdl_types)
303 add_wsdl_service(wsdl)
305 xml.dom.ext.PrettyPrint(wsdl)
307 if __name__ == "__main__":