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 elif arg_type == IntType or arg_type == LongType:
148 elif arg_type == bool:
150 elif arg_type == FloatType:
152 elif arg_type in StringTypes:
156 raise SoapError, "Cannot handle %s objects" % arg_type
158 def param_type(self, arg):
159 return (self.name_complex_type(arg))
161 def add_wsdl_ports_and_bindings (self):
162 for method in apistub.methods:
164 # Skip system. methods
165 if "system." in method:
168 function = apistub.callable(method) # Commented documentation
169 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
170 #print "\n".join(lines)
174 in_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
175 in_el.setAttribute("name", method + "_in")
177 for service_name in function.interfaces:
178 if (self.services.has_key(service_name)):
179 if (not method in self.services[service_name]):
180 self.services[service_name].append(method)
182 self.services[service_name]=[method]
186 if (function.accepts):
187 (min_args, max_args, defaults) = function.args()
188 for (argname,argtype) in zip(max_args,function.accepts):
189 argname = self.filter_argname(argname)
190 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
191 arg_part.setAttribute("name", argname)
192 arg_part.setAttribute("type", self.param_type(argtype))
195 return_type = function.returns
196 out_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
197 out_el.setAttribute("name", method + "_out")
198 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
199 ret_part.setAttribute("name", "Result")
200 ret_part.setAttribute("type", self.param_type(return_type))
202 # Port connecting arguments with return type
204 port_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("portType"))
205 port_el.setAttribute("name", method + "_port")
207 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
208 op_el.setAttribute("name", method)
209 inp_el=self.wsdl.createElement("input")
210 inp_el.setAttribute("message","tns:" + method + "_in")
211 inp_el.setAttribute("name",method+"_request")
212 op_el.appendChild(inp_el)
213 out_el = self.wsdl.createElement("output")
214 out_el.setAttribute("message","tns:" + method + "_out")
215 out_el.setAttribute("name",method+"_response")
216 op_el.appendChild(out_el)
220 bind_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("binding"))
221 bind_el.setAttribute("name", method + "_binding")
222 bind_el.setAttribute("type", "tns:" + method + "_port")
224 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
225 soap_bind.setAttribute("style", "rpc")
226 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
229 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
230 wsdl_op.setAttribute("name", method)
231 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
235 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
236 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
237 input_soap_body.setAttribute("use", "encoded")
238 input_soap_body.setAttribute("namespace", "urn:" + method)
239 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
242 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
243 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
244 output_soap_body.setAttribute("use", "encoded")
245 output_soap_body.setAttribute("namespace", "urn:" + method)
246 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
249 def add_wsdl_services(self):
250 for service in self.services.keys():
251 if (getattr(self.interface_options,service)):
252 service_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("service"))
253 service_el.setAttribute("name", service)
255 for method in self.services[service]:
257 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
258 servport_el.setAttribute("name", name + "_port")
259 servport_el.setAttribute("binding", "tns:" + name + "_binding")
261 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
262 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
265 def compute_wsdl_definitions(self):
266 wsdl_text_header = """
268 name="sfa_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 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
282 def compute_wsdl_definitions_and_types(self):
283 wsdl_text_header = """
285 name="sfa_autogenerated"
286 targetNamespace="%s/2009/07/sfa.wsdl"
287 xmlns="http://schemas.xmlsoap.org/wsdl/"
288 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
289 xmlns:xsdl="%s/2009/07/schema"
290 xmlns:tns="%s/2009/07/sfa.wsdl"
291 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
292 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
293 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
295 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
297 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
298 self.types = xml.dom.minidom.parseString(wsdl_text_header)
301 def add_wsdl_types(self):
302 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
303 self.wsdl.firstChild.appendChild(wsdl_types)
305 def generate_wsdl(self):
306 self.compute_wsdl_definitions_and_types()
307 self.compute_wsdl_definitions()
308 self.add_wsdl_ports_and_bindings()
309 self.add_wsdl_types()
310 self.add_wsdl_services()
312 def pretty_print(self):
314 xml.dom.ext.PrettyPrint(self.wsdl)
316 raise Exception("Empty WSDL")
319 parser = OptionParser()
320 parser.add_option("-r", "--registry", dest="registry", action="store_true",
321 help="Generate registry.wsdl", metavar="FILE")
322 parser.add_option("-s", "--slice-manager",
323 action="store_true", dest="slicemgr",
324 help="Generate sm.wsdl")
325 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
326 help="Generate am.wsdl")
327 parser.add_option("-l", "--lite", action="store_true", dest="lite",
328 help="Generate LITE version of the interface, in which calls exclude credentials")
329 (interface_options, args) = parser.parse_args()
331 gen = WSDLGen(interface_options)
336 if __name__ == "__main__":