3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for sfa
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
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 filter_argname(self,argname):
46 if (not self.interface_options.lite or (argname!="cred")):
47 if (argname.find('(') != -1):
48 # The name has documentation in it :-/
49 brackright = argname.split('(')[1]
50 if (brackright.find(')') == -1):
51 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
52 inbrack = brackright.split(')')[0]
56 def fold_complex_type_names(self,acc, arg):
65 def fold_complex_type(self,acc, arg):
66 name = self.name_complex_type(arg)
67 self.complex_types[arg]=name
71 p_i_b = self.name_complex_type(acc)
75 def name_complex_type(self,arg):
77 types_section = self.types.getElementsByTagName("xsd:schema")[0]
80 if (isinstance(arg, Mixed)):
81 inner_types = reduce(self.fold_complex_type, arg)
82 inner_names = reduce(self.fold_complex_type_names, arg)
83 if (inner_types[-1]=="none"):
84 inner_types=inner_types[:-1]
89 self.num_types=self.num_types+1
90 type_name = "Type%d"%self.num_types
91 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
92 complex_type.setAttribute("name", type_name)
94 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
95 for n,t in zip(inner_names,inner_types):
96 element = choice.appendChild(self.types.createElement("element"))
97 n = self.filter_argname(n)
98 element.setAttribute("name", n)
99 element.setAttribute("type", "%s"%t)
100 element.setAttribute("minOccurs","%d"%min_args)
101 return "xsdl:%s"%type_name
102 elif (isinstance(arg, Parameter)):
103 return (self.name_simple_type(arg.type))
104 elif type(arg) == ListType or type(arg) == TupleType:
105 inner_type = self.name_complex_type(arg[0])
106 self.num_types=self.num_types+1
107 type_name = "Type%d"%self.num_types
108 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
109 type_name = self.filter_argname(type_name)
110 complex_type.setAttribute("name", type_name)
111 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
112 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
113 restriction.setAttribute("base","soapenc:Array")
114 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
115 attribute.setAttribute("ref","soapenc:arrayType")
116 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
118 return "xsdl:%s"%type_name
120 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
121 self.num_types=self.num_types+1
122 type_name = "Type%d"%self.num_types
123 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
124 type_name = self.filter_argname(type_name)
125 complex_type.setAttribute("name", type_name)
126 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
129 inner_type = self.name_complex_type(arg.fields[k])
130 element=complex_content.appendChild(self.types.createElement("xsd:element"))
131 element.setAttribute("name",k)
132 element.setAttribute("type",inner_type)
134 return "xsdl:%s"%type_name
136 return (self.name_simple_type(arg))
138 def name_simple_type(self,arg_type):
139 # A Parameter is reported as an instance, even though it is serialized as a type <>
140 if type(arg_type) == InstanceType:
141 return (self.name_simple_type(arg_type.type))
144 if arg_type == DictType:
146 if arg_type in (ListType, TupleType):
147 return "xsd:arrayType"
148 elif arg_type == IntType or arg_type == LongType:
150 elif arg_type == bool:
152 elif arg_type == FloatType:
154 elif arg_type in StringTypes:
158 raise SoapError, "Cannot handle %s objects" % arg_type
160 def param_type(self, arg):
161 return (self.name_complex_type(arg))
163 def add_wsdl_ports_and_bindings (self):
164 for method in apistub.methods:
166 # Skip system. methods
167 if "system." in method:
170 function = apistub.callable(method) # Commented documentation
171 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
172 #print "\n".join(lines)
176 in_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
177 in_el.setAttribute("name", method + "_in")
179 for service_name in function.interfaces:
180 if (self.services.has_key(service_name)):
181 if (not method in self.services[service_name]):
182 self.services[service_name].append(method)
184 self.services[service_name]=[method]
188 if (function.accepts):
189 (min_args, max_args, defaults) = function.args()
190 for (argname,argtype) in zip(max_args,function.accepts):
191 argname = self.filter_argname(argname)
192 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
193 arg_part.setAttribute("name", argname)
194 arg_part.setAttribute("type", self.param_type(argtype))
197 return_type = function.returns
198 out_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
199 out_el.setAttribute("name", method + "_out")
200 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
201 ret_part.setAttribute("name", "Result")
202 ret_part.setAttribute("type", self.param_type(return_type))
204 # Port connecting arguments with return type
206 port_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("portType"))
207 port_el.setAttribute("name", method + "_port")
209 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
210 op_el.setAttribute("name", method)
211 inp_el=self.wsdl.createElement("input")
212 inp_el.setAttribute("message","tns:" + method + "_in")
213 inp_el.setAttribute("name",method+"_request")
214 op_el.appendChild(inp_el)
215 out_el = self.wsdl.createElement("output")
216 out_el.setAttribute("message","tns:" + method + "_out")
217 out_el.setAttribute("name",method+"_response")
218 op_el.appendChild(out_el)
222 bind_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("binding"))
223 bind_el.setAttribute("name", method + "_binding")
224 bind_el.setAttribute("type", "tns:" + method + "_port")
226 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
227 soap_bind.setAttribute("style", "rpc")
228 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
231 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
232 wsdl_op.setAttribute("name", method)
233 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
237 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
238 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
239 input_soap_body.setAttribute("use", "encoded")
240 input_soap_body.setAttribute("namespace", "urn:" + method)
241 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
244 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
245 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
246 output_soap_body.setAttribute("use", "encoded")
247 output_soap_body.setAttribute("namespace", "urn:" + method)
248 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
251 def add_wsdl_services(self):
252 for service in self.services.keys():
253 if (getattr(self.interface_options,service)):
254 service_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("service"))
255 service_el.setAttribute("name", service)
257 for method in self.services[service]:
259 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
260 servport_el.setAttribute("name", name + "_port")
261 servport_el.setAttribute("binding", "tns:" + name + "_binding")
263 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
264 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
267 def compute_wsdl_definitions(self):
268 wsdl_text_header = """
270 name="sfa_autogenerated"
271 targetNamespace="%s/2009/07/sfa.wsdl"
272 xmlns="http://schemas.xmlsoap.org/wsdl/"
273 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
274 xmlns:xsdl="%s/2009/07/schema"
275 xmlns:tns="%s/2009/07/sfa.wsdl"
276 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
277 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
278 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
279 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
281 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
284 def compute_wsdl_definitions_and_types(self):
285 wsdl_text_header = """
287 name="sfa_autogenerated"
288 targetNamespace="%s/2009/07/sfa.wsdl"
289 xmlns="http://schemas.xmlsoap.org/wsdl/"
290 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
291 xmlns:xsdl="%s/2009/07/schema"
292 xmlns:tns="%s/2009/07/sfa.wsdl"
293 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
294 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
295 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
297 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
299 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
300 self.types = xml.dom.minidom.parseString(wsdl_text_header)
303 def add_wsdl_types(self):
304 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
305 self.wsdl.firstChild.appendChild(wsdl_types)
307 def generate_wsdl(self):
308 self.compute_wsdl_definitions_and_types()
309 self.compute_wsdl_definitions()
310 self.add_wsdl_ports_and_bindings()
311 self.add_wsdl_types()
312 self.add_wsdl_services()
314 def pretty_print(self):
316 xml.dom.ext.PrettyPrint(self.wsdl)
318 raise Exception("Empty WSDL")
321 parser = OptionParser()
322 parser.add_option("-r", "--registry", dest="registry", action="store_true",
323 help="Generate registry.wsdl", metavar="FILE")
324 parser.add_option("-s", "--slice-manager",
325 action="store_true", dest="slicemgr",
326 help="Generate sm.wsdl")
327 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
328 help="Generate am.wsdl")
329 parser.add_option("-c", "--component", action="store_true", dest="component",
330 help="Generate cm.wsdl")
331 parser.add_option("-l", "--lite", action="store_true", dest="lite",
332 help="Generate LITE version of the interface, in which calls exclude credentials")
333 (interface_options, args) = parser.parse_args()
335 gen = WSDLGen(interface_options)
340 if __name__ == "__main__":